lttng: mention argument number on unknown action / condition name
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 24 Aug 2021 21:50:32 +0000 (17:50 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 16 Dec 2021 00:16:58 +0000 (19:16 -0500)
When a unrecognized condition or action name is given, the error message
does not contain the part that mentions the argument index, like
argument parsing error messages have:

    $ lttng add-trigger  --action notify  --condition pouet
    Error: Unknown condition name 'pouet'

I think it would be useful to have it, it can help when you have really
long command lines (something possible with add-trigger).  The result
is:

    $ lttng add-trigger  --action notify  --condition pouet
    Error: While parsing argument #4 (`--condition`): Unknown condition name 'pouet'

    $ lttng add-trigger  --action notify  --condition=pouet
    Error: While parsing argument #4 (`--condition=pouet`): Unknown condition name 'pouet'

    $ lttng add-trigger --condition event-rule-matches --action pouet
    Error: While parsing argument #4 (`--action`): Unknown action name: pouet

    $ lttng add-trigger --condition event-rule-matches --action=pouet
    Error: While parsing argument #4 (`--action=pouet`): Unknown action name: pouet

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

index 58041666a3c692812e79f5d2d784636c09c5189f..b1985e95c9100f89fba780ee42552baaffd18b2a 100644 (file)
@@ -1416,7 +1416,8 @@ struct condition_descr condition_descrs[] = {
 
 static
 struct lttng_condition *parse_condition(const char *condition_name, int *argc,
-               const char ***argv, int argc_offset)
+               const char ***argv, int argc_offset, int orig_arg_index,
+               const char *orig_arg)
 {
        int i;
        struct lttng_condition *cond;
@@ -1430,7 +1431,8 @@ struct lttng_condition *parse_condition(const char *condition_name, int *argc,
        }
 
        if (!descr) {
-               ERR("Unknown condition name '%s'", condition_name);
+               ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown condition name '%s'",
+                       orig_arg_index + 1, orig_arg, condition_name);
                goto error;
        }
 
@@ -2097,7 +2099,8 @@ struct action_descr action_descrs[] = {
 
 static
 struct lttng_action *parse_action(const char *action_name, int *argc,
-               const char ***argv, int argc_offset)
+               const char ***argv, int argc_offset, int orig_arg_index,
+               const char *orig_arg)
 {
        int i;
        struct lttng_action *action;
@@ -2111,7 +2114,8 @@ struct lttng_action *parse_action(const char *action_name, int *argc,
        }
 
        if (!descr) {
-               ERR("Unknown action name: %s", action_name);
+               ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown action name: %s",
+                       orig_arg_index + 1, orig_arg, action_name);
                goto error;
        }
 
@@ -2247,7 +2251,8 @@ int cmd_add_trigger(int argc, const char **argv)
                        }
 
                        condition = parse_condition(arg, &my_argc, &my_argv,
-                               argc - my_argc);
+                               argc - my_argc, argc - my_argc - ingested_args,
+                               my_argv[-ingested_args]);
                        if (!condition) {
                                /*
                                 * An error message was already printed by
@@ -2261,7 +2266,8 @@ int cmd_add_trigger(int argc, const char **argv)
                case OPT_ACTION:
                {
                        action = parse_action(arg, &my_argc, &my_argv,
-                               argc - my_argc);
+                               argc - my_argc,  argc - my_argc - ingested_args,
+                               my_argv[-ingested_args]);
                        if (!action) {
                                /*
                                 * An error message was already printed by
index 9ac246f81bd7d659a98bb3e89615c1b2302bbd22..af3e76102c57fe0d17421f448beaf7091bbede82 100644 (file)
@@ -12,8 +12,6 @@
 #include <common/error.h>
 #include <common/string-utils/string-utils.h>
 
-#define WHILE_PARSING_ARG_N_ARG_FMT "While parsing argument #%d (`%s`): "
-
 /*
  * Given argpar error status `status` and error `error`, return a formatted
  * error message describing the error.
index 89c5db3118d1d90f8c7efac24912bdd52885459b..32c60c6dd7f16a85430a9bc99c73e54c07fd5052 100644 (file)
@@ -18,6 +18,8 @@ extern "C" {
 #include <common/argpar/argpar.h>
 #include <common/string-utils/format.h>
 
+#define WHILE_PARSING_ARG_N_ARG_FMT "While parsing argument #%d (`%s`): "
+
 enum parse_next_item_status
 {
        PARSE_NEXT_ITEM_STATUS_OK = 0,
index 62430279505173b766d531f6061fbd8a75d2debc..03f8b42422f4287de5fc1e70f36947a2ca13cadf 100755 (executable)
@@ -23,7 +23,7 @@ TESTDIR="$CURDIR/../../.."
 # shellcheck source=../../../utils/utils.sh
 source "$TESTDIR/utils/utils.sh"
 
-plan_tests 286
+plan_tests 289
 
 FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}"
 
@@ -453,8 +453,11 @@ test_failure "missing args after --condition" \
        "Error: While parsing argument #2 (\`--condition\`): Missing required argument for option \`--condition\`" \
        --condition
 test_failure "unknown --condition" \
-       "Error: Unknown condition name 'zoofest'" \
+       "Error: While parsing argument #2 (\`--condition\`): Unknown condition name 'zoofest'" \
        --condition zoofest
+test_failure "unknown --condition=" \
+       "Error: While parsing argument #2 (\`--condition=zoofest\`): Unknown condition name 'zoofest'" \
+       --condition=zoofest
 
 # `--condition event-rule-matches` failures
 test_failure "missing args after --condition event-rule-matches" \
This page took 0.028827 seconds and 4 git commands to generate.