From 3f94cf6a2c0a9e9e6ea5f18f4e045ec1975a10f6 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Sun, 5 Oct 2014 22:51:58 +0200 Subject: [PATCH] [PATCH v2 3/4] cli: Add support for parsing multiple keyword arguments --- 0f/0c43d0931b38b9fa1f2e9cf1e3bd14a867d374 | 159 ++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 0f/0c43d0931b38b9fa1f2e9cf1e3bd14a867d374 diff --git a/0f/0c43d0931b38b9fa1f2e9cf1e3bd14a867d374 b/0f/0c43d0931b38b9fa1f2e9cf1e3bd14a867d374 new file mode 100644 index 000000000..30067d77c --- /dev/null +++ b/0f/0c43d0931b38b9fa1f2e9cf1e3bd14a867d374 @@ -0,0 +1,159 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 19A9B431FBC + for ; Sun, 5 Oct 2014 13:52:46 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -2.3 +X-Spam-Level: +X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 7dYPN8j6gBGo for ; + Sun, 5 Oct 2014 13:52:44 -0700 (PDT) +Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36]) + by olra.theworths.org (Postfix) with ESMTP id E617B431FBF + for ; Sun, 5 Oct 2014 13:52:38 -0700 (PDT) +Received: from localhost (unknown [192.168.200.7]) + by max.feld.cvut.cz (Postfix) with ESMTP id 4F88B3CFE94; + Sun, 5 Oct 2014 22:52:36 +0200 (CEST) +X-Virus-Scanned: IMAP STYX AMAVIS +Received: from max.feld.cvut.cz ([192.168.200.1]) + by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new, + port 10044) + with ESMTP id T4ayZKUsuqzQ; Sun, 5 Oct 2014 22:52:32 +0200 (CEST) +Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34]) + by max.feld.cvut.cz (Postfix) with ESMTP id 9B33A3CFE9A; + Sun, 5 Oct 2014 22:52:31 +0200 (CEST) +Received: from wsh by steelpick.2x.cz with local (Exim 4.84) + (envelope-from ) + id 1Xasn2-0005E8-SD; Sun, 05 Oct 2014 22:52:24 +0200 +From: Michal Sojka +To: notmuch@notmuchmail.org +Subject: [PATCH v2 3/4] cli: Add support for parsing multiple keyword + arguments +Date: Sun, 5 Oct 2014 22:51:58 +0200 +Message-Id: <1412542319-20017-4-git-send-email-sojkam1@fel.cvut.cz> +X-Mailer: git-send-email 2.1.1 +In-Reply-To: <1412542319-20017-1-git-send-email-sojkam1@fel.cvut.cz> +References: <874mvqxrnp.fsf@nikula.org> + <1412542319-20017-1-git-send-email-sojkam1@fel.cvut.cz> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sun, 05 Oct 2014 20:52:46 -0000 + +From: Jani Nikula + +This allows having multiple --foo=bar --foo=baz options on the command +line, with the corresponding values OR'd together. + +[Test added by Michal Sojka] +--- + command-line-arguments.c | 6 +++++- + command-line-arguments.h | 1 + + test/T410-argument-parsing.sh | 3 ++- + test/arg-test.c | 9 +++++++++ + 4 files changed, 17 insertions(+), 2 deletions(-) + +diff --git a/command-line-arguments.c b/command-line-arguments.c +index 844d6c3..c6f7269 100644 +--- a/command-line-arguments.c ++++ b/command-line-arguments.c +@@ -23,7 +23,10 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char + while (keywords->name) { + if (strcmp (arg_str, keywords->name) == 0) { + if (arg_desc->output_var) { +- *((int *)arg_desc->output_var) = keywords->value; ++ if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS) ++ *((int *)arg_desc->output_var) |= keywords->value; ++ else ++ *((int *)arg_desc->output_var) = keywords->value; + } + return TRUE; + } +@@ -152,6 +155,7 @@ parse_option (const char *arg, + + switch (try->opt_type) { + case NOTMUCH_OPT_KEYWORD: ++ case NOTMUCH_OPT_KEYWORD_FLAGS: + return _process_keyword_arg (try, next, value); + case NOTMUCH_OPT_BOOLEAN: + return _process_boolean_arg (try, next, value); +diff --git a/command-line-arguments.h b/command-line-arguments.h +index de1734a..085a492 100644 +--- a/command-line-arguments.h ++++ b/command-line-arguments.h +@@ -8,6 +8,7 @@ enum notmuch_opt_type { + NOTMUCH_OPT_BOOLEAN, /* --verbose */ + NOTMUCH_OPT_INT, /* --frob=8 */ + NOTMUCH_OPT_KEYWORD, /* --format=raw|json|text */ ++ NOTMUCH_OPT_KEYWORD_FLAGS, /* the above with values OR'd together */ + NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */ + NOTMUCH_OPT_POSITION /* notmuch dump pos_arg */ + }; +diff --git a/test/T410-argument-parsing.sh b/test/T410-argument-parsing.sh +index 94e9087..2e5d7ae 100755 +--- a/test/T410-argument-parsing.sh ++++ b/test/T410-argument-parsing.sh +@@ -3,9 +3,10 @@ test_description="argument parsing" + . ./test-lib.sh + + test_begin_subtest "sanity check" +-$TEST_DIRECTORY/arg-test pos1 --keyword=one --string=foo pos2 --int=7 > OUTPUT ++$TEST_DIRECTORY/arg-test pos1 --keyword=one --string=foo pos2 --int=7 --flag=one --flag=three > OUTPUT + cat < EXPECTED + keyword 1 ++flags 5 + int 7 + string foo + positional arg 1 pos1 +diff --git a/test/arg-test.c b/test/arg-test.c +index 6c49eac..736686d 100644 +--- a/test/arg-test.c ++++ b/test/arg-test.c +@@ -7,6 +7,7 @@ int main(int argc, char **argv){ + int opt_index=1; + + int kw_val=0; ++ int fl_val=0; + int int_val=0; + char *pos_arg1=NULL; + char *pos_arg2=NULL; +@@ -17,6 +18,11 @@ int main(int argc, char **argv){ + (notmuch_keyword_t []){ { "one", 1 }, + { "two", 2 }, + { 0, 0 } } }, ++ { NOTMUCH_OPT_KEYWORD_FLAGS, &fl_val, "flag", 'f', ++ (notmuch_keyword_t []){ { "one", 1 << 0}, ++ { "two", 1 << 1 }, ++ { "three", 1 << 2 }, ++ { 0, 0 } } }, + { NOTMUCH_OPT_INT, &int_val, "int", 'i', 0}, + { NOTMUCH_OPT_STRING, &string_val, "string", 's', 0}, + { NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0}, +@@ -31,6 +37,9 @@ int main(int argc, char **argv){ + if (kw_val) + printf("keyword %d\n", kw_val); + ++ if (fl_val) ++ printf("flags %d\n", fl_val); ++ + if (int_val) + printf("int %d\n", int_val); + +-- +2.1.1 + -- 2.26.2