[Patch v2 3/4] cli: define shared options, use for --help and --version
authorDavid Bremner <david@tethera.net>
Mon, 6 Apr 2015 12:22:37 +0000 (21:22 +0900)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Aug 2016 21:48:44 +0000 (14:48 -0700)
33/a3624310b63c35036bb0fb98c0c666b5bfe6f9 [new file with mode: 0644]

diff --git a/33/a3624310b63c35036bb0fb98c0c666b5bfe6f9 b/33/a3624310b63c35036bb0fb98c0c666b5bfe6f9
new file mode 100644 (file)
index 0000000..7040daa
--- /dev/null
@@ -0,0 +1,373 @@
+Return-Path: <bremner@tesseract.cs.unb.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id CB3D86DE1B43\r
+ for <notmuch@notmuchmail.org>; Mon,  6 Apr 2015 05:25:04 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.454\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.454 tagged_above=-999 required=5 tests=[AWL=0.444, \r
+ T_HEADER_FROM_DIFFERENT_DOMAINS=0.01] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id WZOygRn0rctd for <notmuch@notmuchmail.org>;\r
+ Mon,  6 Apr 2015 05:25:03 -0700 (PDT)\r
+Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
+ [87.98.215.224])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 92FB26DE1AFC\r
+ for <notmuch@notmuchmail.org>; Mon,  6 Apr 2015 05:25:02 -0700 (PDT)\r
+Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
+ 4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1Yf64i-00059R-Ed; Mon, 06 Apr 2015 12:24:20 +0000\r
+Received: (nullmailer pid 3284 invoked by uid 1000); Mon, 06 Apr 2015\r
+ 12:22:58 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
+Subject: [Patch v2 3/4] cli: define shared options, use for --help and\r
+ --version\r
+Date: Mon,  6 Apr 2015 21:22:37 +0900\r
+Message-Id: <1428322958-2887-4-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1428322958-2887-1-git-send-email-david@tethera.net>\r
+References: <87d23ixnr7.fsf@maritornes.cs.unb.ca>\r
+ <1428322958-2887-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.18\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 06 Apr 2015 12:25:04 -0000\r
+\r
+Unfortunately it seems trickier to support --config globally\r
+\r
+The non-trivial changes are in notmuch.c; most of the other changes\r
+consists of blindly inserting two lines into every subcommand.\r
+---\r
+ notmuch-client.h  |  2 ++\r
+ notmuch-compact.c |  4 ++++\r
+ notmuch-count.c   |  3 +++\r
+ notmuch-dump.c    |  3 +++\r
+ notmuch-insert.c  |  3 +++\r
+ notmuch-new.c     |  3 +++\r
+ notmuch-reply.c   |  3 +++\r
+ notmuch-restore.c |  2 ++\r
+ notmuch-search.c  |  3 +++\r
+ notmuch-show.c    |  3 +++\r
+ notmuch-tag.c     |  3 +++\r
+ notmuch.c         | 52 +++++++++++++++++++++++++++++++---------------------\r
+ 12 files changed, 63 insertions(+), 21 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index fb3021c..ab0d188 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -466,4 +466,6 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
+                      notmuch_bool_t gzip_output);\r
\r
+ #include "command-line-arguments.h"\r
++extern const notmuch_opt_desc_t  notmuch_shared_options [];\r
++void notmuch_process_shared_options (const char* help_name);\r
+ #endif\r
+diff --git a/notmuch-compact.c b/notmuch-compact.c\r
+index 2fc012a..5be551d 100644\r
+--- a/notmuch-compact.c\r
++++ b/notmuch-compact.c\r
+@@ -38,12 +38,16 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])\r
+     notmuch_opt_desc_t options[] = {\r
+       { NOTMUCH_OPT_STRING, &backup_path, "backup", 0, 0 },\r
+       { NOTMUCH_OPT_BOOLEAN,  &quiet, "quiet", 'q', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
++      { 0, 0, 0, 0, 0}\r
+     };\r
\r
+     opt_index = parse_arguments (argc, argv, options, 1);\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     if (! quiet)\r
+       printf ("Compacting database...\n");\r
+     ret = notmuch_database_compact (path, backup_path,\r
+diff --git a/notmuch-count.c b/notmuch-count.c\r
+index 6058f7c..57a88a8 100644\r
+--- a/notmuch-count.c\r
++++ b/notmuch-count.c\r
+@@ -146,6 +146,7 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])\r
+                                 { 0, 0 } } },\r
+       { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },\r
+       { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -153,6 +154,8 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     if (input_file_name) {\r
+       batch = TRUE;\r
+       input = fopen (input_file_name, "r");\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index 9c6ad7f..fab22bd 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -228,6 +228,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+                                 { 0, 0 } } },\r
+       { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },\r
+       { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -235,6 +236,8 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     if (opt_index < argc) {\r
+       query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);\r
+       if (query_str == NULL) {\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 90fe3ba..697880f 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -466,6 +466,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+       { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },\r
+       { NOTMUCH_OPT_BOOLEAN, &keep, "keep", 0, 0 },\r
+       { NOTMUCH_OPT_BOOLEAN,  &no_hooks, "no-hooks", 'n', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -473,6 +474,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     db_path = notmuch_config_get_database_path (config);\r
+     new_tags = notmuch_config_get_new_tags (config, &new_tags_length);\r
+     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index e6c283e..895f5d9 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -934,6 +934,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+       { NOTMUCH_OPT_BOOLEAN,  &verbose, "verbose", 'v', 0 },\r
+       { NOTMUCH_OPT_BOOLEAN,  &add_files_state.debug, "debug", 'd', 0 },\r
+       { NOTMUCH_OPT_BOOLEAN,  &no_hooks, "no-hooks", 'n', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -941,6 +942,8 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     /* quiet trumps verbose */\r
+     if (quiet)\r
+       add_files_state.verbosity = VERBOSITY_QUIET;\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index d51fdfc..4464741 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -790,6 +790,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+                                 { "sender", FALSE },\r
+                                 { 0, 0 } } },\r
+       { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -797,6 +798,8 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     if (format == FORMAT_HEADERS_ONLY) {\r
+       reply_format_func = notmuch_reply_format_headers_only;\r
+     } else if (format == FORMAT_JSON) {\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 584d9f9..2a534dc 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -154,6 +154,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+                                 { 0, 0 } } },\r
+       { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
+       { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -163,6 +164,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+       goto DONE;\r
+     }\r
\r
++    notmuch_process_shared_options (argv[0]);\r
+     name_for_error = input_file_name ? input_file_name : "stdin";\r
\r
+     if (! accumulate)\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index b81ac01..994ae58 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -681,6 +681,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+       { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0  },\r
+       { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0  },\r
+       { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -689,6 +690,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&\r
+       ctx->dupe != -1) {\r
+         fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 43bf71c..b80933a 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1114,6 +1114,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
+       { NOTMUCH_OPT_BOOLEAN, &params.crypto.verify, "verify", 'v', 0 },\r
+       { NOTMUCH_OPT_BOOLEAN, &params.output_body, "body", 'b', 0 },\r
+       { NOTMUCH_OPT_BOOLEAN, &params.include_html, "include-html", 0, 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -1121,6 +1122,8 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     /* decryption implies verification */\r
+     if (params.crypto.decrypt)\r
+       params.crypto.verify = TRUE;\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 5b2f1e4..35f971d 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -206,6 +206,7 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])\r
+       { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },\r
+       { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
+       { NOTMUCH_OPT_BOOLEAN, &remove_all, "remove-all", 0, 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -213,6 +214,8 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])\r
+     if (opt_index < 0)\r
+       return EXIT_FAILURE;\r
\r
++    notmuch_process_shared_options (argv[0]);\r
++\r
+     if (input_file_name) {\r
+       batch = TRUE;\r
+       input = fopen (input_file_name, "r");\r
+diff --git a/notmuch.c b/notmuch.c\r
+index bff941f..c7f8c8f 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -43,6 +43,35 @@ notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]);\r
+ static int\r
+ notmuch_command (notmuch_config_t *config, int argc, char *argv[]);\r
\r
++static int\r
++_help_for (const char *topic);\r
++\r
++static notmuch_bool_t print_version = FALSE, print_help = FALSE;\r
++\r
++const notmuch_opt_desc_t notmuch_shared_options [] = {\r
++    { NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 },\r
++    { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },\r
++    {0, 0, 0, 0, 0}\r
++};\r
++\r
++/* any subcommand wanting to support these options should call\r
++ * inherit notmuch_shared_options and call\r
++ * notmuch_process_shared_options (subcommand_name);\r
++ */\r
++void\r
++notmuch_process_shared_options (const char *help_name) {\r
++    if (print_version) {\r
++      printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n");\r
++      exit (EXIT_SUCCESS);\r
++    }\r
++\r
++    if (print_help) {\r
++      int ret = _help_for (help_name);\r
++      exit (ret);\r
++    }\r
++}\r
++\r
++\r
+ static command_t commands[] = {\r
+     { NULL, notmuch_command, TRUE,\r
+       "Notmuch main command." },\r
+@@ -295,14 +324,12 @@ main (int argc, char *argv[])\r
+     command_t *command;\r
+     char *config_file_name = NULL;\r
+     notmuch_config_t *config = NULL;\r
+-    notmuch_bool_t print_help=FALSE, print_version=FALSE;\r
+     int opt_index;\r
+     int ret;\r
\r
+     notmuch_opt_desc_t options[] = {\r
+-      { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },\r
+-      { NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 },\r
+       { NOTMUCH_OPT_STRING, &config_file_name, "config", 'c', 0 },\r
++      { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -324,24 +351,7 @@ main (int argc, char *argv[])\r
+       goto DONE;\r
+     }\r
\r
+-    /* Handle notmuch --help [command] and notmuch command --help. */\r
+-    if (print_help ||\r
+-      (opt_index + 1 < argc && strcmp (argv[opt_index + 1], "--help") == 0)) {\r
+-      /*\r
+-       * Pass the first positional argument as argv[1] so the help\r
+-       * command can give help for it. The help command ignores the\r
+-       * argv[0] passed to it.\r
+-       */\r
+-      ret = notmuch_help_command (NULL, argc - opt_index + 1,\r
+-                                  argv + opt_index - 1);\r
+-      goto DONE;\r
+-    }\r
+-\r
+-    if (print_version) {\r
+-      printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n");\r
+-      ret = EXIT_SUCCESS;\r
+-      goto DONE;\r
+-    }\r
++    notmuch_process_shared_options (NULL);\r
\r
+     if (opt_index < argc)\r
+       command_name = argv[opt_index];\r
+-- \r
+2.1.4\r
+\r