X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch.c;h=c528dce2d12100b66d9dffd00093faefd33f5965;hp=12f3e7986a0ee57da1bfb637147e26c61a9819b6;hb=447ad6b4984c71881b7f97641c77f0a39b71a991;hpb=0706e0e3e2da9abf0487ec91a505de82cf5e5c10 diff --git a/notmuch.c b/notmuch.c index 12f3e798..c528dce2 100644 --- a/notmuch.c +++ b/notmuch.c @@ -43,6 +43,57 @@ notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]); static int notmuch_command (notmuch_config_t *config, int argc, char *argv[]); +static int +_help_for (const char *topic); + +static notmuch_bool_t print_version = FALSE, print_help = FALSE; + +const notmuch_opt_desc_t notmuch_shared_options [] = { + { NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 }, + { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 }, + {0, 0, 0, 0, 0} +}; + +/* any subcommand wanting to support these options should call + * inherit notmuch_shared_options and call + * notmuch_process_shared_options (subcommand_name); + */ +void +notmuch_process_shared_options (const char *subcommand_name) { + if (print_version) { + printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n"); + exit (EXIT_SUCCESS); + } + + if (print_help) { + int ret = _help_for (subcommand_name); + exit (ret); + } +} + +/* This is suitable for subcommands that do not actually open the + * database. + */ +int notmuch_minimal_options (const char *subcommand_name, + int argc, char **argv) +{ + int opt_index; + + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 }, + { 0, 0, 0, 0, 0 } + }; + + opt_index = parse_arguments (argc, argv, options, 1); + + if (opt_index < 0) + return -1; + + /* We can't use argv here as it is sometimes NULL */ + notmuch_process_shared_options (subcommand_name); + return opt_index; +} + static command_t commands[] = { { NULL, notmuch_command, TRUE, "Notmuch main command." }, @@ -221,7 +272,15 @@ _help_for (const char *topic_name) static int notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[]) { - argc--; argv++; /* Ignore "help" */ + int opt_index; + + opt_index = notmuch_minimal_options ("help", argc, argv); + if (opt_index < 0) + return EXIT_FAILURE; + + /* skip at least subcommand argument */ + argc-= opt_index; + argv+= opt_index; if (argc == 0) { return _help_for (NULL); @@ -295,14 +354,12 @@ main (int argc, char *argv[]) command_t *command; char *config_file_name = NULL; notmuch_config_t *config = NULL; - notmuch_bool_t print_help=FALSE, print_version=FALSE; int opt_index; int ret; notmuch_opt_desc_t options[] = { - { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 }, - { NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 }, { NOTMUCH_OPT_STRING, &config_file_name, "config", 'c', 0 }, + { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 }, { 0, 0, 0, 0, 0 } }; @@ -324,24 +381,7 @@ main (int argc, char *argv[]) goto DONE; } - /* Handle notmuch --help [command] and notmuch command --help. */ - if (print_help || - (opt_index + 1 < argc && strcmp (argv[opt_index + 1], "--help") == 0)) { - /* - * Pass the first positional argument as argv[1] so the help - * command can give help for it. The help command ignores the - * argv[0] passed to it. - */ - ret = notmuch_help_command (NULL, argc - opt_index + 1, - argv + opt_index - 1); - goto DONE; - } - - if (print_version) { - printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n"); - ret = EXIT_SUCCESS; - goto DONE; - } + notmuch_process_shared_options (NULL); if (opt_index < argc) command_name = argv[opt_index];