diff options
Diffstat (limited to 'src/nvgetopt.c')
-rw-r--r-- | src/nvgetopt.c | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/nvgetopt.c b/src/nvgetopt.c index 8e8120e..14a1d4c 100644 --- a/src/nvgetopt.c +++ b/src/nvgetopt.c @@ -220,14 +220,26 @@ int nvgetopt(int argc, char *argv[], const NVGetoptOption *options, } if (strval) *strval = strdup(argument); } else { - argv_index++; - if (argv_index >= argc) { - fprintf(stderr, "%s: option \"%s\" requires an argument.\n", - argv[0], arg); - goto done; + /* + * if the argument is optional, and we're either at the + * end of the argv list, or the next argv starts with '-', + * then assume there is no argument for this option + */ + + if ((o->flags & NVGETOPT_ARGUMENT_IS_OPTIONAL) && + ((argv_index == (argc - 1)) || + (argv[argv_index + 1][0] == '-'))) { + if (strval) *strval = NULL; + } else { + argv_index++; + if (argv_index >= argc) { + fprintf(stderr, "%s: option \"%s\" requires an argument.\n", + argv[0], arg); + goto done; + } + if (strval) *strval = argv[argv_index]; } - if (strval) *strval = argv[argv_index]; - } + } } ret = o->val; |