Re: Hi all
[notmuch-archives.git] / b5 / b0bb644df922ccf88b40d125c7734a3574ad06
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 C5828431FBD\r
6         for <notmuch@notmuchmail.org>; Sun,  3 Jun 2012 04:48:52 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.201\r
10 X-Spam-Level: \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 1PHnEloarOmA for <notmuch@notmuchmail.org>;\r
18         Sun,  3 Jun 2012 04:48:52 -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  0C380431FAE    for <notmuch@notmuchmail.org>; Sun,  3 Jun 2012 04:48:51 -0700\r
23  (PDT)\r
24 Received: by wgbdt14 with SMTP id dt14so2743148wgb.2\r
25         for <notmuch@notmuchmail.org>; Sun, 03 Jun 2012 04:48:50 -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=LEHOHYCG8CDKJS852BxNc9bn0alJQB3UZE6HydZVfgM=;\r
29         b=0ORIE44Bm7fQNY4VLejFJJryJsthvQAMb3BU8iLNaZ2pTeV2gGsjYp3tATomSefybi\r
30         QHC6KGnOKAt9l+H9MQ3axu4sKh33oJIqHSGePn566c5+f/jSZnc3O1AkVrb2ADY4ZBWt\r
31         2PQ8DSSyQJCj+fZNlfeQhoeWwDPkmazIxLwrkDXYqdatAp9mEIiC5Zgk1Rp+IJn1DN7h\r
32         W/irr1JNt85hkKb5Y0j7phaoROu8t7bDFrVUEqsgIaim2gJxMaqrWvGC0CyzmSpA1Epi\r
33         bLIGY/cvwNRGbdid43FvjlLqOUtkYu1R/iUF5OBEqED1VX7tVuqzniJmaC8GUlPjRMOg\r
34         J74A==\r
35 Received: by 10.216.216.148 with SMTP id g20mr8109988wep.187.1338724130670;\r
36         Sun, 03 Jun 2012 04:48:50 -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  m20sm12149276wie.6.2012.06.03.04.48.49 (version=TLSv1/SSLv3 cipher=OTHER);\r
40         Sun, 03 Jun 2012 04:48:50 -0700 (PDT)\r
41 From: Mark Walters <markwalters1009@gmail.com>\r
42 To: notmuch@notmuchmail.org\r
43 Subject: [PATCH] cli: make the command line parser's errors more informative.\r
44 Date: Sun,  3 Jun 2012 12:48:48 +0100\r
45 Message-Id: <1338724128-13158-1-git-send-email-markwalters1009@gmail.com>\r
46 X-Mailer: git-send-email 1.7.9.1\r
47 In-Reply-To: <1338723972-13063-1-git-send-email-markwalters1009@gmail.com>\r
48 References: <1338723972-13063-1-git-send-email-markwalters1009@gmail.com>\r
49 X-BeenThere: notmuch@notmuchmail.org\r
50 X-Mailman-Version: 2.1.13\r
51 Precedence: list\r
52 List-Id: "Use and development of the notmuch mail system."\r
53         <notmuch.notmuchmail.org>\r
54 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
56 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
57 List-Post: <mailto:notmuch@notmuchmail.org>\r
58 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
59 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
60         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
61 X-List-Received-Date: Sun, 03 Jun 2012 11:48:52 -0000\r
62 \r
63 Previously, the cli parser was a little erratic in what errors it\r
64 reported and would fail silently in many cases (for example, when no\r
65 argument was passed to an integer option). This was particularly\r
66 annoying as the user could not (easily) tell whether the command\r
67 failed or just there were no search results.\r
68 \r
69 This patch tries to make the handling consistent and return a helpful\r
70 error message in all cases.\r
71 ---\r
72 \r
73  command-line-arguments.c |   66 ++++++++++++++++++++++++++++++++--------------\r
74  1 files changed, 46 insertions(+), 20 deletions(-)\r
75 \r
76 diff --git a/command-line-arguments.c b/command-line-arguments.c\r
77 index b0a0dab..ea56467 100644\r
78 --- a/command-line-arguments.c\r
79 +++ b/command-line-arguments.c\r
80 @@ -30,9 +30,9 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char\r
81         keywords++;\r
82      }\r
83      if (next != 0)\r
84 -       fprintf (stderr, "unknown keyword: %s\n", arg_str);\r
85 +       fprintf (stderr, "Unknown argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);\r
86      else\r
87 -       fprintf (stderr, "option %s needs a keyword\n", arg_desc->name);\r
88 +       fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);\r
89      return FALSE;\r
90  }\r
91  \r
92 @@ -51,9 +51,43 @@ _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char\r
93         *((notmuch_bool_t *)arg_desc->output_var) = TRUE;\r
94         return TRUE;\r
95      }\r
96 +    fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);\r
97      return FALSE;\r
98  }\r
99  \r
100 +static notmuch_bool_t\r
101 +_process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {\r
102 +\r
103 +    char *endptr;\r
104 +    if (next == 0 || arg_str[0] == 0) {\r
105 +       fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);\r
106 +       return FALSE;\r
107 +    }\r
108 +\r
109 +    *((int *)arg_desc->output_var) = strtol (arg_str, &endptr, 10);\r
110 +    if (*endptr == 0)\r
111 +       return TRUE;\r
112 +\r
113 +    fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",\r
114 +            arg_str, arg_desc->name);\r
115 +    return FALSE;\r
116 +}\r
117 +\r
118 +static notmuch_bool_t\r
119 +_process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {\r
120 +\r
121 +    if (next == 0) {\r
122 +       fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);\r
123 +       return FALSE;\r
124 +    }\r
125 +    if (arg_str[0] == 0) {\r
126 +       fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);\r
127 +       return FALSE;\r
128 +    }\r
129 +    *((const char **)arg_desc->output_var) = arg_str;\r
130 +    return TRUE;\r
131 +}\r
132 +\r
133  /*\r
134     Search for the {pos_arg_index}th position argument, return FALSE if\r
135     that does not exist.\r
136 @@ -99,20 +133,13 @@ parse_option (const char *arg,\r
137             char next = arg[strlen (try->name)];\r
138             const char *value= arg+strlen(try->name)+1;\r
139  \r
140 -           char *endptr;\r
141 -\r
142 -           /* Everything but boolean arguments (switches) needs a\r
143 -            * delimiter, and a non-zero length value. Boolean\r
144 -            * arguments may take an optional =true or =false value.\r
145 -            */\r
146 -           if (next != '=' && next != ':' && next != 0) return FALSE;\r
147 -           if (next == 0) {\r
148 -               if (try->opt_type != NOTMUCH_OPT_BOOLEAN &&\r
149 -                   try->opt_type != NOTMUCH_OPT_KEYWORD)\r
150 -                   return FALSE;\r
151 -           } else {\r
152 -               if (value[0] == 0) return FALSE;\r
153 -           }\r
154 +           /* If this is not the end of the argument (i.e. the next\r
155 +            * character is not a space or a delimiter) we stop\r
156 +            * parsing for this option but allow the parsing to\r
157 +            * continue to for other options. This should allow\r
158 +            * options to be initial segments of other options. */\r
159 +           if (next != '=' && next != ':' && next != 0)\r
160 +               goto DONE_THIS_OPTION;\r
161  \r
162             if (try->output_var == NULL)\r
163                 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);\r
164 @@ -125,12 +152,10 @@ parse_option (const char *arg,\r
165                 return _process_boolean_arg (try, next, value);\r
166                 break;\r
167             case NOTMUCH_OPT_INT:\r
168 -               *((int *)try->output_var) = strtol (value, &endptr, 10);\r
169 -               return (*endptr == 0);\r
170 +               return _process_int_arg (try, next, value);\r
171                 break;\r
172             case NOTMUCH_OPT_STRING:\r
173 -               *((const char **)try->output_var) = value;\r
174 -               return TRUE;\r
175 +               return _process_string_arg (try, next, value);\r
176                 break;\r
177             case NOTMUCH_OPT_POSITION:\r
178             case NOTMUCH_OPT_END:\r
179 @@ -139,6 +164,7 @@ parse_option (const char *arg,\r
180                 /*UNREACHED*/\r
181             }\r
182         }\r
183 +    DONE_THIS_OPTION:\r
184         try++;\r
185      }\r
186      fprintf (stderr, "Unrecognized option: --%s\n", arg);\r
187 -- \r
188 1.7.9.1\r
189 \r