1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

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>
This commit is contained in:
Amitay Isaacs 2018-07-09 15:37:52 +10:00 committed by Martin Schwenke
parent 0361a26e39
commit 29948d7b1e
3 changed files with 34 additions and 24 deletions

View 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;
}

View File

@ -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

View 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);