470,849 Members | 1,097 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

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_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
Jun 27 '08 #1
1 2160
ff0000 <ff*******@gmail.comwrites:
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;
};
<snip>
{"crates", optional_argument, 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.
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Karthik | last post: by
2 posts views Thread by jenea | last post: by
5 posts views Thread by Krumble Bunk | last post: by
1 post views Thread by Angel Tsankov | last post: by
7 posts views Thread by cartoper | last post: by
5 posts views Thread by mosscliffe | last post: by
9 posts views Thread by Daniel Smedegaard Buus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.