From b2b24c91fae30edf797ed30e3c45c259aee00853 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Mon, 11 Nov 2019 17:01:43 +1100 Subject: [PATCH] ctdb-common: Add section to group commands in cmdline Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- ctdb/common/cmdline.c | 9 ++++- ctdb/common/cmdline.h | 2 ++ ctdb/common/conf_tool.c | 7 +++- ctdb/common/path_tool.c | 7 +++- ctdb/event/event_tool.c | 1 + ctdb/tests/src/cmdline_test.c | 64 +++++++++++++++++++++++++++++------ ctdb/tests/src/db_test_tool.c | 1 + 7 files changed, 78 insertions(+), 13 deletions(-) diff --git a/ctdb/common/cmdline.c b/ctdb/common/cmdline.c index 9651e418a50..eb5361cf61a 100644 --- a/ctdb/common/cmdline.c +++ b/ctdb/common/cmdline.c @@ -32,6 +32,7 @@ struct cmdline_context { const char *prog; struct poptOption *options; + const char *section; struct cmdline_command *commands; int max_len; poptContext pc; @@ -209,6 +210,7 @@ static int cmdline_context_destructor(struct cmdline_context *cmdline); int cmdline_init(TALLOC_CTX *mem_ctx, const char *prog, struct poptOption *options, + const char *section, struct cmdline_command *commands, struct cmdline_context **result) { @@ -247,6 +249,7 @@ int cmdline_init(TALLOC_CTX *mem_ctx, talloc_free(cmdline); return ret; } + cmdline->section = section; cmdline->commands = commands; cmdline->max_len = max_len; @@ -449,7 +452,11 @@ static void cmdline_usage_full(struct cmdline_context *cmdline) poptSetOtherOptionHelp(cmdline->pc, "[] []"); poptPrintHelp(cmdline->pc, stdout, 0); - printf("\nCommands:\n"); + printf("\n"); + if (cmdline->section != NULL) { + printf("%s ", cmdline->section); + } + printf("Commands:\n"); for (i=0; cmdline->commands[i].name != NULL; i++) { cmdline_usage_command(cmdline, &cmdline->commands[i], true); diff --git a/ctdb/common/cmdline.h b/ctdb/common/cmdline.h index 1e11c66c76e..f8c17940a90 100644 --- a/ctdb/common/cmdline.h +++ b/ctdb/common/cmdline.h @@ -85,6 +85,7 @@ struct cmdline_command { * @param[in] mem_ctx Talloc memory context * @param[in] prog Program name * @param[in] options Command-line options + * @param[in] section Name of section grouping specified commands * @param[in] commands Commands array * @param[out] result New cmdline context * @return 0 on success, errno on failure @@ -94,6 +95,7 @@ struct cmdline_command { int cmdline_init(TALLOC_CTX *mem_ctx, const char *prog, struct poptOption *options, + const char *section, struct cmdline_command *commands, struct cmdline_context **result); diff --git a/ctdb/common/conf_tool.c b/ctdb/common/conf_tool.c index 8e0753eb787..2d0543d643e 100644 --- a/ctdb/common/conf_tool.c +++ b/ctdb/common/conf_tool.c @@ -205,7 +205,12 @@ int conf_tool_init(TALLOC_CTX *mem_ctx, return ENOMEM; } - ret = cmdline_init(ctx, prog, options, conf_commands, &ctx->cmdline); + ret = cmdline_init(ctx, + prog, + options, + NULL, + conf_commands, + &ctx->cmdline); if (ret != 0) { D_ERR("Failed to initialize cmdline, ret=%d\n", ret); talloc_free(ctx); diff --git a/ctdb/common/path_tool.c b/ctdb/common/path_tool.c index a19afa9b0c3..44d29b6b00f 100644 --- a/ctdb/common/path_tool.c +++ b/ctdb/common/path_tool.c @@ -315,7 +315,12 @@ int path_tool_init(TALLOC_CTX *mem_ctx, return ENOMEM; } - ret = cmdline_init(ctx, prog, options, path_commands, &ctx->cmdline); + ret = cmdline_init(ctx, + prog, + options, + NULL, + path_commands, + &ctx->cmdline); if (ret != 0) { D_ERR("Failed to initialize cmdline, ret=%d\n", ret); talloc_free(ctx); diff --git a/ctdb/event/event_tool.c b/ctdb/event/event_tool.c index 9c95e6d9553..46dc25e6c30 100644 --- a/ctdb/event/event_tool.c +++ b/ctdb/event/event_tool.c @@ -699,6 +699,7 @@ int event_tool_init(TALLOC_CTX *mem_ctx, ret = cmdline_init(mem_ctx, prog, options, + NULL, event_commands, &ctx->cmdline); if (ret != 0) { diff --git a/ctdb/tests/src/cmdline_test.c b/ctdb/tests/src/cmdline_test.c index e9cb3e0ce78..98ce65b0881 100644 --- a/ctdb/tests/src/cmdline_test.c +++ b/ctdb/tests/src/cmdline_test.c @@ -51,13 +51,18 @@ static void test1(void) mem_ctx = talloc_new(NULL); assert(mem_ctx != NULL); - ret = cmdline_init(mem_ctx, NULL, NULL, NULL, &cmdline); + ret = cmdline_init(mem_ctx, NULL, NULL, NULL, NULL, &cmdline); assert(ret == EINVAL); - ret = cmdline_init(mem_ctx, "test1", NULL, NULL, &cmdline); + ret = cmdline_init(mem_ctx, "test1", NULL, NULL, NULL, &cmdline); assert(ret == EINVAL); - ret = cmdline_init(mem_ctx, "test1", dummy_options, NULL, &cmdline); + ret = cmdline_init(mem_ctx, + "test1", + dummy_options, + NULL, + NULL, + &cmdline); assert(ret == EINVAL); talloc_free(mem_ctx); @@ -102,19 +107,44 @@ static void test2(void) mem_ctx = talloc_new(NULL); assert(mem_ctx != NULL); - ret = cmdline_init(mem_ctx, "test2", NULL, test2_nofunc, &cmdline); + ret = cmdline_init(mem_ctx, + "test2", + NULL, + NULL, + test2_nofunc, + &cmdline); assert(ret == EINVAL); - ret = cmdline_init(mem_ctx, "test2", NULL, test2_nohelp, &cmdline); + ret = cmdline_init(mem_ctx, + "test2", + NULL, + NULL, + test2_nohelp, + &cmdline); assert(ret == EINVAL); - ret = cmdline_init(mem_ctx, "test2", NULL, test2_long, &cmdline); + ret = cmdline_init(mem_ctx, + "test2", + NULL, + NULL, + test2_long, + &cmdline); assert(ret == EINVAL); - ret = cmdline_init(mem_ctx, "test2", NULL, test2_longhelp, &cmdline); + ret = cmdline_init(mem_ctx, + "test2", + NULL, + NULL, + test2_longhelp, + &cmdline); assert(ret == EINVAL); - ret = cmdline_init(mem_ctx, "test2", NULL, test2_twowords, &cmdline); + ret = cmdline_init(mem_ctx, + "test2", + NULL, + NULL, + test2_twowords, + &cmdline); assert(ret == 0); talloc_free(mem_ctx); @@ -154,6 +184,7 @@ static void test3(void) ret = cmdline_init(mem_ctx, "test3", test3_noname, + NULL, dummy_commands, &cmdline); assert(ret == EINVAL); @@ -161,6 +192,7 @@ static void test3(void) ret = cmdline_init(mem_ctx, "test3", test3_notype, + NULL, dummy_commands, &cmdline); assert(ret == EINVAL); @@ -168,6 +200,7 @@ static void test3(void) ret = cmdline_init(mem_ctx, "test3", test3_noarg, + NULL, dummy_commands, &cmdline); assert(ret == EINVAL); @@ -207,6 +240,7 @@ static void test4(void) ret = cmdline_init(mem_ctx, "test4", test4_options, + NULL, test4_commands, &cmdline); assert(ret == 0); @@ -249,7 +283,12 @@ static void test5(void) mem_ctx = talloc_new(NULL); assert(mem_ctx != NULL); - ret = cmdline_init(mem_ctx, "test5", NULL, action_commands, &cmdline); + ret = cmdline_init(mem_ctx, + "test5", + NULL, + NULL, + action_commands, + &cmdline); assert(ret == 0); ret = cmdline_parse(cmdline, 2, argv1, true); @@ -279,7 +318,12 @@ static void test6(void) mem_ctx = talloc_new(NULL); assert(mem_ctx != NULL); - ret = cmdline_init(mem_ctx, "test6", NULL, action_commands, &cmdline); + ret = cmdline_init(mem_ctx, + "test6", + NULL, + NULL, + action_commands, + &cmdline); assert(ret == 0); ret = cmdline_parse(cmdline, 2, argv1, false); diff --git a/ctdb/tests/src/db_test_tool.c b/ctdb/tests/src/db_test_tool.c index c772a89aed3..eb0ad14fcba 100644 --- a/ctdb/tests/src/db_test_tool.c +++ b/ctdb/tests/src/db_test_tool.c @@ -653,6 +653,7 @@ int db_test_tool_init(TALLOC_CTX *mem_ctx, ret = cmdline_init(mem_ctx, prog, options, + NULL, db_test_commands, &ctx->cmdline); if (ret != 0) {