F
ff0000
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_argument (or 1) if the option requires an
argument; or
optional_argument (or 2) if the option takes an
optional argu-
ment.
Reading this i expect that an option set with "optional_argument"
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@tsi00588pc:tmp$ diff ./glibc-2.6.1/manual/examples/longopt.c
longopt.c
27a28
< c = getopt_long (argc, argv, "abc:d:f:",
---
Building it:
ff0000@tsi00588pc:tmp$ gcc -Wall -Werror -o longopt longopt.c
ff0000@tsi00588pc:tmp$
Testing "-C" option with and without argument:
ff0000@tsi00588pc:tmp$ ./longopt -C 1
option -C with value `1'
ff0000@tsi00588pc:tmp$ ./longopt -C
../longopt: option requires an argument -- C
ff0000@tsi00588pc:tmp$
Why in the latter case it tells me that the option *requires* an
argument
even if it's set to "optional_argument"?
It behaves like a "required_argument"' s option:
ff0000@tsi00588pc:tmp$ ./longopt -c
../longopt: option requires an argument -- c
ff0000@tsi00588pc:tmp$
Another one: is it normal that an option's argument could be another
option?
ff0000@tsi00588pc:tmp$ ./longopt -c -c
option -c with value `-c'
ff0000@tsi00588pc: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_argument (or 1) if the option requires an
argument; or
optional_argument (or 2) if the option takes an
optional argu-
ment.
Reading this i expect that an option set with "optional_argument"
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@tsi00588pc:tmp$ diff ./glibc-2.6.1/manual/examples/longopt.c
longopt.c
27a28
34c35{"crates", optional_argument, 0, 'C'},
< 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@tsi00588pc:tmp$ gcc -Wall -Werror -o longopt longopt.c
ff0000@tsi00588pc:tmp$
Testing "-C" option with and without argument:
ff0000@tsi00588pc:tmp$ ./longopt -C 1
option -C with value `1'
ff0000@tsi00588pc:tmp$ ./longopt -C
../longopt: option requires an argument -- C
ff0000@tsi00588pc:tmp$
Why in the latter case it tells me that the option *requires* an
argument
even if it's set to "optional_argument"?
It behaves like a "required_argument"' s option:
ff0000@tsi00588pc:tmp$ ./longopt -c
../longopt: option requires an argument -- c
ff0000@tsi00588pc:tmp$
Another one: is it normal that an option's argument could be another
option?
ff0000@tsi00588pc:tmp$ ./longopt -c -c
option -c with value `-c'
ff0000@tsi00588pc:tmp$
?
Thanks a lot.
ff0000