[PATCH 1/3] command-line-arguments: allow true and false keywords for booleans

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


Add support for --arg=true and --arg=false for NOTMUCH_OPT_BOOLEAN
arguments to be able to disable a boolean argument. Plain --arg
remains unchanged, meaning true.

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

diff --git a/command-line-arguments.c b/command-line-arguments.c
index e711414..1bdb881 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -28,6 +28,27 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, const char *arg_str) {
     return FALSE;
 }
 
+static notmuch_bool_t
+_process_boolean_arg (const notmuch_opt_desc_t *arg_desc, const char *arg_str)
+{
+    notmuch_bool_t value = TRUE;
+
+    if (arg_str) {
+	if (strcmp (arg_str, "true") == 0) {
+	    value = TRUE;
+	} else if (strcmp (arg_str, "false") == 0) {
+	    value = FALSE;
+	} else {
+	    fprintf (stderr, "unknown boolean: %s\n", arg_str);
+	    return FALSE;
+	}
+    }
+
+    *((notmuch_bool_t *)arg_desc->output_var) = value;
+
+    return TRUE;
+}
+
 /*
    Search for the {pos_arg_index}th position argument, return FALSE if
    that does not exist.
@@ -79,11 +100,15 @@ parse_option (const char *arg,
 	     * delimiter, and a non-zero length value
 	     */
 
-	    if (try->opt_type != NOTMUCH_OPT_BOOLEAN) {
-		if (next != '=' && next != ':') return FALSE;
-		if (value[0] == 0) return FALSE;
+	    if (next == '=' || next == ':') {
+		if (value[0] == '\0')
+		    return FALSE;
+	    } else if (next == '\0') {
+		value = NULL;
+		if (try->opt_type != NOTMUCH_OPT_BOOLEAN)
+		    return FALSE;
 	    } else {
-		if (next != 0) return FALSE;
+		return FALSE;
 	    }
 
 	    if (try->output_var == NULL)
@@ -94,8 +119,7 @@ parse_option (const char *arg,
 		return _process_keyword_arg (try, value);
 		break;
 	    case NOTMUCH_OPT_BOOLEAN:
-		*((notmuch_bool_t *)try->output_var) = TRUE;
-		return TRUE;
+		return _process_boolean_arg (try, value);
 		break;
 	    case NOTMUCH_OPT_INT:
 		*((int *)try->output_var) = strtol (value, &endptr, 10);
-- 
1.7.5.4



More information about the notmuch mailing list