[PATCH 2/3] command-line-arguments: support keyword arguments with default value

Jani Nikula jani at nikula.org
Fri Mar 9 14:33:29 PST 2012


Add NOTMUCH_OPT_KEYWORD_DEFAULT to support plain --arg in addition to
--arg=value. The value to use is the first in the list of keywords.

Signed-off-by: Jani Nikula <jani at nikula.org>
---
 command-line-arguments.c |   11 ++++++++++-
 command-line-arguments.h |    1 +
 2 files changed, 11 insertions(+), 1 deletions(-)

diff --git a/command-line-arguments.c b/command-line-arguments.c
index 1bdb881..2a3646f 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -15,6 +15,13 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, const char *arg_str) {
 
     const notmuch_keyword_t *keywords = arg_desc->keywords;
 
+    if (arg_str == NULL) {
+	/* no keyword specified, use the first keyword as default */
+	if (arg_desc->output_var)
+	    *((int *)arg_desc->output_var) = keywords->value;
+	return TRUE;
+    }
+
     while (keywords->name) {
 	if (strcmp (arg_str, keywords->name) == 0) {
 	    if (arg_desc->output_var) {
@@ -105,7 +112,8 @@ parse_option (const char *arg,
 		    return FALSE;
 	    } else if (next == '\0') {
 		value = NULL;
-		if (try->opt_type != NOTMUCH_OPT_BOOLEAN)
+		if (try->opt_type != NOTMUCH_OPT_BOOLEAN &&
+		    try->opt_type != NOTMUCH_OPT_KEYWORD_DEFAULT)
 		    return FALSE;
 	    } else {
 		return FALSE;
@@ -116,6 +124,7 @@ parse_option (const char *arg,
 
 	    switch (try->opt_type) {
 	    case NOTMUCH_OPT_KEYWORD:
+	    case NOTMUCH_OPT_KEYWORD_DEFAULT:
 		return _process_keyword_arg (try, value);
 		break;
 	    case NOTMUCH_OPT_BOOLEAN:
diff --git a/command-line-arguments.h b/command-line-arguments.h
index de1734a..d70c84c 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_DEFAULT,/* --format[=raw|json]    */
     NOTMUCH_OPT_STRING,		/* --file=/tmp/gnarf.txt  */
     NOTMUCH_OPT_POSITION	/* notmuch dump pos_arg   */
 };
-- 
1.7.5.4



More information about the notmuch mailing list