Skip to content

Commit 89a77b3

Browse files
authored
Merge pull request #5180 from chu11/issue5178_liboptparse_missing_argument
liboptparse: correct message on missing argument
2 parents 11d4362 + 5a51e3a commit 89a77b3

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/common/liboptparse/optparse.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,14 @@ static char * optstring_create ()
11971197
char *optstring = calloc (4, 1);
11981198
if (optstring == NULL)
11991199
return (NULL);
1200-
optstring[0] = '\0';
1200+
/* Per getopt(3) manpage
1201+
*
1202+
* "If the first character ... of optstring is a colon (':'), then
1203+
* getopt() returns ':' instead of '?' to indicate a missing
1204+
* option argument"
1205+
*/
1206+
optstring[0] = ':';
1207+
optstring[1] = '\0';
12011208
return (optstring);
12021209
}
12031210

@@ -1365,7 +1372,13 @@ int optparse_parse_args (optparse_t *p, int argc, char *argv[])
13651372
&li, &d, p->posixly_correct)) >= 0) {
13661373
struct option_info *opt;
13671374
struct optparse_option *o;
1368-
if (c == '?') {
1375+
if (c == ':') {
1376+
(*p->log_fn) ("%s: '%s' missing argument\n",
1377+
fullname, argv[d.optind-1]);
1378+
d.optind = -1;
1379+
break;
1380+
}
1381+
else if (c == '?') {
13691382
if (d.optopt != '\0')
13701383
(*p->log_fn) ("%s: unrecognized option '-%c'\n",
13711384
fullname, d.optopt);

src/common/liboptparse/test/optparse.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,18 @@ test two: unrecognized option '-Z'\n\
934934
Try `test two --help' for more information.\n",
935935
"bad argument error message is expected");
936936

937+
// Test missing argument prints expected error
938+
char *av43[] = { "test", "two", "-t", NULL};
939+
ac = ARRAY_SIZE (av43) - 1;
940+
941+
diag ("parsing test two -t");
942+
n = optparse_run_subcommand (a, ac, av43);
943+
ok (n == -1,
944+
"optparse_run_subcommand with missing argument fails");
945+
946+
usage_output_is ("test two: '-t' missing argument\n",
947+
"missing argument error message is expected");
948+
937949
// Test no subcommand (and subcommand required) prints error
938950
char *av5[] = { "test", NULL };
939951
ac = ARRAY_SIZE (av5) - 1;
@@ -1346,15 +1358,15 @@ static void test_optional_args ()
13461358

13471359
int main (int argc, char *argv[])
13481360
{
1349-
plan (296);
1361+
plan (299);
13501362

13511363
test_convenience_accessors (); /* 36 tests */
13521364
test_usage_output (); /* 46 tests */
13531365
test_option_cb (); /* 16 tests */
13541366
test_errors (); /* 9 tests */
13551367
test_multiret (); /* 19 tests */
13561368
test_data (); /* 8 tests */
1357-
test_subcommand (); /* 67 tests */
1369+
test_subcommand (); /* 70 tests */
13581370
test_long_only (); /* 13 tests */
13591371
test_optional_argument (); /* 9 tests */
13601372
test_corner_case (); /* 3 tests */

0 commit comments

Comments
 (0)