lttng add-trigger: parse condition / action name as option arguments
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 9 Apr 2021 17:14:44 +0000 (13:14 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 22 Apr 2021 02:16:58 +0000 (22:16 -0400)
Change the `add-trigger` command's --condition and --action options to
take the condition name and action name as option arguments.  Currently,
when we see the --condition option, we go into the parse_condition
function and expect the first argument to be the condition name (same
idea for the action).  Since the condition/action name is mandatory
after --condition/--action, it is simpler to say that --condition and
--action require an argument.

This will let the user do:

    --condition=on-event

in addition to the current syntax:

    --condition on-event

The code is a bit cleaner as a result.

Change-Id: Ic7ea97c2657e5de6ae18563694ad22bbffbb2aa8
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.c
tests/regression/tools/trigger/test_add_trigger_cli

index 10a7944df8e82b4f2f4151917c1ae6c8ed5ca16c..fdfbecfaa0afbb8e23d65747fce8d5c61db02c40 100644 (file)
@@ -1293,23 +1293,13 @@ struct condition_descr condition_descrs[] = {
 };
 
 static
-struct lttng_condition *parse_condition(int *argc, const char ***argv)
+struct lttng_condition *parse_condition(const char *condition_name, int *argc,
+               const char ***argv)
 {
        int i;
        struct lttng_condition *cond;
-       const char *condition_name;
        const struct condition_descr *descr = NULL;
 
-       if (*argc == 0) {
-               ERR("Missing condition name.");
-               goto error;
-       }
-
-       condition_name = (*argv)[0];
-
-       (*argc)--;
-       (*argv)++;
-
        for (i = 0; i < ARRAY_SIZE(condition_descrs); i++) {
                if (strcmp(condition_name, condition_descrs[i].name) == 0) {
                        descr = &condition_descrs[i];
@@ -1999,23 +1989,12 @@ struct action_descr action_descrs[] = {
 };
 
 static
-struct lttng_action *parse_action(int *argc, const char ***argv)
+struct lttng_action *parse_action(const char *action_name, int *argc, const char ***argv)
 {
        int i;
        struct lttng_action *action;
-       const char *action_name;
        const struct action_descr *descr = NULL;
 
-       if (*argc == 0) {
-               ERR("Missing action name.");
-               goto error;
-       }
-
-       action_name = (*argv)[0];
-
-       (*argc)--;
-       (*argv)++;
-
        for (i = 0; i < ARRAY_SIZE(action_descrs); i++) {
                if (strcmp(action_name, action_descrs[i].name) == 0) {
                        descr = &action_descrs[i];
@@ -2045,8 +2024,8 @@ static const
 struct argpar_opt_descr add_trigger_options[] = {
        { OPT_HELP, 'h', "help", false },
        { OPT_LIST_OPTIONS, '\0', "list-options", false },
-       { OPT_CONDITION, '\0', "condition", false },
-       { OPT_ACTION, '\0', "action", false },
+       { OPT_CONDITION, '\0', "condition", true },
+       { OPT_ACTION, '\0', "action", true },
        { OPT_ID, '\0', "id", true },
        { OPT_USER_ID, '\0', "user-id", true },
        ARGPAR_OPT_DESCR_SENTINEL,
@@ -2139,7 +2118,7 @@ int cmd_add_trigger(int argc, const char **argv)
                                goto error;
                        }
 
-                       condition = parse_condition(&my_argc, &my_argv);
+                       condition = parse_condition(item_opt->arg, &my_argc, &my_argv);
                        if (!condition) {
                                /*
                                 * An error message was already printed by
@@ -2152,7 +2131,7 @@ int cmd_add_trigger(int argc, const char **argv)
                }
                case OPT_ACTION:
                {
-                       action = parse_action(&my_argc, &my_argv);
+                       action = parse_action(item_opt->arg, &my_argc, &my_argv);
                        if (!action) {
                                /*
                                 * An error message was already printed by
index 974938068707c48243ddf2977de5b65ceef0782b..fd962c04bae76421336e8bc43bee441fbb1111b6 100755 (executable)
@@ -290,7 +290,7 @@ test_failure "invalid argument to --rate-policy: unknown policy type" \
 
 # `--condition` failures
 test_failure "missing args after --condition" \
-       "Error: Missing condition name." \
+       "Error: While parsing argument #1 (\`--condition\`): Missing required argument for option \`--condition\`" \
        --condition
 test_failure "unknown --condition" \
        "Error: Unknown condition name 'zoofest'" \
@@ -380,7 +380,7 @@ test_failure "--condition on-event --capture: missing colon in app-specific cont
 
 # `--action` failures
 test_failure "missing args after --action" \
-       "Error: Missing action name." \
+       "Error: While parsing argument #1 (\`--action\`): Missing required argument for option \`--action\`" \
        --condition on-event -u -a \
        --action
 
This page took 0.027542 seconds and 4 git commands to generate.