understanding getopt_long()'s hasarg setting

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
{"crates", optional_argument, 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@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
 
B

Ben Bacarisse

ff0000 said:
Preamble: i'm a newbie :)

This question would have been better in comp.unix.programmer. 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;
};
{"crates", optional_argument, 0, 'C'},
c = getopt_long (argc, argv, "abc:d:f:C:",
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top