From 4465171593fdbe8d74445dd2d6526ef2cdea4d6e Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 30 Mar 2022 12:02:35 +0200 Subject: [PATCH] MINOR: cli: alphanumerically sort the dump of supported commands Like for previous keyword classes, we're sorting the output. But this time as it's not trivial to do it with multiple words, instead we're proceeding like the help command, we sort them on their usage message when present, and fall back to the first word of the command when there is no usage message (e.g. "help" command). --- src/cli.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/cli.c b/src/cli.c index 9f4b8df5a1..25d28f14d1 100644 --- a/src/cli.c +++ b/src/cli.c @@ -372,24 +372,37 @@ void cli_register_kw(struct cli_kw_list *kw_list) void cli_list_keywords(void) { struct cli_kw_list *kw_list; - struct cli_kw *kw; + struct cli_kw *kwp, *kwn, *kw; int idx; - list_for_each_entry(kw_list, &cli_keywords.list, list) { - for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) { - for (idx = 0; kw->str_kw[idx]; idx++) { - printf("%s ", kw->str_kw[idx]); + for (kwn = kwp = NULL;; kwp = kwn) { + list_for_each_entry(kw_list, &cli_keywords.list, list) { + /* note: we sort based on the usage message when available, + * otherwise we fall back to the first keyword. + */ + for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) { + if (strordered(kwp ? kwp->usage ? kwp->usage : kwp->str_kw[0] : NULL, + kw->usage ? kw->usage : kw->str_kw[0], + kwn != kwp ? kwn->usage ? kwn->usage : kwn->str_kw[0] : NULL)) + kwn = kw; } - if (kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) - printf("[MASTER] "); - if (!(kw->level & ACCESS_MASTER_ONLY)) - printf("[WORKER] "); - if (kw->level & ACCESS_EXPERT) - printf("[EXPERT] "); - if (kw->level & ACCESS_EXPERIMENTAL) - printf("[EXPERIM] "); - printf("\n"); } + + if (kwn == kwp) + break; + + for (idx = 0; kwn->str_kw[idx]; idx++) { + printf("%s ", kwn->str_kw[idx]); + } + if (kwn->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) + printf("[MASTER] "); + if (!(kwn->level & ACCESS_MASTER_ONLY)) + printf("[WORKER] "); + if (kwn->level & ACCESS_EXPERT) + printf("[EXPERT] "); + if (kwn->level & ACCESS_EXPERIMENTAL) + printf("[EXPERIM] "); + printf("\n"); } } -- 2.47.3