Find the answer to your Linux question:
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....
  1. #1
    Just 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.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:
    Code:
    man popt
    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.

    Hope this helps.

  3. #3
    Just 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.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    optind actually isn't mentioned in my man for getopt, which I wouldn't think of as a particularly good thing
    This bothered me too. My man page mentions it prominently. So I googled
    Code:
    man getopt_long
    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:
    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.
    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.

    They do mention optind, however; it's buried in their example, after the loop is complete:
    Code:
    argc -= optind;
    argv += optind;
    and I'm wondering whether that might be in your man page as well. If it's not, that is truly freaky.

    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.

  5. #5
    Just 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.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    So I have three questions for you.
    1. Is your problem solved?
    2. Does your getopt_long() man page have the difficulty I described in the previous post?
    3. Would you be so kind as to post the bottom line of your getopt_long() man page? It could look something like this:
      Code:
      GNU                         2002-02-16                  GETOPT(3)
      That one was good.

      Or this:
      Code:
      BSD                              April 1, 2000                             BSD
      That one was bad.


    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...