Results 1 to 6 of 6
I was wondering if there was a getopt_long alternative that advances the pointer on argv (for cleanliness purposes) automatically....
- 11-08-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 6
getopt with advancing pointer?
I was wondering if there was a getopt_long alternative that advances the pointer on argv (for cleanliness purposes) automatically.
- 11-08-2007 #2
I seriously doubt that in the public arena there's an alternative to getopt_long() that advances argv "automatically".
The way you normally use getopt_long() is to have a loop in which you call it until all the parameters have been absorbed. Then, of course (and I say "of course" because I know you've read the getopt_long() man page before bothering anyone about this) optind will indicate the first element of argv in which you're interested. That's about as clean as you can get.
But maybe you want something that works a little differently. If you want a super-duper, handy-dandy, nifty-swifty, neato-keeno, peachy-keeny alternative that will do everything for you except wipe your nose, try this at the command line:
My advice, though, is to stick with getopt_long(). I think more people use it, and therefore more people are familiar with it, and therefore your program will be maintainable by more people.Code:man popt
Hope this helps.
- 11-08-2007 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 6
optind actually isn't mentioned in my man for getopt, which I wouldn't think of as a particularly good thing.
Anyways, what I meant was a pointer to argv[optind], but at this point I don't really need to worry about that.
- 11-08-2007 #4This bothered me too. My man page mentions it prominently. So I googledoptind actually isn't mentioned in my man for getopt, which I wouldn't think of as a particularly good thing
and the first man page I came across organizes things quite a bit differently from the man page I'm used to. The man page I'm used to combines getopt() and getopt_long() in the same page. But the one that I found on the web documents just getopt_long(), and says this:Code:man getopt_long
The implication, which they don't spell out clearly, is that for a full understanding of how getopt_long() works, you need to understand fully how getopt() works. They (it's always They, isn't it?) should make it more clear.The getopt_long() function is similar to getopt(3) but it accepts options in two forms: words and characters. The getopt_long() function provides a superset of the functionality of getopt(3). The getopt_long() function can be used in two ways. In the first way, every long option understood by the program has a corresponding short option, and the option structure is only used to translate from long options to short options. When used in this fashion, getopt_long() behaves identically to getopt(3). This is a good way to add long option processing to an existing program with the minimum of rewriting.
They do mention optind, however; it's buried in their example, after the loop is complete:
and I'm wondering whether that might be in your man page as well. If it's not, that is truly freaky.Code:argc -= optind; argv += optind;
I think the two functions are better documented on the same man page. I have found GNU man pages which do this; one is dated 2002, the one on my own system, and another dated 1995 I found on the web. The getopt_long()-only one that I found on the web was for Mac OSX, and dated 2000.--
Bill
Old age and treachery will overcome youth and skill.
- 11-08-2007 #5Just Joined!
- Join Date
- Nov 2007
- Posts
- 6
Yea, pretty much my handling of arguments is
progname options arg arg arg (...) : arg arg arg (...), so the non-option args are pretty important.
- 11-08-2007 #6
So I have three questions for you.
- Is your problem solved?
- Does your getopt_long() man page have the difficulty I described in the previous post?
- Would you be so kind as to post the bottom line of your getopt_long() man page? It could look something like this:
That one was good.Code:GNU 2002-02-16 GETOPT(3)
Or this:
That one was bad.Code:BSD April 1, 2000 BSD
I'd just like to get a sense of which folks document this right and which ones do it wrong.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote