[sheepdog] [PATCH] dog: let usage printing adopt nested subcommands
Hitoshi Mitake
mitake.hitoshi at gmail.com
Thu Nov 14 15:58:00 CET 2013
At Thu, 14 Nov 2013 23:52:03 +0900,
Hitoshi Mitake wrote:
>
> Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
> ---
> dog/common.c | 49 ++++++++++++++++++++++++++++---------------------
> dog/dog.c | 21 ++++++++++++++-------
> dog/dog.h | 7 ++++++-
> 3 files changed, 48 insertions(+), 29 deletions(-)
>
> diff --git a/dog/common.c b/dog/common.c
> index 8c9b326..01d9f54 100644
> --- a/dog/common.c
> +++ b/dog/common.c
> @@ -224,36 +224,43 @@ int send_light_req(const struct node_id *nid, struct sd_req *hdr)
> return 0;
> }
>
> +int subcmd_depth = -1;
> +struct subcommand *subcmd_stack[MAX_SUBCMD_DEPTH];
> +
> int do_generic_subcommand(struct subcommand *sub, int argc, char **argv)
> {
> int i, ret;
> - static int depth = -1;
>
> - depth++;
> + subcmd_stack[++subcmd_depth] = sub;
do_generic_subcommand() should check the depth of subcommands here,
sorry. I'll send v2 soon.
Thanks,
Hitoshi
> +
> for (i = 0; sub[i].name; i++) {
> - if (!strcmp(sub[i].name, argv[optind])) {
> - unsigned long flags = sub[i].flags;
> -
> - if (flags & CMD_NEED_NODELIST) {
> - ret = update_node_list(SD_MAX_NODES);
> - if (ret < 0) {
> - sd_err("Failed to get node list");
> - exit(EXIT_SYSFAIL);
> - }
> - }
> + unsigned long flags;
> +
> + if (strcmp(sub[i].name, argv[optind]))
> + continue;
>
> - if (flags & CMD_NEED_ARG && argc < 5 + depth)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> - optind++;
> - ret = sub[i].fn(argc, argv);
> - if (ret == EXIT_USAGE)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> - return ret;
> + flags = sub[i].flags;
> +
> + if (flags & CMD_NEED_NODELIST) {
> + ret = update_node_list(SD_MAX_NODES);
> + if (ret < 0) {
> + sd_err("Failed to get node list");
> + exit(EXIT_SYSFAIL);
> + }
> }
> +
> + if (flags & CMD_NEED_ARG && argc < 5 + subcmd_depth)
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, true);
> + optind++;
> + ret = sub[i].fn(argc, argv);
> + if (ret == EXIT_USAGE)
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, true);
> + return ret;
> }
>
> - depth--;
> - subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
> + subcommand_usage(argv[1], argv[2], EXIT_FAILURE, true);
> + subcmd_depth--;
> +
> return EXIT_FAILURE;
> }
>
> diff --git a/dog/dog.c b/dog/dog.c
> index 16298ad..3db90f6 100644
> --- a/dog/dog.c
> +++ b/dog/dog.c
> @@ -254,7 +254,7 @@ static void usage(const struct command *commands, int status)
> exit(status);
> }
>
> -void subcommand_usage(char *cmd, char *subcmd, int status)
> +void subcommand_usage(char *cmd, char *subcmd, int status, bool print_stack)
> {
> int i, n, len = strlen(command_opts);
> const struct sd_option *sd_opt;
> @@ -263,9 +263,16 @@ void subcommand_usage(char *cmd, char *subcmd, int status)
>
> printf("Usage: %s %s %s", program_name, cmd, subcmd);
>
> - /* Show subcmd's subcommands if necessary */
> - sub = find_subcmd(cmd, subcmd);
> - subsub = sub->sub;
> + if (print_stack) {
> + for (i = 0; i < subcmd_depth + 1; i++)
> + printf(" %s", subcmd_stack[i]->name);
> +
> + subsub = subcmd_stack[i - 1]->sub;
> + } else {
> + sub = find_subcmd(cmd, subcmd);
> + subsub = sub->sub;
> + }
> +
> if (subsub) {
> n = 0;
> while (subsub[n].name)
> @@ -397,7 +404,7 @@ int main(int argc, char **argv)
> verbose = true;
> break;
> case 'h':
> - subcommand_usage(argv[1], argv[2], EXIT_SUCCESS);
> + subcommand_usage(argv[1], argv[2], EXIT_SUCCESS, false);
> break;
> case '?':
> usage(commands, EXIT_USAGE);
> @@ -423,7 +430,7 @@ int main(int argc, char **argv)
> }
>
> if (flags & CMD_NEED_ARG && argc == optind)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, false);
>
> if (init_event(EPOLL_SIZE) < 0)
> exit(EXIT_SYSFAIL);
> @@ -440,6 +447,6 @@ int main(int argc, char **argv)
>
> ret = command_fn(argc, argv);
> if (ret == EXIT_USAGE)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, false);
> return ret;
> }
> diff --git a/dog/dog.h b/dog/dog.h
> index 28c36a1..9b210ca 100644
> --- a/dog/dog.h
> +++ b/dog/dog.h
> @@ -47,7 +47,12 @@ struct subcommand {
> int (*fn)(int, char **);
> const struct sd_option *options;
> };
> -void subcommand_usage(char *cmd, char *subcmd, int status);
> +void subcommand_usage(char *cmd, char *subcmd, int status, bool print_stack);
> +
> +#define MAX_SUBCMD_DEPTH 8
> +
> +extern int subcmd_depth;
> +extern struct subcommand *subcmd_stack[MAX_SUBCMD_DEPTH];
>
> extern struct node_id sd_nid;
> extern bool highlight;
> --
> 1.8.1.2
>
More information about the sheepdog
mailing list