--- /dev/null
+Return-Path: <sojkam1@fel.cvut.cz>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id EB1B7431FAE\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:38 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id QJoXCiJXBwAd for <notmuch@notmuchmail.org>;\r
+ Mon, 22 Sep 2014 02:39:32 -0700 (PDT)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 21AD5431FBC\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:28 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id C88885CCE5D;\r
+ Mon, 22 Sep 2014 11:39:26 +0200 (CEST)\r
+X-Virus-Scanned: IMAP STYX AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+ by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
+ port 10044)\r
+ with ESMTP id gltCzW4C67Ly; Mon, 22 Sep 2014 11:39:22 +0200 (CEST)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id A1A995CCE66;\r
+ Mon, 22 Sep 2014 11:39:22 +0200 (CEST)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1XW05U-0001wO-No; Mon, 22 Sep 2014 11:39:16 +0200\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 3/5] cli: Add support for parsing command line "flag" options\r
+Date: Mon, 22 Sep 2014 11:37:57 +0200\r
+Message-Id: <1411378679-7307-4-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1411378679-7307-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1411378679-7307-1-git-send-email-sojkam1@fel.cvut.cz>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\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: Mon, 22 Sep 2014 09:39:39 -0000\r
+\r
+This allows having multiple flags as a value of a command line\r
+option (e.g. --foo=bar,baz). The values of the given flags are OR'd\r
+together.\r
+\r
+This was inspired by a similar patch from Jani Nikula.\r
+---\r
+ command-line-arguments.c | 40 ++++++++++++++++++++++++++++++++++++++++\r
+ command-line-arguments.h | 1 +\r
+ test/Makefile.local | 2 +-\r
+ test/T410-argument-parsing.sh | 3 ++-\r
+ test/arg-test.c | 8 ++++++++\r
+ 5 files changed, 52 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/command-line-arguments.c b/command-line-arguments.c\r
+index 844d6c3..50723e4 100644\r
+--- a/command-line-arguments.c\r
++++ b/command-line-arguments.c\r
+@@ -3,6 +3,7 @@\r
+ #include <stdio.h>\r
+ #include "error_util.h"\r
+ #include "command-line-arguments.h"\r
++#include "string-util.h"\r
+ \r
+ /*\r
+ Search the array of keywords for a given argument, assigning the\r
+@@ -36,6 +37,43 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char\r
+ return FALSE;\r
+ }\r
+ \r
++/*\r
++ An arguments composed of comma-separated flags is parsed and the\r
++ output variable is assigned the ORed flag values. Return FALSE in\r
++ case of error.\r
++*/\r
++static notmuch_bool_t\r
++_process_flags_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {\r
++\r
++ const notmuch_keyword_t *keyword;\r
++ const char *flag = arg_str;\r
++ size_t flen = 0;\r
++ notmuch_bool_t match;\r
++\r
++ if (next == '\0' || *arg_str == '\0') {\r
++ /* No flag given */\r
++ fprintf (stderr, "Option \"%s\" needs an flags argument.\n", arg_desc->name);\r
++ return FALSE;\r
++ }\r
++\r
++ while ((flag = strtok_len_c (flag + flen, ",", &flen))) {\r
++ for (keyword = arg_desc->keywords, match = FALSE; keyword->name; keyword++) {\r
++ if (strncmp (flag, keyword->name, flen) == 0 &&\r
++ flen == strlen (keyword->name)) {\r
++ *((int *)arg_desc->output_var) |= keyword->value;\r
++ match = TRUE;\r
++ break;\r
++ }\r
++ }\r
++ if (! match) {\r
++ fprintf (stderr, "Unknown flag argument \"%.*s\" for option \"%s\".\n", (int)flen, flag, arg_desc->name);\r
++ return FALSE;\r
++ }\r
++ }\r
++ return TRUE;\r
++}\r
++\r
++\r
+ static notmuch_bool_t\r
+ _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {\r
+ \r
+@@ -153,6 +191,8 @@ parse_option (const char *arg,\r
+ switch (try->opt_type) {\r
+ case NOTMUCH_OPT_KEYWORD:\r
+ return _process_keyword_arg (try, next, value);\r
++ case NOTMUCH_OPT_FLAGS:\r
++ return _process_flags_arg (try, next, value);\r
+ case NOTMUCH_OPT_BOOLEAN:\r
+ return _process_boolean_arg (try, next, value);\r
+ case NOTMUCH_OPT_INT:\r
+diff --git a/command-line-arguments.h b/command-line-arguments.h\r
+index de1734a..192ce06 100644\r
+--- a/command-line-arguments.h\r
++++ b/command-line-arguments.h\r
+@@ -8,6 +8,7 @@ enum notmuch_opt_type {\r
+ NOTMUCH_OPT_BOOLEAN, /* --verbose */\r
+ NOTMUCH_OPT_INT, /* --frob=8 */\r
+ NOTMUCH_OPT_KEYWORD, /* --format=raw|json|text */\r
++ NOTMUCH_OPT_FLAGS, /* --flags=name,addr,casefold */\r
+ NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */\r
+ NOTMUCH_OPT_POSITION /* notmuch dump pos_arg */\r
+ };\r
+diff --git a/test/Makefile.local b/test/Makefile.local\r
+index a2d58fc..efa3410 100644\r
+--- a/test/Makefile.local\r
++++ b/test/Makefile.local\r
+@@ -13,7 +13,7 @@ smtp_dummy_srcs = \\r
+ smtp_dummy_modules = $(smtp_dummy_srcs:.c=.o)\r
+ \r
+ $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a\r
+- $(call quiet,CC) $^ -o $@\r
++ $(call quiet,CC) $^ -o $@ -ltalloc\r
+ \r
+ $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a\r
+ $(call quiet,CC) $^ $(TALLOC_LDFLAGS) -o $@\r
+diff --git a/test/T410-argument-parsing.sh b/test/T410-argument-parsing.sh\r
+index 94e9087..9c4cc84 100755\r
+--- a/test/T410-argument-parsing.sh\r
++++ b/test/T410-argument-parsing.sh\r
+@@ -3,9 +3,10 @@ test_description="argument parsing"\r
+ . ./test-lib.sh\r
+ \r
+ test_begin_subtest "sanity check"\r
+-$TEST_DIRECTORY/arg-test pos1 --keyword=one --string=foo pos2 --int=7 > OUTPUT\r
++$TEST_DIRECTORY/arg-test pos1 --keyword=one --flags=one,two --string=foo pos2 --int=7 > OUTPUT\r
+ cat <<EOF > EXPECTED\r
+ keyword 1\r
++flags 3\r
+ int 7\r
+ string foo\r
+ positional arg 1 pos1\r
+diff --git a/test/arg-test.c b/test/arg-test.c\r
+index 6c49eac..0d75fe7 100644\r
+--- a/test/arg-test.c\r
++++ b/test/arg-test.c\r
+@@ -7,6 +7,7 @@ int main(int argc, char **argv){\r
+ int opt_index=1;\r
+ \r
+ int kw_val=0;\r
++ int fl_val=0;\r
+ int int_val=0;\r
+ char *pos_arg1=NULL;\r
+ char *pos_arg2=NULL;\r
+@@ -17,6 +18,10 @@ int main(int argc, char **argv){\r
+ (notmuch_keyword_t []){ { "one", 1 },\r
+ { "two", 2 },\r
+ { 0, 0 } } },\r
++ { NOTMUCH_OPT_FLAGS, &fl_val, "flags", 'f',\r
++ (notmuch_keyword_t []){ { "one", 1 << 0},\r
++ { "two", 1 << 1 },\r
++ { 0, 0 } } },\r
+ { NOTMUCH_OPT_INT, &int_val, "int", 'i', 0},\r
+ { NOTMUCH_OPT_STRING, &string_val, "string", 's', 0},\r
+ { NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0},\r
+@@ -31,6 +36,9 @@ int main(int argc, char **argv){\r
+ if (kw_val)\r
+ printf("keyword %d\n", kw_val);\r
+ \r
++ if (fl_val)\r
++ printf("flags %d\n", fl_val);\r
++\r
+ if (int_val)\r
+ printf("int %d\n", int_val);\r
+ \r
+-- \r
+2.1.0\r
+\r