Commit f8e7d8fd by Edward Thomson

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.
parent c6dd82d9
......@@ -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>`.
......@@ -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);
}
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment