1 Return-Path: <markwalters1009@gmail.com>
\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 olra.theworths.org (Postfix) with ESMTP id B1028431FB6
\r
6 for <notmuch@notmuchmail.org>; Tue, 5 Jun 2012 07:36:44 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0.201 tagged_above=-999 required=5
\r
12 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
\r
13 FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001,
\r
14 RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
15 Received: from olra.theworths.org ([127.0.0.1])
\r
16 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
17 with ESMTP id uV8m17Ibeigq for <notmuch@notmuchmail.org>;
\r
18 Tue, 5 Jun 2012 07:36:42 -0700 (PDT)
\r
19 Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com
\r
20 [74.125.82.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client
\r
21 certificate requested) by olra.theworths.org (Postfix) with ESMTPS id
\r
22 8C2CD431FAE for <notmuch@notmuchmail.org>; Tue, 5 Jun 2012 07:36:42 -0700
\r
24 Received: by wgbdt14 with SMTP id dt14so4367607wgb.2
\r
25 for <notmuch@notmuchmail.org>; Tue, 05 Jun 2012 07:36:39 -0700 (PDT)
\r
26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
\r
27 h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;
\r
28 bh=HWxzz8H+vobZV9ooepheMwydwZagFl56U1Ylacl7IMo=;
\r
29 b=NZw9goUOlmoSQD9T4S57D54wU5K5A4c2dtBiNfX9ZgB8iZ6IB0eof/CFXbSg/N6Bcd
\r
30 x93LF3eIfq3XkBkrwyOx/NrogiQDOfZymXhqYrrsSNustB93r4INq1J+Ln8Hz/g0zRN5
\r
31 655mMI/MT8WRbcNuXIcwC3acDbpaw6pnU7dcUcBn+P9BAVBAuyP9n3pHyZD9sJxG0x2E
\r
32 +HihLvZ0WWBbzGhcmUxceA4//YEI/p4YF3CuTgZQVeUKHLdEpkUj3hoEnkt4W+7L20j8
\r
33 YJzj4V8vJQkhKBdjxpdbeoP5268QAeRS7tk/YK7hglNi/pzBa0Cavaxd91T6IalzzDqg
\r
35 Received: by 10.216.206.164 with SMTP id l36mr14126494weo.154.1338906999766;
\r
36 Tue, 05 Jun 2012 07:36:39 -0700 (PDT)
\r
37 Received: from localhost (94-192-233-223.zone6.bethere.co.uk.
\r
38 [94.192.233.223]) by mx.google.com with ESMTPS id
\r
39 m20sm27402051wie.6.2012.06.05.07.36.37 (version=TLSv1/SSLv3 cipher=OTHER);
\r
40 Tue, 05 Jun 2012 07:36:38 -0700 (PDT)
\r
41 From: Mark Walters <markwalters1009@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v2] cli: make the command line parser's errors more
\r
45 Date: Tue, 5 Jun 2012 15:36:36 +0100
\r
46 Message-Id: <1338906996-18720-1-git-send-email-markwalters1009@gmail.com>
\r
47 X-Mailer: git-send-email 1.7.9.1
\r
48 In-Reply-To: <87oboxakus.fsf@qmul.ac.uk>
\r
49 References: <87oboxakus.fsf@qmul.ac.uk>
\r
50 X-BeenThere: notmuch@notmuchmail.org
\r
51 X-Mailman-Version: 2.1.13
\r
53 List-Id: "Use and development of the notmuch mail system."
\r
54 <notmuch.notmuchmail.org>
\r
55 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
56 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
57 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
58 List-Post: <mailto:notmuch@notmuchmail.org>
\r
59 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
60 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
61 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
62 X-List-Received-Date: Tue, 05 Jun 2012 14:36:44 -0000
\r
64 Previously, the cli parser was a little erratic in what errors it
\r
65 reported and would fail silently in many cases (for example, when no
\r
66 argument was passed to an integer option). This was particularly
\r
67 annoying as the user could not (easily) tell whether the command
\r
68 failed or just there were no search results.
\r
70 This patch tries to make the handling consistent and return a helpful
\r
71 error message in all cases.
\r
73 command-line-arguments.c | 76 ++++++++++++++++++++++++++++++----------------
\r
74 1 files changed, 50 insertions(+), 26 deletions(-)
\r
76 diff --git a/command-line-arguments.c b/command-line-arguments.c
\r
77 index b0a0dab..bf9aeca 100644
\r
78 --- a/command-line-arguments.c
\r
79 +++ b/command-line-arguments.c
\r
80 @@ -15,7 +15,7 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
\r
82 const notmuch_keyword_t *keywords = arg_desc->keywords;
\r
85 + if (next == '\0') {
\r
86 /* No keyword given */
\r
89 @@ -29,17 +29,17 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
\r
94 - fprintf (stderr, "unknown keyword: %s\n", arg_str);
\r
96 + fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
\r
98 - fprintf (stderr, "option %s needs a keyword\n", arg_desc->name);
\r
99 + fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
\r
103 static notmuch_bool_t
\r
104 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
\r
107 + if (next == '\0') {
\r
108 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
\r
111 @@ -51,9 +51,43 @@ _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
\r
112 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
\r
115 + fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
\r
119 +static notmuch_bool_t
\r
120 +_process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
\r
123 + if (next == '\0' || arg_str[0] == '\0') {
\r
124 + fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
\r
128 + *((int *)arg_desc->output_var) = strtol (arg_str, &endptr, 10);
\r
129 + if (*endptr == '\0')
\r
132 + fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
\r
133 + arg_str, arg_desc->name);
\r
137 +static notmuch_bool_t
\r
138 +_process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
\r
140 + if (next == '\0') {
\r
141 + fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
\r
144 + if (arg_str[0] == '\0') {
\r
145 + fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
\r
148 + *((const char **)arg_desc->output_var) = arg_str;
\r
153 Search for the {pos_arg_index}th position argument, return FALSE if
\r
154 that does not exist.
\r
155 @@ -93,26 +127,19 @@ parse_option (const char *arg,
\r
159 - const notmuch_opt_desc_t *try = options;
\r
160 - while (try->opt_type != NOTMUCH_OPT_END) {
\r
161 + const notmuch_opt_desc_t *try;
\r
162 + for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
\r
163 if (try->name && strncmp (arg, try->name, strlen (try->name)) == 0) {
\r
164 char next = arg[strlen (try->name)];
\r
165 const char *value= arg+strlen(try->name)+1;
\r
169 - /* Everything but boolean arguments (switches) needs a
\r
170 - * delimiter, and a non-zero length value. Boolean
\r
171 - * arguments may take an optional =true or =false value.
\r
173 - if (next != '=' && next != ':' && next != 0) return FALSE;
\r
175 - if (try->opt_type != NOTMUCH_OPT_BOOLEAN &&
\r
176 - try->opt_type != NOTMUCH_OPT_KEYWORD)
\r
179 - if (value[0] == 0) return FALSE;
\r
181 + /* If we have not reached the end of the argument
\r
182 + (i.e. the next character is not a space or delimiter)
\r
183 + then the argument could still match a longer option
\r
184 + name later in the option table.
\r
186 + if (next != '=' && next != ':' && next != '\0')
\r
189 if (try->output_var == NULL)
\r
190 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
\r
191 @@ -125,12 +152,10 @@ parse_option (const char *arg,
\r
192 return _process_boolean_arg (try, next, value);
\r
194 case NOTMUCH_OPT_INT:
\r
195 - *((int *)try->output_var) = strtol (value, &endptr, 10);
\r
196 - return (*endptr == 0);
\r
197 + return _process_int_arg (try, next, value);
\r
199 case NOTMUCH_OPT_STRING:
\r
200 - *((const char **)try->output_var) = value;
\r
202 + return _process_string_arg (try, next, value);
\r
204 case NOTMUCH_OPT_POSITION:
\r
205 case NOTMUCH_OPT_END:
\r
206 @@ -139,7 +164,6 @@ parse_option (const char *arg,
\r
212 fprintf (stderr, "Unrecognized option: --%s\n", arg);
\r