From: Simon Marchi Date: Fri, 9 Apr 2021 16:39:19 +0000 (-0400) Subject: Fix: Error out when passing an argument to long option that takes no argument X-Git-Url: http://git.efficios.com/?p=argpar.git;a=commitdiff_plain;h=430fe8864aa297e333761b75eaad70900043e3d3 Fix: Error out when passing an argument to long option that takes no argument If the user defines a long option `--foo` and passes `--foo=arg`, the current behavior is that the option foo is recognized and the argument is ignored. Change that to be an error. It is currently not possible to pass arguments to short options using an equal sign, so this doesn't apply to short options. Change-Id: I4b0c50398161aebc81789fdb3fce19a80a5f6646 Signed-off-by: Simon Marchi --- diff --git a/argpar/argpar.c b/argpar/argpar.c index 13f6681..b110f18 100644 --- a/argpar/argpar.c +++ b/argpar/argpar.c @@ -453,6 +453,15 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg, opt_arg = next_orig_arg; *used_next_orig_arg = true; } + } else if (eq_pos) { + /* + * Unexpected `--opt=arg` style for a long option which + * doesn't accept an argument. + */ + argpar_string_append_printf(&parse_ret->error, + "Unexpected argument for option `--%s`", + long_opt_name); + goto error; } /* Create and append option argument */ diff --git a/tests/test_argpar.c b/tests/test_argpar.c index 458c69e..f4944ea 100644 --- a/tests/test_argpar.c +++ b/tests/test_argpar.c @@ -629,11 +629,23 @@ void fail_tests(void) "While parsing argument #2 (`--`): Invalid argument", descrs); } + + { + const struct argpar_opt_descr descrs[] = { + { 0, 'c', "chevre", false }, + ARGPAR_OPT_DESCR_SENTINEL + }; + + test_fail( + "--chevre=fromage", + "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`", + descrs); + } } int main(void) { - plan_tests(129); + plan_tests(132); succeed_tests(); fail_tests(); return exit_status();