From add9d7fe4b0f0f856adc4fd390350d807204f70c Mon Sep 17 00:00:00 2001 From: David Sterba Date: Mon, 8 Jun 2015 18:54:04 +0200 Subject: [PATCH] btrfs-progs: print compact help for btrfs Running 'btrfs' without arguments will print complete help that spans a lot of lines and is really helpful. Print only subcommand group names with short descriptions, similar to what 'git' does. Signed-off-by: David Sterba --- btrfs.c | 2 +- commands.h | 1 + help.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/btrfs.c b/btrfs.c index f0fa848b..63df377a 100644 --- a/btrfs.c +++ b/btrfs.c @@ -230,7 +230,7 @@ int main(int argc, char **argv) if (!prefixcmp(argv[0], "--")) argv[0] += 2; } else { - usage_command_group(&btrfs_cmd_group, 0, 0); + usage_command_group_short(&btrfs_cmd_group); exit(1); } } diff --git a/commands.h b/commands.h index bd23340d..f9733719 100644 --- a/commands.h +++ b/commands.h @@ -72,6 +72,7 @@ extern const char * const generic_cmd_help_usage[]; void usage(const char * const *usagestr) __attribute__((noreturn)); void usage_command(const struct cmd_struct *cmd, int full, int err); void usage_command_group(const struct cmd_group *grp, int all, int err); +void usage_command_group_short(const struct cmd_group *grp); void help_unknown_token(const char *arg, const struct cmd_group *grp) __attribute__((noreturn)); void help_ambiguous_token(const char *arg, const struct cmd_group *grp) __attribute__((noreturn)); diff --git a/help.c b/help.c index 56aded3a..a67e3fdf 100644 --- a/help.c +++ b/help.c @@ -160,6 +160,50 @@ static void usage_command_group_internal(const struct cmd_group *grp, int full, } } +void usage_command_group_short(const struct cmd_group *grp) +{ + const char * const *usagestr = grp->usagestr; + FILE *outf = stdout; + const struct cmd_struct *cmd; + + if (usagestr && *usagestr) { + fprintf(outf, "usage: %s\n", *usagestr++); + while (*usagestr) + fprintf(outf, " or: %s\n", *usagestr++); + } + + fputc('\n', outf); + + fprintf(outf, "Command groups:\n"); + for (cmd = grp->commands; cmd->token; cmd++) { + if (cmd->hidden) + continue; + + if (!cmd->next) + continue; + + fprintf(outf, " %-16s %s\n", cmd->token, cmd->next->infostr); + } + + fprintf(outf, "\nCommands:\n"); + for (cmd = grp->commands; cmd->token; cmd++) { + if (cmd->hidden) + continue; + + if (cmd->next) + continue; + + fprintf(outf, " %-16s %s\n", cmd->token, cmd->usagestr[1]); + } + + fputc('\n', outf); + fprintf(stderr, "For an overview of a given command use 'btrfs command --help'\n"); + fprintf(stderr, "or 'btrfs [command...] --help --full' to print all available options.\n"); + fprintf(stderr, "Any command name can be shortened as far as it stays unambiguous,\n"); + fprintf(stderr, "however it is recommended to use full command names in scripts.\n"); + fprintf(stderr, "All command groups have their manual page named 'btrfs-'.\n"); +} + void usage_command_group(const struct cmd_group *grp, int full, int err) { const char * const *usagestr = grp->usagestr;