]> git.notmuchmail.org Git - notmuch/commitdiff
cli: define shared options, use for --help and --version
authorDavid Bremner <david@tethera.net>
Sun, 5 Apr 2015 13:13:03 +0000 (22:13 +0900)
committerDavid Bremner <david@tethera.net>
Mon, 1 Jun 2015 05:32:54 +0000 (07:32 +0200)
Unfortunately it seems trickier to support --config globally

The non-trivial changes are in notmuch.c; most of the other changes
consists of blindly inserting two lines into every subcommand.

12 files changed:
notmuch-client.h
notmuch-compact.c
notmuch-count.c
notmuch-dump.c
notmuch-insert.c
notmuch-new.c
notmuch-reply.c
notmuch-restore.c
notmuch-search.c
notmuch-show.c
notmuch-tag.c
notmuch.c

index fb3021cc37c1aec68a9b2b193b1cea9c011cd200..8ecfac6b1cb4d8a92365c8e6f0af67d6c557d17b 100644 (file)
@@ -466,4 +466,6 @@ notmuch_database_dump (notmuch_database_t *notmuch,
                       notmuch_bool_t gzip_output);
 
 #include "command-line-arguments.h"
                       notmuch_bool_t gzip_output);
 
 #include "command-line-arguments.h"
+extern const notmuch_opt_desc_t  notmuch_shared_options [];
+void notmuch_process_shared_options (const char* subcommand_name);
 #endif
 #endif
index 2fc012a982d7f3b879fc6fe795d01b194b240c31..5be551d4ed733750cf1d685bcc89ac831e19de4b 100644 (file)
@@ -38,12 +38,16 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])
     notmuch_opt_desc_t options[] = {
        { NOTMUCH_OPT_STRING, &backup_path, "backup", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN,  &quiet, "quiet", 'q', 0 },
     notmuch_opt_desc_t options[] = {
        { NOTMUCH_OPT_STRING, &backup_path, "backup", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN,  &quiet, "quiet", 'q', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
+       { 0, 0, 0, 0, 0}
     };
 
     opt_index = parse_arguments (argc, argv, options, 1);
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     };
 
     opt_index = parse_arguments (argc, argv, options, 1);
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     if (! quiet)
        printf ("Compacting database...\n");
     ret = notmuch_database_compact (path, backup_path,
     if (! quiet)
        printf ("Compacting database...\n");
     ret = notmuch_database_compact (path, backup_path,
index 6058f7c9510d6bdbc15a22c4f24a630eadc50666..57a88a8d1806df1bb0616b64a2555994319f37dd 100644 (file)
@@ -146,6 +146,7 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
                                  { 0, 0 } } },
        { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
        { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
        { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -153,6 +154,8 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     if (input_file_name) {
        batch = TRUE;
        input = fopen (input_file_name, "r");
     if (input_file_name) {
        batch = TRUE;
        input = fopen (input_file_name, "r");
index 9c6ad7f47b0fde514a5e64c96705d598f2eee7ec..fab22bdd6bcfe7f1b0ca26e433001d49e416c4ab 100644 (file)
@@ -228,6 +228,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
                                  { 0, 0 } } },
        { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
        { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
        { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -235,6 +236,8 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     if (opt_index < argc) {
        query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
        if (query_str == NULL) {
     if (opt_index < argc) {
        query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
        if (query_str == NULL) {
index 41a11443ef8f6d0ef5c7d84d68791ac32a32e62c..c277d6206712a23718cbd4c75d55c7fc3bb688e7 100644 (file)
@@ -466,6 +466,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
        { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN, &keep, "keep", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN,  &no_hooks, "no-hooks", 'n', 0 },
        { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN, &keep, "keep", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN,  &no_hooks, "no-hooks", 'n', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { NOTMUCH_OPT_END, 0, 0, 0, 0 }
     };
 
        { NOTMUCH_OPT_END, 0, 0, 0, 0 }
     };
 
@@ -473,6 +474,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     db_path = notmuch_config_get_database_path (config);
     new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
     db_path = notmuch_config_get_database_path (config);
     new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
index 5ac7eedff70bfff763bbe29e39a3f369f7537650..8ff1ade70e49565b0c456272e51133e2e7c9bf17 100644 (file)
@@ -934,6 +934,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
        { NOTMUCH_OPT_BOOLEAN,  &verbose, "verbose", 'v', 0 },
        { NOTMUCH_OPT_BOOLEAN,  &add_files_state.debug, "debug", 'd', 0 },
        { NOTMUCH_OPT_BOOLEAN,  &no_hooks, "no-hooks", 'n', 0 },
        { NOTMUCH_OPT_BOOLEAN,  &verbose, "verbose", 'v', 0 },
        { NOTMUCH_OPT_BOOLEAN,  &add_files_state.debug, "debug", 'd', 0 },
        { NOTMUCH_OPT_BOOLEAN,  &no_hooks, "no-hooks", 'n', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -941,6 +942,8 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     /* quiet trumps verbose */
     if (quiet)
        add_files_state.verbosity = VERBOSITY_QUIET;
     /* quiet trumps verbose */
     if (quiet)
        add_files_state.verbosity = VERBOSITY_QUIET;
index d51fdfc360dd58edc1e5358433663273188aa8a9..4464741fe3567e974e6f07038b93f6446b744150 100644 (file)
@@ -790,6 +790,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
                                  { "sender", FALSE },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },
                                  { "sender", FALSE },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -797,6 +798,8 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     if (format == FORMAT_HEADERS_ONLY) {
        reply_format_func = notmuch_reply_format_headers_only;
     } else if (format == FORMAT_JSON) {
     if (format == FORMAT_HEADERS_ONLY) {
        reply_format_func = notmuch_reply_format_headers_only;
     } else if (format == FORMAT_JSON) {
index 584d9f96ade8dc33a12213b4df6384ed553f9027..2a534dc4544886febb16a246c2caf8d60245baf0 100644 (file)
@@ -154,6 +154,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
                                  { 0, 0 } } },
        { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
        { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
        { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -163,6 +164,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
        goto DONE;
     }
 
        goto DONE;
     }
 
+    notmuch_process_shared_options (argv[0]);
     name_for_error = input_file_name ? input_file_name : "stdin";
 
     if (! accumulate)
     name_for_error = input_file_name ? input_file_name : "stdin";
 
     if (! accumulate)
index b81ac0134ef23cbcfec3d37f063a6f758cc99fdf..b89a17e5c4bfd5adeb5c3a9ce4aa74ca1c58da14 100644 (file)
@@ -681,6 +681,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
        { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0  },
        { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0  },
        { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
        { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0  },
        { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0  },
        { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -689,6 +690,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
        ctx->dupe != -1) {
         fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
        ctx->dupe != -1) {
         fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
@@ -737,6 +740,7 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
                                  { "false", NOTMUCH_EXCLUDE_FALSE },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
                                  { "false", NOTMUCH_EXCLUDE_FALSE },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -744,6 +748,8 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
        ctx->output |= OUTPUT_SENDER;
 
     if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
        ctx->output |= OUTPUT_SENDER;
 
index 43bf71c8c8924e80c28f08469d2615642cba44b4..b80933ad3a00963e07047c56707863291d9e321e 100644 (file)
@@ -1114,6 +1114,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.verify, "verify", 'v', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.output_body, "body", 'b', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.include_html, "include-html", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.verify, "verify", 'v', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.output_body, "body", 'b', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.include_html, "include-html", 0, 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -1121,6 +1122,8 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     /* decryption implies verification */
     if (params.crypto.decrypt)
        params.crypto.verify = TRUE;
     /* decryption implies verification */
     if (params.crypto.decrypt)
        params.crypto.verify = TRUE;
index 2c6e442aa93903f29b3792b73e0f24afab612677..38d99aa9db0be0a40ee720b5776f0fa3ae7167f6 100644 (file)
@@ -206,6 +206,7 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
        { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
        { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
        { NOTMUCH_OPT_BOOLEAN, &remove_all, "remove-all", 0, 0 },
        { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
        { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
        { NOTMUCH_OPT_BOOLEAN, &remove_all, "remove-all", 0, 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -213,6 +214,8 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    notmuch_process_shared_options (argv[0]);
+
     if (input_file_name) {
        batch = TRUE;
        input = fopen (input_file_name, "r");
     if (input_file_name) {
        batch = TRUE;
        input = fopen (input_file_name, "r");
index 12f3e7986a0ee57da1bfb637147e26c61a9819b6..3ce30b456a369266d9831ccf089deeb66763f7ad 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -43,6 +43,35 @@ 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
 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);
+    }
+}
+
+
 static command_t commands[] = {
     { NULL, notmuch_command, TRUE,
       "Notmuch main command." },
 static command_t commands[] = {
     { NULL, notmuch_command, TRUE,
       "Notmuch main command." },
@@ -295,14 +324,12 @@ main (int argc, char *argv[])
     command_t *command;
     char *config_file_name = NULL;
     notmuch_config_t *config = NULL;
     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[] = {
     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_STRING, &config_file_name, "config", 'c', 0 },
+       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
        { 0, 0, 0, 0, 0 }
     };
 
        { 0, 0, 0, 0, 0 }
     };
 
@@ -324,24 +351,7 @@ main (int argc, char *argv[])
        goto DONE;
     }
 
        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];
 
     if (opt_index < argc)
        command_name = argv[opt_index];