ctdb-common: Generate usage message from cmdline_parse()
authorAmitay Isaacs <amitay@gmail.com>
Mon, 9 Jul 2018 05:37:52 +0000 (15:37 +1000)
committerMartin Schwenke <martins@samba.org>
Thu, 14 Nov 2019 10:38:34 +0000 (10:38 +0000)
If any of the option parsing or command parsing fails, generate usage
message.

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/common/cmdline.c
ctdb/tests/UNIT/cunit/cmdline_test_001.sh
ctdb/tests/src/cmdline_test.c

index f3768c068fecac94f89837a80b054f87886d2d3e..9651e418a506b068633bd140aaa5252d6a127621 100644 (file)
@@ -387,6 +387,7 @@ int cmdline_parse(struct cmdline_context *cmdline,
        int ret;
 
        if (argc < 2) {
+               cmdline_usage(cmdline, NULL);
                return EINVAL;
        }
 
@@ -395,6 +396,7 @@ int cmdline_parse(struct cmdline_context *cmdline,
        if (parse_options) {
                ret = cmdline_parse_options(cmdline, argc, argv);
                if (ret != 0) {
+                       cmdline_usage(cmdline, NULL);
                        return ret;
                }
        } else {
@@ -403,11 +405,22 @@ int cmdline_parse(struct cmdline_context *cmdline,
        }
 
        ret = cmdline_match(cmdline);
-       if (!cmdline_show_help && ret != 0) {
-               return ret;
+
+       if (ret != 0 || cmdline_show_help) {
+               const char *name = NULL;
+
+               if (cmdline->match_cmd != NULL) {
+                       name = cmdline->match_cmd->name;
+               }
+
+               cmdline_usage(cmdline, name);
+
+               if (cmdline_show_help) {
+                       ret = EAGAIN;
+               }
        }
 
-       return 0;
+       return ret;
 }
 
 static void cmdline_usage_command(struct cmdline_context *cmdline,
@@ -480,21 +493,6 @@ int cmdline_run(struct cmdline_context *cmdline,
        TALLOC_CTX *tmp_ctx;
        int ret;
 
-       if (cmdline_show_help) {
-               const char *name = NULL;
-
-               if (cmd != NULL) {
-                       name = cmdline->match_cmd->name;
-               }
-
-               cmdline_usage(cmdline, name);
-
-               if (result != NULL) {
-                       *result = 0;
-               }
-               return EAGAIN;
-       }
-
        if (cmd == NULL) {
                return ENOENT;
        }
index 527b1143e7e02d7fbadb71842e76e988c8b0f523..72089519f5e99665277cda3dedbd120d191957e9 100755 (executable)
@@ -43,6 +43,22 @@ unit_test cmdline_test 4
 ok <<EOF
 Usage: test5 [<options>] <command> [<args>]
 
+Help Options:
+  -h, --help     Show this help message
+
+Commands:
+  action one      action one help
+  action two      action two help
+Usage: test5 [<options>] <command> [<args>]
+
+Help Options:
+  -h, --help     Show this help message
+
+Commands:
+  action one      action one help
+  action two      action two help
+Usage: test5 [<options>] <command> [<args>]
+
 Help Options:
   -h, --help     Show this help message
 
index 8f5b6028fe43f2e56b2d33a6cd7e65111f7add17..e9cb3e0ce7817136dce178b7753e5d1af1162660 100644 (file)
@@ -244,7 +244,7 @@ static void test5(void)
        const char *argv2[] = { "test5", "action" };
        const char *argv3[] = { "test5", "action", "--help" };
        const char *argv4[] = { "test5", "action", "one" };
-       int ret, result;
+       int ret;
 
        mem_ctx = talloc_new(NULL);
        assert(mem_ctx != NULL);
@@ -253,17 +253,13 @@ static void test5(void)
        assert(ret == 0);
 
        ret = cmdline_parse(cmdline, 2, argv1, true);
-       assert(ret == 0);
-
-       ret = cmdline_run(cmdline, NULL, &result);
        assert(ret == EAGAIN);
-       assert(result == 0);
 
        ret = cmdline_parse(cmdline, 2, argv2, true);
        assert(ret == ENOENT);
 
        ret = cmdline_parse(cmdline, 3, argv3, true);
-       assert(ret == 0);
+       assert(ret == EAGAIN);
 
        ret = cmdline_parse(cmdline, 3, argv4, true);
        assert(ret == 0);