From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id EB1B7431FAE for ; Mon, 22 Sep 2014 02:39:38 -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 QJoXCiJXBwAd for ; Mon, 22 Sep 2014 02:39:32 -0700 (PDT) Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36]) by olra.theworths.org (Postfix) with ESMTP id 21AD5431FBC for ; Mon, 22 Sep 2014 02:39:28 -0700 (PDT) Received: from localhost (unknown [192.168.200.7]) by max.feld.cvut.cz (Postfix) with ESMTP id C88885CCE5D; Mon, 22 Sep 2014 11:39:26 +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 gltCzW4C67Ly; Mon, 22 Sep 2014 11:39:22 +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 A1A995CCE66; Mon, 22 Sep 2014 11:39:22 +0200 (CEST) Received: from wsh by steelpick.2x.cz with local (Exim 4.84) (envelope-from ) id 1XW05U-0001wO-No; Mon, 22 Sep 2014 11:39:16 +0200 From: Michal Sojka To: notmuch@notmuchmail.org Subject: [PATCH 3/5] cli: Add support for parsing command line "flag" options Date: Mon, 22 Sep 2014 11:37:57 +0200 Message-Id: <1411378679-7307-4-git-send-email-sojkam1@fel.cvut.cz> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1411378679-7307-1-git-send-email-sojkam1@fel.cvut.cz> References: <1411378679-7307-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: Mon, 22 Sep 2014 09:39:39 -0000 This allows having multiple flags as a value of a command line option (e.g. --foo=bar,baz). The values of the given flags are OR'd together. This was inspired by a similar patch from Jani Nikula. --- command-line-arguments.c | 40 ++++++++++++++++++++++++++++++++++++++++ command-line-arguments.h | 1 + test/Makefile.local | 2 +- test/T410-argument-parsing.sh | 3 ++- test/arg-test.c | 8 ++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/command-line-arguments.c b/command-line-arguments.c index 844d6c3..50723e4 100644 --- a/command-line-arguments.c +++ b/command-line-arguments.c @@ -3,6 +3,7 @@ #include #include "error_util.h" #include "command-line-arguments.h" +#include "string-util.h" /* Search the array of keywords for a given argument, assigning the @@ -36,6 +37,43 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char return FALSE; } +/* + An arguments composed of comma-separated flags is parsed and the + output variable is assigned the ORed flag values. Return FALSE in + case of error. +*/ +static notmuch_bool_t +_process_flags_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) { + + const notmuch_keyword_t *keyword; + const char *flag = arg_str; + size_t flen = 0; + notmuch_bool_t match; + + if (next == '\0' || *arg_str == '\0') { + /* No flag given */ + fprintf (stderr, "Option \"%s\" needs an flags argument.\n", arg_desc->name); + return FALSE; + } + + while ((flag = strtok_len_c (flag + flen, ",", &flen))) { + for (keyword = arg_desc->keywords, match = FALSE; keyword->name; keyword++) { + if (strncmp (flag, keyword->name, flen) == 0 && + flen == strlen (keyword->name)) { + *((int *)arg_desc->output_var) |= keyword->value; + match = TRUE; + break; + } + } + if (! match) { + fprintf (stderr, "Unknown flag argument \"%.*s\" for option \"%s\".\n", (int)flen, flag, arg_desc->name); + return FALSE; + } + } + return TRUE; +} + + static notmuch_bool_t _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) { @@ -153,6 +191,8 @@ parse_option (const char *arg, switch (try->opt_type) { case NOTMUCH_OPT_KEYWORD: return _process_keyword_arg (try, next, value); + case NOTMUCH_OPT_FLAGS: + return _process_flags_arg (try, next, value); case NOTMUCH_OPT_BOOLEAN: return _process_boolean_arg (try, next, value); case NOTMUCH_OPT_INT: diff --git a/command-line-arguments.h b/command-line-arguments.h index de1734a..192ce06 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_FLAGS, /* --flags=name,addr,casefold */ NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */ NOTMUCH_OPT_POSITION /* notmuch dump pos_arg */ }; diff --git a/test/Makefile.local b/test/Makefile.local index a2d58fc..efa3410 100644 --- a/test/Makefile.local +++ b/test/Makefile.local @@ -13,7 +13,7 @@ smtp_dummy_srcs = \ smtp_dummy_modules = $(smtp_dummy_srcs:.c=.o) $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a - $(call quiet,CC) $^ -o $@ + $(call quiet,CC) $^ -o $@ -ltalloc $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a $(call quiet,CC) $^ $(TALLOC_LDFLAGS) -o $@ diff --git a/test/T410-argument-parsing.sh b/test/T410-argument-parsing.sh index 94e9087..9c4cc84 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 --flags=one,two --string=foo pos2 --int=7 > OUTPUT cat < EXPECTED keyword 1 +flags 3 int 7 string foo positional arg 1 pos1 diff --git a/test/arg-test.c b/test/arg-test.c index 6c49eac..0d75fe7 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,10 @@ int main(int argc, char **argv){ (notmuch_keyword_t []){ { "one", 1 }, { "two", 2 }, { 0, 0 } } }, + { NOTMUCH_OPT_FLAGS, &fl_val, "flags", 'f', + (notmuch_keyword_t []){ { "one", 1 << 0}, + { "two", 1 << 1 }, + { 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 +36,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.0