From 49919efe1479f9ce6f5311df32a10d3d7b90d0af Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Thu, 7 Sep 2017 14:34:23 -0700 Subject: [PATCH 1/2] optparse: emit correct unrecognized option character Emit the correct unrecognized option character on error when short options are used and grouped on the commandline. Fixes #1179. --- src/common/liboptparse/optparse.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/common/liboptparse/optparse.c b/src/common/liboptparse/optparse.c index 0cfef4a5dac9..601ebdb33e31 100644 --- a/src/common/liboptparse/optparse.c +++ b/src/common/liboptparse/optparse.c @@ -1279,8 +1279,12 @@ int optparse_parse_args (optparse_t *p, int argc, char *argv[]) struct option_info *opt; struct optparse_option *o; if (c == '?') { - (*p->log_fn) ("%s: unrecognized option '%s'\n", - fullname, argv[d.optind-1]); + if (d.optopt != '\0') + (*p->log_fn) ("%s: unrecognized option '-%c'\n", + fullname, d.optopt); + else + (*p->log_fn) ("%s: unrecognized option '%s'\n", + fullname, argv[d.optind-1]); (*p->log_fn) ("Try `%s --help' for more information.\n", fullname); d.optind = -1; break; From aed7f57bb4d654fae3682a52ac1cc1a6d01c7d90 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Thu, 7 Sep 2017 14:27:19 -0700 Subject: [PATCH 2/2] optparse/test: add test for bad short option error messages Add test that the correct unrecognized short option is displayed in the optparse error messages when getopt calls out unknown option. --- src/common/liboptparse/test/optparse.c | 31 ++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/common/liboptparse/test/optparse.c b/src/common/liboptparse/test/optparse.c index 0f789a7857a4..6cf306e867b4 100644 --- a/src/common/liboptparse/test/optparse.c +++ b/src/common/liboptparse/test/optparse.c @@ -830,6 +830,33 @@ test two: unrecognized option '--unknown'\n\ Try `test two --help' for more information.\n", "bad argument error message is expected"); + // Test unknown short option prints expected error + char *av41[] = { "test", "two", "-X", NULL }; + ac = sizeof (av41) / sizeof (av41[0]) - 1; + + diag ("parsing test two -X"); + n = optparse_run_subcommand (a, ac, av41); + ok (n == -1, "optparse_run_subcommand with bad short opt returns error"); + + usage_output_is ("\ +test two: unrecognized option '-X'\n\ +Try `test two --help' for more information.\n", + "bad argument error message is expected"); + + // Test unknown short option with good option prints expected error + char *av42[] = { "test", "two", "-Zt", "foo", NULL}; + ac = sizeof (av42) / sizeof (av42[0]) - 1; + + diag ("parsing test two -Zt foo"); + n = optparse_run_subcommand (a, ac, av42); + ok (n == -1, + "optparse_run_subcommand with bad short opt mixed with good fails"); + + usage_output_is ("\ +test two: unrecognized option '-Z'\n\ +Try `test two --help' for more information.\n", + "bad argument error message is expected"); + // Test no subcommand (and subcommand required) prints error char *av5[] = { "test", NULL }; ac = sizeof (av5) / sizeof (av5[0]) - 1; @@ -1076,7 +1103,7 @@ void test_non_option_arguments (void) int main (int argc, char *argv[]) { - plan (245); + plan (251); test_convenience_accessors (); /* 35 tests */ test_usage_output (); /* 42 tests */ @@ -1084,7 +1111,7 @@ int main (int argc, char *argv[]) test_errors (); /* 9 tests */ test_multiret (); /* 19 tests */ test_data (); /* 8 tests */ - test_subcommand (); /* 56 tests */ + test_subcommand (); /* 62 tests */ test_long_only (); /* 13 tests */ test_optional_argument (); /* 9 tests */ test_corner_case (); /* 3 tests */