understanding getopt_long()'s hasarg setting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ff0000

    understanding getopt_long()'s hasarg setting

    Hi everyone,

    Preamble: i'm a newbie :-)
    I've a doubt about the behaviour of variable "hasarg" in the option
    struct (from getopt(3)):

    struct option {
    const char *name;
    int has_arg;
    int *flag;
    int val;
    };

    has_arg
    is: no_argument (or 0) if the option does not take an
    argument;
    required_argume nt (or 1) if the option requires an
    argument; or
    optional_argume nt (or 2) if the option takes an
    optional argu-
    ment.

    Reading this i expect that an option set with "optional_argum ent"
    could get or not an
    argument, but proprably i'm wrong...
    I take longopt.c example from GNU libc source and i add one option,
    here's the diff:

    ff0000@tsi00588 pc:tmp$ diff ./glibc-2.6.1/manual/examples/longopt.c
    longopt.c
    27a28
    {"crates", optional_argume nt, 0, 'C'},
    34c35
    < c = getopt_long (argc, argv, "abc:d:f:",
    ---
    c = getopt_long (argc, argv, "abc:d:f:C: ",
    64a66,69
    case 'C':
    printf ("option -C with value `%s'\n", optarg);
    break;
    >
    Building it:

    ff0000@tsi00588 pc:tmp$ gcc -Wall -Werror -o longopt longopt.c
    ff0000@tsi00588 pc:tmp$

    Testing "-C" option with and without argument:

    ff0000@tsi00588 pc:tmp$ ./longopt -C 1
    option -C with value `1'
    ff0000@tsi00588 pc:tmp$ ./longopt -C
    ../longopt: option requires an argument -- C
    ff0000@tsi00588 pc:tmp$

    Why in the latter case it tells me that the option *requires* an
    argument
    even if it's set to "optional_argum ent"?
    It behaves like a "required_argum ent"' s option:

    ff0000@tsi00588 pc:tmp$ ./longopt -c
    ../longopt: option requires an argument -- c
    ff0000@tsi00588 pc:tmp$

    Another one: is it normal that an option's argument could be another
    option?

    ff0000@tsi00588 pc:tmp$ ./longopt -c -c
    option -c with value `-c'
    ff0000@tsi00588 pc:tmp$

    ?

    Thanks a lot.
    ff0000
  • Ben Bacarisse

    #2
    Re: understanding getopt_long()'s hasarg setting

    ff0000 <ff0000.it@gmai l.comwrites:
    Preamble: i'm a newbie :-)
    This question would have been better in comp.unix.progr ammer. You'd
    hit a wider pool of people who know getopt_long -- it is not standard
    C. I've set followup-to that group.
    I've a doubt about the behaviour of variable "hasarg" in the option
    struct (from getopt(3)):
    >
    struct option {
    const char *name;
    int has_arg;
    int *flag;
    int val;
    };
    <snip>
    {"crates", optional_argume nt, 0, 'C'},
    <snip>
    c = getopt_long (argc, argv, "abc:d:f:C: ",
    <snip>
    case 'C':
    printf ("option -C with value `%s'\n", optarg);
    break;
    has_arg affects only the long version permitting '--crates' and
    '--crates=value'. As a GNU extension you can use double :: to extend
    the optional nature of an argument to the single-letter form, but
    optional arguments never just *follow* a single letter flag, they come
    from the rest of the argument. So using:

    c = getopt_long (argc, argv, "abc:d:f:C: :", ...)

    the user can write both '-C' and '-Cvalue', but '-Ca' will be taken as
    --crates=a and not as -C -a.

    --
    Ben.

    Comment

    Working...