help: use parseopt
This patch converts cmd_help to use parseopt, along with a few style cleanups, including: - enum constants are now ALL_CAPS - parse_help_format returns an enum value rather than setting a global as a side effect Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Feb 24, 2008 at 17:16 UTC
41eb33bd0cbecf1b441ada91ab186ee49fb086cc
1 file changed
+59
-64
help.c
+59
-64
@@ -7,40 +7,49 @@
7
#include "builtin.h"
8
#include "exec_cmd.h"
9
#include "common-cmds.h"
10
-
11
-static const char *help_default_format;
12
-
13
-static enum help_format {
14
- man_format,
15
- info_format,
16
- web_format,
17
-} help_format = man_format;
18
-
19
-static void parse_help_format(const char *format)
10
+#include "parse-options.h"
11
+
12
+enum help_format {
13
+ HELP_FORMAT_MAN,
14
+ HELP_FORMAT_INFO,
15
+ HELP_FORMAT_WEB,
16
+};
17
+
18
+static int show_all = 0;
19
+static enum help_format help_format = HELP_FORMAT_MAN;
20
+static struct option builtin_help_options[] = {
21
+ OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
22
+ OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
23
+ OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
24
+ HELP_FORMAT_WEB),
25
+ OPT_SET_INT('i', "info", &help_format, "show info page",
26
+ HELP_FORMAT_INFO),
27
+};
28
+
29
+static const char * const builtin_help_usage[] = {
30
+ "git-help [--all] [--man|--web|--info] [command]",
31
+ NULL
32
+};
33
+
34
+static enum help_format parse_help_format(const char *format)
35
{
21
- if (!format) {
22
- help_format = man_format;
23
- return;
24
- }
25
- if (!strcmp(format, "man")) {
26
- help_format = man_format;
27
- return;
28
- }
29
- if (!strcmp(format, "info")) {
30
- help_format = info_format;
31
- return;
32
- }
33
- if (!strcmp(format, "web") || !strcmp(format, "html")) {
34
- help_format = web_format;
35
- return;
36
- }
36
+ if (!strcmp(format, "man"))
37
+ return HELP_FORMAT_MAN;
38
+ if (!strcmp(format, "info"))
39
+ return HELP_FORMAT_INFO;
40
+ if (!strcmp(format, "web") || !strcmp(format, "html"))
41
+ return HELP_FORMAT_WEB;
42
die("unrecognized help format '%s'", format);
43
}
44
45
static int git_help_config(const char *var, const char *value)
46
{
42
- if (!strcmp(var, "help.format"))
43
- return git_config_string(&help_default_format, var, value);
47
+ if (!strcmp(var, "help.format")) {
48
+ if (!value)
49
+ return config_error_nonbool(var);
50
+ help_format = parse_help_format(value);
51
+ return 0;
52
+ }
53
return git_default_config(var, value);
54
}
55
@@ -362,50 +371,36 @@ int cmd_version(int argc, const char **argv, const char *prefix)
371
372
int cmd_help(int argc, const char **argv, const char *prefix)
373
{
365
- const char *help_cmd = argv[1];
374
+ int nongit;
375
367
- if (argc < 2) {
368
- printf("usage: %s\n\n", git_usage_string);
369
- list_common_cmds_help();
370
- exit(0);
371
- }
376
+ setup_git_directory_gently(&nongit);
377
+ git_config(git_help_config);
378
373
- if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
379
+ argc = parse_options(argc, argv, builtin_help_options,
380
+ builtin_help_usage, 0);
381
+
382
+ if (show_all) {
383
printf("usage: %s\n\n", git_usage_string);
384
list_commands();
385
+ return 0;
386
}
387
378
- else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
379
- show_html_page(argc > 2 ? argv[2] : NULL);
380
- }
381
-
382
- else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
383
- show_info_page(argc > 2 ? argv[2] : NULL);
384
- }
385
-
386
- else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
387
- show_man_page(argc > 2 ? argv[2] : NULL);
388
+ if (!argv[0]) {
389
+ printf("usage: %s\n\n", git_usage_string);
390
+ list_common_cmds_help();
391
+ return 0;
392
}
393
390
- else {
391
- int nongit;
392
-
393
- setup_git_directory_gently(&nongit);
394
- git_config(git_help_config);
395
- if (help_default_format)
396
- parse_help_format(help_default_format);
397
-
398
- switch (help_format) {
399
- case man_format:
400
- show_man_page(help_cmd);
401
- break;
402
- case info_format:
403
- show_info_page(help_cmd);
404
- break;
405
- case web_format:
406
- show_html_page(help_cmd);
407
- break;
408
- }
394
+ switch (help_format) {
395
+ case HELP_FORMAT_MAN:
396
+ show_man_page(argv[0]);
397
+ break;
398
+ case HELP_FORMAT_INFO:
399
+ show_info_page(argv[0]);
400
+ break;
401
+ case HELP_FORMAT_WEB:
402
+ show_html_page(argv[0]);
403
+ break;
404
}
405
406
return 0;