]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-common: Add section to group commands in cmdline
authorAmitay Isaacs <amitay@gmail.com>
Mon, 11 Nov 2019 06:01:43 +0000 (17:01 +1100)
committerMartin Schwenke <martins@samba.org>
Thu, 14 Nov 2019 10:38:34 +0000 (10:38 +0000)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/common/cmdline.c
ctdb/common/cmdline.h
ctdb/common/conf_tool.c
ctdb/common/path_tool.c
ctdb/event/event_tool.c
ctdb/tests/src/cmdline_test.c
ctdb/tests/src/db_test_tool.c

index 9651e418a506b068633bd140aaa5252d6a127621..eb5361cf61abc30aa0845886b7735e5939cefad5 100644 (file)
@@ -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, "[<options>] <command> [<args>]");
        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);
 
index 1e11c66c76e66564b6bdd303923fbd3ce4c68d86..f8c17940a90cd5460443d0859d2ca17827b13fbd 100644 (file)
@@ -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);
 
index 8e0753eb787f62b4a084da3d996d1e9171b87d62..2d0543d643e3452c350d0fca732af378848693b3 100644 (file)
@@ -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);
index a19afa9b0c34711589bb1576e9de15edef9e6cfd..44d29b6b00fd1af1551c941301c47be2a557b982 100644 (file)
@@ -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);
index 9c95e6d955343d510045a5bdd239c038e9d68d56..46dc25e6c306362dff54b585fb49578e3216c998 100644 (file)
@@ -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) {
index e9cb3e0ce7817136dce178b7753e5d1af1162660..98ce65b0881bd3175aac3a182d69ecc35488b285 100644 (file)
@@ -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);
index c772a89aed3eed9b9e9400dccd2ff98746ba179e..eb0ad14fcbac869de8be479b981058a3895d7911 100644 (file)
@@ -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) {