From adaf1f7ea38798d4ddbb3cff6513188fd6e98c9e Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Mon, 22 Apr 2024 17:40:53 +0800 Subject: [PATCH] shared/verbs: show list of verbs when missing Replaces #32062 As discussed in #32062, making 'help' the default verb is not very appealing for two reasons: 1) If the verb is missing, showing a help which is pages long isn't really helpful to locate the problem. (https://github.com/systemd/systemd/pull/32062#issuecomment-2064997158) 2) We want to reserve the right to set default verbs to be more useful ones, instead of help. E.g. 'busctl' lists all bus peers by default. So, when there are more than 2 verbs, let's instead add the list of available verbs to the "Command verb required" message, that serves as a hint. That way we try to be friendlier to users, but still make the problem obvious. --- src/shared/verbs.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/shared/verbs.c b/src/shared/verbs.c index 54817252b5b..7e7ac2a2554 100644 --- a/src/shared/verbs.c +++ b/src/shared/verbs.c @@ -143,6 +143,17 @@ int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command verb '%s'.", name); } + _cleanup_free_ char *verb_list = NULL; + size_t i; + + for (i = 0; verbs[i].dispatch; i++) + if (!strextend_with_separator(&verb_list, ", ", verbs[i].verb)) + return log_oom(); + + if (i > 2) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Command verb required (one of %s).", verb_list); + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Command verb required."); }