Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / 5b / c4f721e5ee1e585d0f5b89978cf7c41ab945a4
1 Return-Path: <bremner@tesseract.cs.unb.ca>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5  by arlo.cworth.org (Postfix) with ESMTP id 0F9B46DE1AE1\r
6  for <notmuch@notmuchmail.org>; Tue,  7 Apr 2015 12:32:44 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.436\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.436 tagged_above=-999 required=5 tests=[AWL=0.426, \r
12  T_HEADER_FROM_DIFFERENT_DOMAINS=0.01] autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id gO_F99FMcIup for <notmuch@notmuchmail.org>;\r
16  Tue,  7 Apr 2015 12:32:41 -0700 (PDT)\r
17 Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
18  [87.98.215.224])\r
19  by arlo.cworth.org (Postfix) with ESMTPS id 5E4946DE1ADD\r
20  for <notmuch@notmuchmail.org>; Tue,  7 Apr 2015 12:32:40 -0700 (PDT)\r
21 Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
22  4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
23  id 1YfZDC-0001xl-I3; Tue, 07 Apr 2015 19:31:02 +0000\r
24 Received: (nullmailer pid 16636 invoked by uid 1000); Tue, 07 Apr 2015\r
25  19:30:44 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: Mark Walters <markwalters1009@gmail.com>, David Bremner\r
28  <david@tethera.net>, notmuch@notmuchmail.org\r
29 Subject: argument parsing refactoring round3\r
30 Date: Wed,  8 Apr 2015 04:30:38 +0900\r
31 Message-Id: <1428435042-16503-1-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 2.1.4\r
33 In-Reply-To: <871tjws8w8.fsf@qmul.ac.uk>\r
34 References: <871tjws8w8.fsf@qmul.ac.uk>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.18\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39  <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Tue, 07 Apr 2015 19:32:44 -0000\r
48 \r
49 I think a dealt with all of Mark's comments, even "notmuch help\r
50 --help".\r
51 \r
52 I ended up creating a new function for the places where we want to\r
53 process _only_ the shared options (config, setup, and help)\r
54 \r
55 diff --git a/notmuch-client.h b/notmuch-client.h\r
56 index ab0d188..78680aa 100644\r
57 --- a/notmuch-client.h\r
58 +++ b/notmuch-client.h\r
59 @@ -467,5 +467,7 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
60  \r
61  #include "command-line-arguments.h"\r
62  extern const notmuch_opt_desc_t  notmuch_shared_options [];\r
63 -void notmuch_process_shared_options (const char* help_name);\r
64 +void notmuch_process_shared_options (const char* subcommand_name);\r
65 +int notmuch_minimal_options (const char* subcommand_name,\r
66 +                            int argc, char **argv);\r
67  #endif\r
68 diff --git a/notmuch-config.c b/notmuch-config.c\r
69 index f2cd6a8..9348278 100644\r
70 --- a/notmuch-config.c\r
71 +++ b/notmuch-config.c\r
72 @@ -874,17 +874,10 @@ notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])\r
73      int ret;\r
74      int opt_index;\r
75  \r
76 -    notmuch_opt_desc_t options[] = {\r
77 -       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
78 -       { 0, 0, 0, 0, 0 }\r
79 -    };\r
80 -\r
81 -    opt_index = parse_arguments (argc, argv, options, 1);\r
82 +    opt_index = notmuch_minimal_options ("config", argc, argv);\r
83      if (opt_index < 0)\r
84         return EXIT_FAILURE;\r
85  \r
86 -    notmuch_process_shared_options (argv[0]);\r
87 -\r
88      /* skip at least subcommand argument */\r
89      argc-= opt_index;\r
90      argv+= opt_index;\r
91 diff --git a/notmuch-search.c b/notmuch-search.c\r
92 index 994ae58..b89a17e 100644\r
93 --- a/notmuch-search.c\r
94 +++ b/notmuch-search.c\r
95 @@ -740,6 +740,7 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
96                                   { "false", NOTMUCH_EXCLUDE_FALSE },\r
97                                   { 0, 0 } } },\r
98         { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },\r
99 +       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
100         { 0, 0, 0, 0, 0 }\r
101      };\r
102  \r
103 @@ -747,6 +748,8 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
104      if (opt_index < 0)\r
105         return EXIT_FAILURE;\r
106  \r
107 +    notmuch_process_shared_options (argv[0]);\r
108 +\r
109      if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))\r
110         ctx->output |= OUTPUT_SENDER;\r
111  \r
112 diff --git a/notmuch-setup.c b/notmuch-setup.c\r
113 index 21d074a..7dd5822 100644\r
114 --- a/notmuch-setup.c\r
115 +++ b/notmuch-setup.c\r
116 @@ -133,7 +133,6 @@ notmuch_setup_command (notmuch_config_t *config,\r
117      size_t new_tags_len;\r
118      const char **search_exclude_tags;\r
119      size_t search_exclude_tags_len;\r
120 -    int opt_index;\r
121  \r
122  #define prompt(format, ...)                                    \\r
123      do {                                                       \\r
124 @@ -146,18 +145,9 @@ notmuch_setup_command (notmuch_config_t *config,\r
125         chomp_newline (response);                               \\r
126      } while (0)\r
127  \r
128 -    notmuch_opt_desc_t options[] = {\r
129 -       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
130 -       { 0, 0, 0, 0, 0 }\r
131 -    };\r
132 -\r
133 -    opt_index = parse_arguments (argc, argv, options, 1);\r
134 -    if (opt_index < 0)\r
135 +    if (notmuch_minimal_options ("setup", argc, argv) < 0)\r
136         return EXIT_FAILURE;\r
137  \r
138 -    /* We can't use argv here as it is sometimes NULL */\r
139 -    notmuch_process_shared_options ("setup");\r
140 -\r
141      if (notmuch_config_is_new (config))\r
142         welcome_message_pre_setup ();\r
143  \r
144 diff --git a/notmuch.c b/notmuch.c\r
145 index c7f8c8f..2198b73 100644\r
146 --- a/notmuch.c\r
147 +++ b/notmuch.c\r
148 @@ -59,18 +59,40 @@ const notmuch_opt_desc_t notmuch_shared_options [] = {\r
149   * notmuch_process_shared_options (subcommand_name);\r
150   */\r
151  void\r
152 -notmuch_process_shared_options (const char *help_name) {\r
153 +notmuch_process_shared_options (const char *subcommand_name) {\r
154      if (print_version) {\r
155         printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n");\r
156         exit (EXIT_SUCCESS);\r
157      }\r
158  \r
159      if (print_help) {\r
160 -       int ret = _help_for (help_name);\r
161 +       int ret = _help_for (subcommand_name);\r
162         exit (ret);\r
163      }\r
164  }\r
165  \r
166 +/* This is suitable for subcommands that do not actually open the\r
167 + * database.\r
168 + */\r
169 +int notmuch_minimal_options (const char *subcommand_name,\r
170 +                                 int argc, char **argv)\r
171 +{\r
172 +    int opt_index;\r
173 +\r
174 +    notmuch_opt_desc_t options[] = {\r
175 +       { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
176 +       { 0, 0, 0, 0, 0 }\r
177 +    };\r
178 +\r
179 +    opt_index = parse_arguments (argc, argv, options, 1);\r
180 +\r
181 +    if (opt_index < 0)\r
182 +       return -1;\r
183 +\r
184 +    /* We can't use argv here as it is sometimes NULL */\r
185 +    notmuch_process_shared_options (subcommand_name);\r
186 +    return opt_index;\r
187 +}\r
188  \r
189  static command_t commands[] = {\r
190      { NULL, notmuch_command, TRUE,\r
191 @@ -250,7 +272,15 @@ _help_for (const char *topic_name)\r
192  static int\r
193  notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[])\r
194  {\r
195 -    argc--; argv++; /* Ignore "help" */\r
196 +    int opt_index;\r
197 +\r
198 +    opt_index = notmuch_minimal_options ("help", argc, argv);\r
199 +    if (opt_index < 0)\r
200 +       return EXIT_FAILURE;\r
201 +\r
202 +    /* skip at least subcommand argument */\r
203 +    argc-= opt_index;\r
204 +    argv+= opt_index;\r
205  \r
206      if (argc == 0) {\r
207         return _help_for (NULL);\r
208 diff --git a/test/random-corpus.c b/test/random-corpus.c\r
209 index 6c467bb..b377eb4 100644\r
210 --- a/test/random-corpus.c\r
211 +++ b/test/random-corpus.c\r
212 @@ -125,6 +125,14 @@ notmuch_process_shared_options (unused (const char *dummy))\r
213  }\r
214  \r
215  int\r
216 +notmuch_minimal_options (unused (const char *subcommand),\r
217 +                        unused (int argc),\r
218 +                        unused (char **argv))\r
219 +{\r
220 +    return 0;\r
221 +}\r
222 +\r
223 +int\r
224  main (int argc, char **argv)\r
225  {\r
226  \r