--- /dev/null
+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 0F9B46DE1AE1\r
+ for <notmuch@notmuchmail.org>; Tue, 7 Apr 2015 12:32:44 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.436\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.436 tagged_above=-999 required=5 tests=[AWL=0.426, \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 gO_F99FMcIup for <notmuch@notmuchmail.org>;\r
+ Tue, 7 Apr 2015 12:32:41 -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 5E4946DE1ADD\r
+ for <notmuch@notmuchmail.org>; Tue, 7 Apr 2015 12:32:40 -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 1YfZDC-0001xl-I3; Tue, 07 Apr 2015 19:31:02 +0000\r
+Received: (nullmailer pid 16636 invoked by uid 1000); Tue, 07 Apr 2015\r
+ 19:30:44 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: Mark Walters <markwalters1009@gmail.com>, David Bremner\r
+ <david@tethera.net>, notmuch@notmuchmail.org\r
+Subject: argument parsing refactoring round3\r
+Date: Wed, 8 Apr 2015 04:30:38 +0900\r
+Message-Id: <1428435042-16503-1-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <871tjws8w8.fsf@qmul.ac.uk>\r
+References: <871tjws8w8.fsf@qmul.ac.uk>\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: Tue, 07 Apr 2015 19:32:44 -0000\r
+\r
+I think a dealt with all of Mark's comments, even "notmuch help\r
+--help".\r
+\r
+I ended up creating a new function for the places where we want to\r
+process _only_ the shared options (config, setup, and help)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index ab0d188..78680aa 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -467,5 +467,7 @@ notmuch_database_dump (notmuch_database_t *notmuch,\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
++void notmuch_process_shared_options (const char* subcommand_name);\r
++int notmuch_minimal_options (const char* subcommand_name,\r
++ int argc, char **argv);\r
+ #endif\r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index f2cd6a8..9348278 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -874,17 +874,10 @@ notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])\r
+ int ret;\r
+ int opt_index;\r
+ \r
+- notmuch_opt_desc_t options[] = {\r
+- { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },\r
+- { 0, 0, 0, 0, 0 }\r
+- };\r
+-\r
+- opt_index = parse_arguments (argc, argv, options, 1);\r
++ opt_index = notmuch_minimal_options ("config", argc, argv);\r
+ if (opt_index < 0)\r
+ return EXIT_FAILURE;\r
+ \r
+- notmuch_process_shared_options (argv[0]);\r
+-\r
+ /* skip at least subcommand argument */\r
+ argc-= opt_index;\r
+ argv+= opt_index;\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 994ae58..b89a17e 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -740,6 +740,7 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { "false", NOTMUCH_EXCLUDE_FALSE },\r
+ { 0, 0 } } },\r
+ { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },\r
++ { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },\r
+ { 0, 0, 0, 0, 0 }\r
+ };\r
+ \r
+@@ -747,6 +748,8 @@ notmuch_address_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_SENDER | OUTPUT_RECIPIENTS)))\r
+ ctx->output |= OUTPUT_SENDER;\r
+ \r
+diff --git a/notmuch-setup.c b/notmuch-setup.c\r
+index 21d074a..7dd5822 100644\r
+--- a/notmuch-setup.c\r
++++ b/notmuch-setup.c\r
+@@ -133,7 +133,6 @@ notmuch_setup_command (notmuch_config_t *config,\r
+ size_t new_tags_len;\r
+ const char **search_exclude_tags;\r
+ size_t search_exclude_tags_len;\r
+- int opt_index;\r
+ \r
+ #define prompt(format, ...) \\r
+ do { \\r
+@@ -146,18 +145,9 @@ notmuch_setup_command (notmuch_config_t *config,\r
+ chomp_newline (response); \\r
+ } while (0)\r
+ \r
+- notmuch_opt_desc_t options[] = {\r
+- { NOTMUCH_OPT_INHERIT, (void *) ¬much_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
++ if (notmuch_minimal_options ("setup", argc, argv) < 0)\r
+ return EXIT_FAILURE;\r
+ \r
+- /* We can't use argv here as it is sometimes NULL */\r
+- notmuch_process_shared_options ("setup");\r
+-\r
+ if (notmuch_config_is_new (config))\r
+ welcome_message_pre_setup ();\r
+ \r
+diff --git a/notmuch.c b/notmuch.c\r
+index c7f8c8f..2198b73 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -59,18 +59,40 @@ const notmuch_opt_desc_t notmuch_shared_options [] = {\r
+ * notmuch_process_shared_options (subcommand_name);\r
+ */\r
+ void\r
+-notmuch_process_shared_options (const char *help_name) {\r
++notmuch_process_shared_options (const char *subcommand_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
++ int ret = _help_for (subcommand_name);\r
+ exit (ret);\r
+ }\r
+ }\r
+ \r
++/* This is suitable for subcommands that do not actually open the\r
++ * database.\r
++ */\r
++int notmuch_minimal_options (const char *subcommand_name,\r
++ int argc, char **argv)\r
++{\r
++ int opt_index;\r
++\r
++ notmuch_opt_desc_t options[] = {\r
++ { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },\r
++ { 0, 0, 0, 0, 0 }\r
++ };\r
++\r
++ opt_index = parse_arguments (argc, argv, options, 1);\r
++\r
++ if (opt_index < 0)\r
++ return -1;\r
++\r
++ /* We can't use argv here as it is sometimes NULL */\r
++ notmuch_process_shared_options (subcommand_name);\r
++ return opt_index;\r
++}\r
+ \r
+ static command_t commands[] = {\r
+ { NULL, notmuch_command, TRUE,\r
+@@ -250,7 +272,15 @@ _help_for (const char *topic_name)\r
+ static int\r
+ notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[])\r
+ {\r
+- argc--; argv++; /* Ignore "help" */\r
++ int opt_index;\r
++\r
++ opt_index = notmuch_minimal_options ("help", argc, argv);\r
++ if (opt_index < 0)\r
++ return EXIT_FAILURE;\r
++\r
++ /* skip at least subcommand argument */\r
++ argc-= opt_index;\r
++ argv+= opt_index;\r
+ \r
+ if (argc == 0) {\r
+ return _help_for (NULL);\r
+diff --git a/test/random-corpus.c b/test/random-corpus.c\r
+index 6c467bb..b377eb4 100644\r
+--- a/test/random-corpus.c\r
++++ b/test/random-corpus.c\r
+@@ -125,6 +125,14 @@ notmuch_process_shared_options (unused (const char *dummy))\r
+ }\r
+ \r
+ int\r
++notmuch_minimal_options (unused (const char *subcommand),\r
++ unused (int argc),\r
++ unused (char **argv))\r
++{\r
++ return 0;\r
++}\r
++\r
++int\r
+ main (int argc, char **argv)\r
+ {\r
+ \r