ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i found out that you can reinitialize getopt by setting optind = 0. i tried it and it worked. the thing is i didn't see it documented on the man pages or the info pages. did i miss something, or is this just something that's "well-known but undocumented".
optind, AFAIK is an internal counter telling getopt where to find it's next operand. That is, argv + optind is where it'll find it's next argument. So, resetting it will obviously make it look at arguments it has previously seen. It's probably not documented as it's not to be meddled with and not many situations demand this kind of a behaviour.
Setting it to 0 might not be such a good idea, as argv + optind = argv. So, argv[0] will be accessed and that is the program name. But, depending on when optind is internally incremented, you might find that even though you set it to 0, it might be incremented to 1.
Arvind
PS: I just checked the glibc implementation of getopt. optind is checked to see if it's 0. If it is, it's set to 1, and getopt is re-initialized. Nothing special.
PS: Have you tried setting it to a negative number?
there's also a static variable nextchar (as mentioned in the man page) that's involved. for example, if you used a command of the form "command -abc", getopt would have optind = 1 while it parses all three options, and nextchar would be incremented as the options are retrieved. but, apparently, setting optind = 0 resets everything, including nextchar. that's good. but, i like to have everything documented in the man page; i don't like to discover things by tinkering with them.
Well, it's not meant to be known at all. You're given an interface to work with and that interface is documented. But, if you change an internal variable and something happens, then, you can hardly complain that it's undocumented behaviour. :-)
If this was in C++ or something, it'd be a private variable and you couldn't access it anyway. But, the abstraction facilities offered by C are not great. So, you can tinker with the internal variables of getopt. If such behaviour was wanted by many, then, a function called "_getopt_initialize" (as in the sources) would've been documented.
optind is meant purely for finding out which is the next non-option argument, and to use the rest of them.
Yes, I suppose they set a bad example. As they're all GNU code, they know how each one will be implemented and so, they make use of that fact to (maybe) make it work a little faster. But, I'm sure you'll agree that it's not a very good idea to be so dependant on implementation. What happens if the getopt implementation changes?
For the record, the getopt file says, about optind:
Quote:
On entry to `getopt', zero means this is the first call; initialize.
Later, it says:
Quote:
Formerly, initialization of getopt depended on optind==0, which causes problems with re-calling getopt as programs generally don't know that.
yes, i agree with you that an undocumented feature shouldn't be used.
by the way, what you quoted had more information than was contained in the man pages. where did you find it?
arvind, thank you very much. no need to send it to me, though. i'm walking away from this with the conclusion that this feature is undocumented (unless one reads the source code itself -- because even the comments to the code leave room for speculation).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.