From: Abhijeet Rastogi Date: Thu, 17 Nov 2022 12:42:38 +0000 (-0800) Subject: MINOR: cli: print parsed command when not found X-Git-Tag: v2.7-dev10~51 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c8601507b27db9df3c6e406c6d90c473c6976619;p=thirdparty%2Fhaproxy.git MINOR: cli: print parsed command when not found It is useful because when we're passing data to runtime API, specially via code, we can mistakenly send newlines leading to some lines being wrongly interpretted as commands. This is analogous to how it's done in a shell, example bash: $ not_found arg1 bash: not_found: command not found... $ Real world example: Following the official docs to add a cert: $ echo -e "set ssl cert ./cert.pem <<\n$(cat ./cert.pem)\n" | socat stdio tcp4-connect:127.0.0.1:9999 Note, how the payload is sent via '<<\n$(cat ./cert.pem)\n'. If cert.pem contains a newline between various PEM blocks, which is valid, the above command would generate a flood of 'Unknown command' messages for every line sent after the first newline. As a new user, this detail is not clearly visible as socket API doesn't say what exactly what was 'unknown' about it. The cli interface should be obvious around guiding user on "what do do next". This commit changes that by printing the parsed cmd in output like 'Unknown command: ""' so the user gets clear "next steps", like bash, regarding what indeed was the wrong command that HAproxy couldn't interpret. Previously: $ echo -e "show version\nhelpp"| socat ./haproxy.sock - | head -n4 2.7-dev6 Unknown command, but maybe one of the following ones is a better match: add map [@] : add a map entry (payload supported instead of key/val) Now: $ echo -e "show version\nhelpp"| socat ./haproxy.sock - | head -n4 2.7-dev8-737bb7-156 Unknown command: 'helpp', but maybe one of the following ones is a better match: add map [@] : add a map entry (payload supported instead of key/val) --- diff --git a/src/cli.c b/src/cli.c index 4e7ae14963..2b3ea3a3c9 100644 --- a/src/cli.c +++ b/src/cli.c @@ -170,10 +170,17 @@ static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args) chunk_reset(tmp); if (ishelp) // this is the help message. chunk_strcat(tmp, "The following commands are valid at this level:\n"); - else if (!length && (!args || !*args || !**args)) // no match - chunk_strcat(tmp, "Unknown command. Please enter one of the following commands only:\n"); - else // partial match - chunk_strcat(tmp, "Unknown command, but maybe one of the following ones is a better match:\n"); + else { + chunk_strcat(tmp, "Unknown command: '"); + if (args && *args) + chunk_strcat(tmp, *args); + chunk_strcat(tmp, "'"); + + if (!length && (!args || !*args || !**args)) // no match + chunk_strcat(tmp, ". Please enter one of the following commands only:\n"); + else // partial match + chunk_strcat(tmp, ", but maybe one of the following ones is a better match:\n"); + } for (idx = 0; idx < CLI_MAX_MATCHES; idx++) { matches[idx].kw = NULL;