unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Michal Sojka <sojkam1@fel.cvut.cz>
To: notmuch@notmuchmail.org
Subject: [PATCH v3 2/4] cli: Add support for parsing multiple keyword arguments
Date: Sun, 12 Oct 2014 23:41:31 +0200	[thread overview]
Message-ID: <1413150093-8383-3-git-send-email-sojkam1@fel.cvut.cz> (raw)
In-Reply-To: <1413150093-8383-1-git-send-email-sojkam1@fel.cvut.cz>

From: Jani Nikula <jani@nikula.org>

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 <<EOF > 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

  parent reply	other threads:[~2014-10-12 21:41 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-22  9:37 [PATCH 0/5] notmuch search --output=addresses Michal Sojka
2014-09-22  9:37 ` [PATCH 1/5] cli: Refactor option passing in the search command Michal Sojka
2014-09-25 19:13   ` Tomi Ollila
2014-09-25 20:48     ` Michal Sojka
2014-09-29 16:04       ` Tomi Ollila
2014-09-29 17:28       ` Jani Nikula
2014-10-05 20:51         ` [PATCH v2 0/4] notmuch search --output=addresses Michal Sojka
2014-10-05 20:51           ` [PATCH v2 1/4] cli: Refactor option passing in the search command Michal Sojka
2014-10-05 20:51           ` [PATCH v2 2/4] cli: Extend the search command for --output=addresses and similar Michal Sojka
2014-10-06 18:56             ` Tomi Ollila
2014-10-09 10:55               ` Michal Sojka
2014-10-12 21:41                 ` [PATCH v3 0/4] notmuch search --output=sender/recipients Michal Sojka
2014-10-12 21:41                   ` [PATCH v3 1/4] cli: Refactor option passing in the search command Michal Sojka
2014-10-22 10:44                     ` Mark Walters
2014-10-12 21:41                   ` Michal Sojka [this message]
2014-10-12 21:41                   ` [PATCH v3 3/4] cli: Extend the search command for --output={sender, recipients} Michal Sojka
2014-10-13 19:00                     ` Tomi Ollila
2014-10-13 21:38                       ` Michal Sojka
2014-10-19 10:02                         ` Tomi Ollila
2014-10-22 10:51                     ` Mark Walters
2014-10-24 10:57                       ` Michal Sojka
2014-10-23  9:41                     ` Mark Walters
2014-10-24  9:38                       ` Tomi Ollila
2014-10-24 10:23                       ` David Edmondson
2014-10-12 21:41                   ` [PATCH v3 4/4] cli: Add an option to filter our duplicate addresses Michal Sojka
2014-10-05 20:51           ` [PATCH v2 3/4] cli: Add support for parsing multiple keyword arguments Michal Sojka
2014-10-05 20:51           ` [PATCH v2 4/4] cli: Add configurable address deduplication for --output=addresses Michal Sojka
2014-09-22  9:37 ` [PATCH 2/5] cli: Extend the search command for --output=addresses and similar Michal Sojka
2014-09-22  9:37 ` [PATCH 3/5] cli: Add support for parsing command line "flag" options Michal Sojka
2014-09-22  9:37 ` [PATCH 4/5] cli: Add configurable address deduplication for --output=addresses Michal Sojka
2014-09-22  9:37 ` [PATCH 5/5] cli: Add tests for 'search --output=addresses' and similar Michal Sojka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1413150093-8383-3-git-send-email-sojkam1@fel.cvut.cz \
    --to=sojkam1@fel.cvut.cz \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).