1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

ctdb-common: Add section to group commands in cmdline

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Amitay Isaacs 2019-11-11 17:01:43 +11:00 committed by Martin Schwenke
parent 29948d7b1e
commit b2b24c91fa
7 changed files with 78 additions and 13 deletions

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

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

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

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

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

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

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