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
34c35
< c = getopt_long (argc, argv, "abc:d:f:",
---
64a66,69
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
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'},
< c = getopt_long (argc, argv, "abc:d:f:",
---
c = getopt_long (argc, argv, "abc:d:f:C: ",
case 'C':
printf ("option -C with value `%s'\n", optarg);
break;
>
printf ("option -C with value `%s'\n", optarg);
break;
>
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
Comment