1 Return-Path: <sojkam1@fel.cvut.cz>
\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 EB1B7431FAE
\r
6 for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:38 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5
\r
12 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id QJoXCiJXBwAd for <notmuch@notmuchmail.org>;
\r
16 Mon, 22 Sep 2014 02:39:32 -0700 (PDT)
\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])
\r
18 by olra.theworths.org (Postfix) with ESMTP id 21AD5431FBC
\r
19 for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:28 -0700 (PDT)
\r
20 Received: from localhost (unknown [192.168.200.7])
\r
21 by max.feld.cvut.cz (Postfix) with ESMTP id C88885CCE5D;
\r
22 Mon, 22 Sep 2014 11:39:26 +0200 (CEST)
\r
23 X-Virus-Scanned: IMAP STYX AMAVIS
\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])
\r
25 by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,
\r
27 with ESMTP id gltCzW4C67Ly; Mon, 22 Sep 2014 11:39:22 +0200 (CEST)
\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])
\r
29 by max.feld.cvut.cz (Postfix) with ESMTP id A1A995CCE66;
\r
30 Mon, 22 Sep 2014 11:39:22 +0200 (CEST)
\r
31 Received: from wsh by steelpick.2x.cz with local (Exim 4.84)
\r
32 (envelope-from <sojkam1@fel.cvut.cz>)
\r
33 id 1XW05U-0001wO-No; Mon, 22 Sep 2014 11:39:16 +0200
\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>
\r
35 To: notmuch@notmuchmail.org
\r
36 Subject: [PATCH 3/5] cli: Add support for parsing command line "flag" options
\r
37 Date: Mon, 22 Sep 2014 11:37:57 +0200
\r
38 Message-Id: <1411378679-7307-4-git-send-email-sojkam1@fel.cvut.cz>
\r
39 X-Mailer: git-send-email 2.1.0
\r
40 In-Reply-To: <1411378679-7307-1-git-send-email-sojkam1@fel.cvut.cz>
\r
41 References: <1411378679-7307-1-git-send-email-sojkam1@fel.cvut.cz>
\r
42 X-BeenThere: notmuch@notmuchmail.org
\r
43 X-Mailman-Version: 2.1.13
\r
45 List-Id: "Use and development of the notmuch mail system."
\r
46 <notmuch.notmuchmail.org>
\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
48 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
50 List-Post: <mailto:notmuch@notmuchmail.org>
\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
53 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
54 X-List-Received-Date: Mon, 22 Sep 2014 09:39:39 -0000
\r
56 This allows having multiple flags as a value of a command line
\r
57 option (e.g. --foo=bar,baz). The values of the given flags are OR'd
\r
60 This was inspired by a similar patch from Jani Nikula.
\r
62 command-line-arguments.c | 40 ++++++++++++++++++++++++++++++++++++++++
\r
63 command-line-arguments.h | 1 +
\r
64 test/Makefile.local | 2 +-
\r
65 test/T410-argument-parsing.sh | 3 ++-
\r
66 test/arg-test.c | 8 ++++++++
\r
67 5 files changed, 52 insertions(+), 2 deletions(-)
\r
69 diff --git a/command-line-arguments.c b/command-line-arguments.c
\r
70 index 844d6c3..50723e4 100644
\r
71 --- a/command-line-arguments.c
\r
72 +++ b/command-line-arguments.c
\r
75 #include "error_util.h"
\r
76 #include "command-line-arguments.h"
\r
77 +#include "string-util.h"
\r
80 Search the array of keywords for a given argument, assigning the
\r
81 @@ -36,6 +37,43 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
\r
86 + An arguments composed of comma-separated flags is parsed and the
\r
87 + output variable is assigned the ORed flag values. Return FALSE in
\r
90 +static notmuch_bool_t
\r
91 +_process_flags_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
\r
93 + const notmuch_keyword_t *keyword;
\r
94 + const char *flag = arg_str;
\r
96 + notmuch_bool_t match;
\r
98 + if (next == '\0' || *arg_str == '\0') {
\r
99 + /* No flag given */
\r
100 + fprintf (stderr, "Option \"%s\" needs an flags argument.\n", arg_desc->name);
\r
104 + while ((flag = strtok_len_c (flag + flen, ",", &flen))) {
\r
105 + for (keyword = arg_desc->keywords, match = FALSE; keyword->name; keyword++) {
\r
106 + if (strncmp (flag, keyword->name, flen) == 0 &&
\r
107 + flen == strlen (keyword->name)) {
\r
108 + *((int *)arg_desc->output_var) |= keyword->value;
\r
114 + fprintf (stderr, "Unknown flag argument \"%.*s\" for option \"%s\".\n", (int)flen, flag, arg_desc->name);
\r
122 static notmuch_bool_t
\r
123 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
\r
125 @@ -153,6 +191,8 @@ parse_option (const char *arg,
\r
126 switch (try->opt_type) {
\r
127 case NOTMUCH_OPT_KEYWORD:
\r
128 return _process_keyword_arg (try, next, value);
\r
129 + case NOTMUCH_OPT_FLAGS:
\r
130 + return _process_flags_arg (try, next, value);
\r
131 case NOTMUCH_OPT_BOOLEAN:
\r
132 return _process_boolean_arg (try, next, value);
\r
133 case NOTMUCH_OPT_INT:
\r
134 diff --git a/command-line-arguments.h b/command-line-arguments.h
\r
135 index de1734a..192ce06 100644
\r
136 --- a/command-line-arguments.h
\r
137 +++ b/command-line-arguments.h
\r
138 @@ -8,6 +8,7 @@ enum notmuch_opt_type {
\r
139 NOTMUCH_OPT_BOOLEAN, /* --verbose */
\r
140 NOTMUCH_OPT_INT, /* --frob=8 */
\r
141 NOTMUCH_OPT_KEYWORD, /* --format=raw|json|text */
\r
142 + NOTMUCH_OPT_FLAGS, /* --flags=name,addr,casefold */
\r
143 NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */
\r
144 NOTMUCH_OPT_POSITION /* notmuch dump pos_arg */
\r
146 diff --git a/test/Makefile.local b/test/Makefile.local
\r
147 index a2d58fc..efa3410 100644
\r
148 --- a/test/Makefile.local
\r
149 +++ b/test/Makefile.local
\r
150 @@ -13,7 +13,7 @@ smtp_dummy_srcs = \
\r
151 smtp_dummy_modules = $(smtp_dummy_srcs:.c=.o)
\r
153 $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a
\r
154 - $(call quiet,CC) $^ -o $@
\r
155 + $(call quiet,CC) $^ -o $@ -ltalloc
\r
157 $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a
\r
158 $(call quiet,CC) $^ $(TALLOC_LDFLAGS) -o $@
\r
159 diff --git a/test/T410-argument-parsing.sh b/test/T410-argument-parsing.sh
\r
160 index 94e9087..9c4cc84 100755
\r
161 --- a/test/T410-argument-parsing.sh
\r
162 +++ b/test/T410-argument-parsing.sh
\r
163 @@ -3,9 +3,10 @@ test_description="argument parsing"
\r
166 test_begin_subtest "sanity check"
\r
167 -$TEST_DIRECTORY/arg-test pos1 --keyword=one --string=foo pos2 --int=7 > OUTPUT
\r
168 +$TEST_DIRECTORY/arg-test pos1 --keyword=one --flags=one,two --string=foo pos2 --int=7 > OUTPUT
\r
169 cat <<EOF > EXPECTED
\r
174 positional arg 1 pos1
\r
175 diff --git a/test/arg-test.c b/test/arg-test.c
\r
176 index 6c49eac..0d75fe7 100644
\r
177 --- a/test/arg-test.c
\r
178 +++ b/test/arg-test.c
\r
179 @@ -7,6 +7,7 @@ int main(int argc, char **argv){
\r
185 char *pos_arg1=NULL;
\r
186 char *pos_arg2=NULL;
\r
187 @@ -17,6 +18,10 @@ int main(int argc, char **argv){
\r
188 (notmuch_keyword_t []){ { "one", 1 },
\r
191 + { NOTMUCH_OPT_FLAGS, &fl_val, "flags", 'f',
\r
192 + (notmuch_keyword_t []){ { "one", 1 << 0},
\r
193 + { "two", 1 << 1 },
\r
195 { NOTMUCH_OPT_INT, &int_val, "int", 'i', 0},
\r
196 { NOTMUCH_OPT_STRING, &string_val, "string", 's', 0},
\r
197 { NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0},
\r
198 @@ -31,6 +36,9 @@ int main(int argc, char **argv){
\r
200 printf("keyword %d\n", kw_val);
\r
203 + printf("flags %d\n", fl_val);
\r
206 printf("int %d\n", int_val);
\r