summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmitay Isaacs <amitay@gmail.com>2018-07-09 15:37:52 +1000
committerMartin Schwenke <martins@samba.org>2019-11-14 10:38:34 +0000
commit29948d7b1eb1337c7e3d91dc53bb3e1e42de6dd3 (patch)
tree93365cdee0cdd79c725485e4287fa86d8a61cf72
parent0361a26e395723296899c3d48cff86d532372710 (diff)
downloadsamba-29948d7b1eb1337c7e3d91dc53bb3e1e42de6dd3.tar.gz
ctdb-common: Generate usage message from cmdline_parse()
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>
-rw-r--r--ctdb/common/cmdline.c34
-rwxr-xr-xctdb/tests/UNIT/cunit/cmdline_test_001.sh16
-rw-r--r--ctdb/tests/src/cmdline_test.c8
3 files changed, 34 insertions, 24 deletions
diff --git a/ctdb/common/cmdline.c b/ctdb/common/cmdline.c
index f3768c068fe..9651e418a50 100644
--- a/ctdb/common/cmdline.c
+++ b/ctdb/common/cmdline.c
@@ -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;
}
diff --git a/ctdb/tests/UNIT/cunit/cmdline_test_001.sh b/ctdb/tests/UNIT/cunit/cmdline_test_001.sh
index 527b1143e7e..72089519f5e 100755
--- a/ctdb/tests/UNIT/cunit/cmdline_test_001.sh
+++ b/ctdb/tests/UNIT/cunit/cmdline_test_001.sh
@@ -49,6 +49,22 @@ Help Options:
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
+
+Commands:
+ action one action one help
+ action two action two help
EOF
unit_test cmdline_test 5
diff --git a/ctdb/tests/src/cmdline_test.c b/ctdb/tests/src/cmdline_test.c
index 8f5b6028fe4..e9cb3e0ce78 100644
--- a/ctdb/tests/src/cmdline_test.c
+++ b/ctdb/tests/src/cmdline_test.c
@@ -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);