From e776cf47dcf2cd7ab154392f30eb015f6a6f2866 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 24 Aug 2021 17:50:32 -0400 Subject: [PATCH] lttng: mention argument number on unknown action / condition name MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Jérémie Galarneau --- src/bin/lttng/commands/add_trigger.cpp | 18 ++++++++++++------ src/common/argpar-utils/argpar-utils.c | 2 -- src/common/argpar-utils/argpar-utils.h | 2 ++ .../tools/trigger/test_add_trigger_cli | 7 +++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/bin/lttng/commands/add_trigger.cpp b/src/bin/lttng/commands/add_trigger.cpp index 58041666a..b1985e95c 100644 --- a/src/bin/lttng/commands/add_trigger.cpp +++ b/src/bin/lttng/commands/add_trigger.cpp @@ -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 diff --git a/src/common/argpar-utils/argpar-utils.c b/src/common/argpar-utils/argpar-utils.c index 9ac246f81..af3e76102 100644 --- a/src/common/argpar-utils/argpar-utils.c +++ b/src/common/argpar-utils/argpar-utils.c @@ -12,8 +12,6 @@ #include #include -#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. diff --git a/src/common/argpar-utils/argpar-utils.h b/src/common/argpar-utils/argpar-utils.h index 89c5db311..32c60c6dd 100644 --- a/src/common/argpar-utils/argpar-utils.h +++ b/src/common/argpar-utils/argpar-utils.h @@ -18,6 +18,8 @@ extern "C" { #include #include +#define WHILE_PARSING_ARG_N_ARG_FMT "While parsing argument #%d (`%s`): " + enum parse_next_item_status { PARSE_NEXT_ITEM_STATUS_OK = 0, diff --git a/tests/regression/tools/trigger/test_add_trigger_cli b/tests/regression/tools/trigger/test_add_trigger_cli index 624302795..03f8b4242 100755 --- a/tests/regression/tools/trigger/test_add_trigger_cli +++ b/tests/regression/tools/trigger/test_add_trigger_cli @@ -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" \ -- 2.34.1