[PATCH 1/3] cli: allow keyword lists in argument parser.

Mark Walters markwalters1009 at gmail.com
Sat Jul 7 08:12:56 PDT 2012


We allow keyword lists in the argument parser. The parser returns a
bitfield of the arguments present which is the union of the specified
bitfields for each option. Since it is the union the parser could
allow things like

--output-headers=default,reply-to

to get the default headers with reply-to added.
---
 command-line-arguments.c |   47 ++++++++++++++++++++++++++++++++++++++++++++++
 command-line-arguments.h |    3 +-
 2 files changed, 49 insertions(+), 1 deletions(-)

diff --git a/command-line-arguments.c b/command-line-arguments.c
index b0a0dab..d322f9e 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -37,6 +37,50 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
 }
 
 static notmuch_bool_t
+_process_keyword_list (const notmuch_opt_desc_t *arg_desc, const char *arg_str) {
+
+    char *key_str, *final;
+    unsigned int matched_keys = 0;
+    notmuch_bool_t matched;
+    const notmuch_keyword_t *keywords;
+
+    key_str = strdup (arg_str);
+
+    do {
+	keywords = arg_desc->keywords;
+	matched = FALSE;
+
+	final = strrchr (key_str, ',');
+
+	if (final) {
+	    *final = '\0';
+	    final++;
+	} else {
+	    final = key_str;
+	}
+
+	while (keywords->name && !matched) {
+	    if (strcmp (final, keywords->name) == 0) {
+		matched_keys |= keywords->value;
+		matched = TRUE;
+	    }
+	    keywords++;
+	}
+	if (!matched) {
+	    fprintf (stderr, "unknown keyword: \'%s\' in list %s\n", final, arg_str);
+	    goto DONE;
+	}
+    } while (final != key_str);
+
+    if (arg_desc->output_var) {
+	*((unsigned int *)arg_desc->output_var) = matched_keys;
+    }
+DONE:
+    free (key_str);
+    return matched;
+}
+
+static notmuch_bool_t
 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
 
     if (next == 0) {
@@ -121,6 +165,9 @@ parse_option (const char *arg,
 	    case NOTMUCH_OPT_KEYWORD:
 		return _process_keyword_arg (try, next, value);
 		break;
+	    case NOTMUCH_OPT_KEYWORD_LIST:
+		return _process_keyword_list (try, value);
+		break;
 	    case NOTMUCH_OPT_BOOLEAN:
 		return _process_boolean_arg (try, next, value);
 		break;
diff --git a/command-line-arguments.h b/command-line-arguments.h
index de1734a..f2b2275 100644
--- a/command-line-arguments.h
+++ b/command-line-arguments.h
@@ -9,7 +9,8 @@ enum notmuch_opt_type {
     NOTMUCH_OPT_INT,		/* --frob=8               */
     NOTMUCH_OPT_KEYWORD,	/* --format=raw|json|text */
     NOTMUCH_OPT_STRING,		/* --file=/tmp/gnarf.txt  */
-    NOTMUCH_OPT_POSITION	/* notmuch dump pos_arg   */
+    NOTMUCH_OPT_POSITION,	/* notmuch dump pos_arg   */
+    NOTMUCH_OPT_KEYWORD_LIST	/* --output=header,body   */
 };
 
 /*
-- 
1.7.9.1



More information about the notmuch mailing list