]> git.proxmox.com Git - libgit2.git/blob - src/cli/cmd_help.c
New upstream version 1.5.0+ds
[libgit2.git] / src / cli / cmd_help.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include <stdio.h>
9 #include <git2.h>
10 #include "cli.h"
11 #include "cmd.h"
12
13 #define COMMAND_NAME "help"
14
15 static char *command;
16 static int show_help;
17
18 static const cli_opt_spec opts[] = {
19 { CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1,
20 CLI_OPT_USAGE_HIDDEN, NULL, "display help about the help command" },
21 { CLI_OPT_TYPE_ARG, "command", 0, &command, 0,
22 CLI_OPT_USAGE_DEFAULT, "command", "the command to show help for" },
23 { 0 },
24 };
25
26 static int print_help(void)
27 {
28 cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts);
29 printf("\n");
30
31 printf("Display help information about %s. If a command is specified, help\n", PROGRAM_NAME);
32 printf("about that command will be shown. Otherwise, general information about\n");
33 printf("%s will be shown, including the commands available.\n", PROGRAM_NAME);
34
35 return 0;
36 }
37
38 static int print_commands(void)
39 {
40 const cli_cmd_spec *cmd;
41
42 cli_opt_usage_fprint(stdout, PROGRAM_NAME, NULL, cli_common_opts);
43 printf("\n");
44
45 printf("These are the %s commands available:\n\n", PROGRAM_NAME);
46
47 for (cmd = cli_cmds; cmd->name; cmd++)
48 printf(" %-11s %s\n", cmd->name, cmd->desc);
49
50 printf("\nSee '%s help <command>' for more information on a specific command.\n", PROGRAM_NAME);
51
52 return 0;
53 }
54
55 int cmd_help(int argc, char **argv)
56 {
57 char *fake_args[2];
58 const cli_cmd_spec *cmd;
59 cli_opt invalid_opt;
60
61 if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
62 return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
63
64 /* Show the meta-help */
65 if (show_help)
66 return print_help();
67
68 /* We were not asked to show help for a specific command. */
69 if (!command)
70 return print_commands();
71
72 /*
73 * If we were asked for help for a command (eg, `help <command>`),
74 * delegate back to that command's `--help` option. This lets
75 * commands own their help. Emulate the command-line arguments
76 * that would invoke `<command> --help` and invoke that command.
77 */
78 fake_args[0] = command;
79 fake_args[1] = "--help";
80
81 if ((cmd = cli_cmd_spec_byname(command)) == NULL)
82 return cli_error("'%s' is not a %s command. See '%s help'.",
83 command, PROGRAM_NAME, PROGRAM_NAME);
84
85 return cmd->fn(2, fake_args);
86 }