Fix: Error out when passing an argument to long option that takes no argument
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 9 Apr 2021 16:39:19 +0000 (12:39 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Sat, 10 Apr 2021 01:35:14 +0000 (21:35 -0400)
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 <simon.marchi@efficios.com>
argpar/argpar.c
tests/test_argpar.c

index 13f66812894037b669924eb4446fd3f5472e464b..b110f18a90e2432a7f9469b71851af90e834aa2c 100644 (file)
@@ -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 */
index 458c69e5bd2d9c00cee5ff5d2ae981ff7003b7db..f4944eac1e37d1a836e80966ae4118d1e166c45e 100644 (file)
@@ -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();
This page took 0.034383 seconds and 4 git commands to generate.