]>
git.proxmox.com Git - libgit2.git/blob - src/cli/cmd_help.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
13 #define COMMAND_NAME "help"
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" },
26 static int print_help(void)
28 cli_opt_usage_fprint(stdout
, PROGRAM_NAME
, COMMAND_NAME
, opts
);
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
);
38 static int print_commands(void)
40 const cli_cmd_spec
*cmd
;
42 cli_opt_usage_fprint(stdout
, PROGRAM_NAME
, NULL
, cli_common_opts
);
45 printf("These are the %s commands available:\n\n", PROGRAM_NAME
);
47 for (cmd
= cli_cmds
; cmd
->name
; cmd
++)
48 printf(" %-11s %s\n", cmd
->name
, cmd
->desc
);
50 printf("\nSee '%s help <command>' for more information on a specific command.\n", PROGRAM_NAME
);
55 int cmd_help(int argc
, char **argv
)
58 const cli_cmd_spec
*cmd
;
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
);
64 /* Show the meta-help */
68 /* We were not asked to show help for a specific command. */
70 return print_commands();
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.
78 fake_args
[0] = command
;
79 fake_args
[1] = "--help";
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
);
85 return cmd
->fn(2, fake_args
);