From 29948d7b1eb1337c7e3d91dc53bb3e1e42de6dd3 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Mon, 9 Jul 2018 15:37:52 +1000 Subject: [PATCH] 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 Reviewed-by: Martin Schwenke --- ctdb/common/cmdline.c | 34 +++++++++++------------ ctdb/tests/UNIT/cunit/cmdline_test_001.sh | 16 +++++++++++ ctdb/tests/src/cmdline_test.c | 8 ++---- 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 @@ -43,6 +43,22 @@ unit_test cmdline_test 4 ok <] [] +Help Options: + -h, --help Show this help message + +Commands: + action one action one help + action two action two help +Usage: test5 [] [] + +Help Options: + -h, --help Show this help message + +Commands: + action one action one help + action two action two help +Usage: test5 [] [] + Help Options: -h, --help Show this help message 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); -- 2.47.3