cli: support `help <command>` Support `help <command>` by re-invoking the command itself with the `--help` argument. This allows us to keep the help logic with the commands itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
diff --git a/src/cli/README.md b/src/cli/README.md
index 26f11d9..3087c39 100644
--- a/src/cli/README.md
+++ b/src/cli/README.md
@@ -20,3 +20,7 @@ A git-compatible command-line interface that uses libgit2.
entrypoint and a brief description that can be printed in `git help`.
This is done because commands are linked into the main cli.
+3. Commands should accept a `--help` option that displays their help
+ information. This will be shown when a user runs `<command> --help` and
+ when a user runs `help <command>`.
+
diff --git a/src/cli/cmd_help.c b/src/cli/cmd_help.c
index d2ff5d4..7ee9822 100644
--- a/src/cli/cmd_help.c
+++ b/src/cli/cmd_help.c
@@ -45,7 +45,7 @@ static int print_commands(void)
printf("These are the %s commands available:\n\n", PROGRAM_NAME);
for (cmd = cli_cmds; cmd->name; cmd++)
- printf(" %-8s %s\n", cmd->name, cmd->desc);
+ printf(" %-11s %s\n", cmd->name, cmd->desc);
printf("\nSee '%s help <command>' for more information on a specific command.\n", PROGRAM_NAME);
@@ -54,6 +54,8 @@ static int print_commands(void)
int cmd_help(int argc, char **argv)
{
+ char *fake_args[2];
+ const cli_cmd_spec *cmd;
cli_opt invalid_opt;
if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
@@ -67,11 +69,18 @@ int cmd_help(int argc, char **argv)
if (!command)
return print_commands();
- /* If the user asks for help with the help command */
- if (strcmp(command, "help") == 0)
- return print_help();
+ /*
+ * If we were asked for help for a command (eg, `help <command>`),
+ * delegate back to that command's `--help` option. This lets
+ * commands own their help. Emulate the command-line arguments
+ * that would invoke `<command> --help` and invoke that command.
+ */
+ fake_args[0] = command;
+ fake_args[1] = "--help";
+
+ if ((cmd = cli_cmd_spec_byname(command)) == NULL)
+ return cli_error("'%s' is not a %s command. See '%s help'.",
+ command, PROGRAM_NAME, PROGRAM_NAME);
- fprintf(stderr, "%s: '%s' is not a %s command. See '%s help'.\n",
- PROGRAM_NAME, command, PROGRAM_NAME, PROGRAM_NAME);
- return CLI_EXIT_ERROR;
+ return cmd->fn(2, fake_args);
}
diff --git a/src/cli/main.c b/src/cli/main.c
index c68c349..d961f65 100644
--- a/src/cli/main.c
+++ b/src/cli/main.c
@@ -37,6 +37,8 @@ int main(int argc, char **argv)
const cli_cmd_spec *cmd;
cli_opt_parser optparser;
cli_opt opt;
+ char *help_args[3] = { NULL };
+ int help_args_len;
int args_len = 0;
int ret = 0;
@@ -72,10 +74,19 @@ int main(int argc, char **argv)
goto done;
}
- /* If there was no command, we want to invoke "help" */
+ /*
+ * If `--help <command>` is specified, delegate to that command's
+ * `--help` option. If no command is specified, run the `help`
+ * command. Do this by updating the args to emulate that behavior.
+ */
if (!command || show_help) {
- cli_opt_usage_fprint(stdout, PROGRAM_NAME, NULL, cli_common_opts);
- goto done;
+ help_args[0] = command ? (char *)command : "help";
+ help_args[1] = command ? "--help" : NULL;
+ help_args_len = command ? 2 : 1;
+
+ command = help_args[0];
+ args = help_args;
+ args_len = help_args_len;
}
if ((cmd = cli_cmd_spec_byname(command)) == NULL) {