argpar-utils: tweak unknown option error message
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 24 Aug 2021 01:42:42 +0000 (21:42 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 16 Dec 2021 00:13:39 +0000 (19:13 -0500)
Add the "While parsing argument #X" context, that is used when other
errors are encountered.  Before:

    Error: Unknown option `--hello`

After:

    Error: While parsing argument #1 (`--hello`): Unknown option `--hello`

Change-Id: Ifd84a1e8b1fe0456e09ff154cc515eaa2850a7e1
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/argpar-utils/argpar-utils.c
tests/regression/tools/trigger/test_add_trigger_cli

index 1f1f38bccdb69afac143ec88c092089688ff3bac..6f5548917abf755f2c7fda3c9776f08a7d2de2d6 100644 (file)
@@ -54,7 +54,7 @@ char *format_arg_error_v(const struct argpar_error *error,
        {
        case ARGPAR_ERROR_TYPE_MISSING_OPT_ARG:
        {
-               int orig_index = argpar_error_orig_index(error);
+               const int orig_index = argpar_error_orig_index(error);
                const char *arg = argv[orig_index];
 
                ret = strutils_appendf(&str,
@@ -92,10 +92,12 @@ char *format_arg_error_v(const struct argpar_error *error,
        }
        case ARGPAR_ERROR_TYPE_UNKNOWN_OPT:
        {
+               int orig_index = argpar_error_orig_index(error);
                const char *unknown_opt = argpar_error_unknown_opt_name(error);
 
                ret = strutils_appendf(&str,
-                       "Unknown option `%s`", unknown_opt);
+                       WHILE_PARSING_ARG_N_ARG_FMT "Unknown option `%s`",
+                       orig_index + 1, argv[orig_index], unknown_opt);
 
                if (ret < 0) {
                        goto end;
index 49cff975a3644803eef1b030cabae9d0a7899cb6..ecf04dd46589a20dbb60225063b42563c82a5c6d 100755 (executable)
@@ -409,7 +409,7 @@ test_success "--action snapshot-session with ctrl/data URIs" "notify-15"\
 test_failure "no args" "Error: Missing --condition."
 
 test_failure "unknown option" \
-       "Error: Unknown option \`--hello\`" \
+       "Error: While parsing argument #1 (\`--hello\`): Unknown option \`--hello\`" \
        --hello
 
 test_failure "missing --action" \
This page took 0.026554 seconds and 4 git commands to generate.