Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optparse: fix incorrect error message when invalid short option is grouped with valid short options #1183

Merged
merged 2 commits into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/common/liboptparse/optparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 29 additions & 2 deletions src/common/liboptparse/test/optparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1076,15 +1103,15 @@ 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 */
test_option_cb (); /* 16 tests */
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 */
Expand Down