[PATCH 6/9] cli: add support for --no- prefixed boolean and keyword flag arguments

Jani Nikula jani at nikula.org
Tue Sep 19 13:39:26 PDT 2017


Add transparent support for negating boolean and keyword flag
arguments using --no-argument style on the command line. That is, if
the option description contains a boolean or a keyword flag argument
named "argument", --no-argument will match and negate it.

For boolean arguments this obviously means the logical NOT. For
keyword flag arguments this means bitwise AND of the bitwise NOT,
i.e. masking out the specified bits instead of OR'ing them in.

For example, you can use --no-exclude instead of --exclude=false in
notmuch show. If we had keyword flag arguments with some flags
defaulting to on, say --include=tags in notmuch dump/restore, this
would allow --no-include=tags to switch that off while not affecting
other flags.

As a curiosity, you should be able to warp your brain using
--no-exclude=true meaning false and --no-exclude=false meaning true if
you wish.

Specifying both "argument" and "no-argument" style arguments in the
same option description should be avoided. In this case, --no-argument
would match whichever is specified first, and --argument would only
match "argument".
---
 command-line-arguments.c | 47 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 10 deletions(-)

diff --git a/command-line-arguments.c b/command-line-arguments.c
index a79afcaf8a15..d61d345285b9 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -11,7 +11,8 @@
 */
 
 static notmuch_bool_t
-_process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
+_process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next,
+		      const char *arg_str, notmuch_bool_t negate) {
 
     const notmuch_keyword_t *keywords;
 
@@ -25,7 +26,9 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
 	    continue;
 
 	if (arg_desc->output_var) {
-	    if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS)
+	    if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS && negate)
+		*((int *)arg_desc->output_var) &= ~keywords->value;
+	    else if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS)
 		*((int *)arg_desc->output_var) |= keywords->value;
 	    else
 		*((int *)arg_desc->output_var) = keywords->value;
@@ -41,7 +44,8 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
 }
 
 static notmuch_bool_t
-_process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
+_process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
+		      const char *arg_str, notmuch_bool_t negate) {
     notmuch_bool_t value;
 
     if (next == '\0' || strcmp (arg_str, "true") == 0) {
@@ -53,7 +57,7 @@ _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
 	return FALSE;
     }
 
-    *((notmuch_bool_t *)arg_desc->output_var) = value;
+    *((notmuch_bool_t *)arg_desc->output_var) = negate ? !value : value;
 
     return TRUE;
 }
@@ -116,6 +120,8 @@ parse_position_arg (const char *arg_str, int pos_arg_index,
     return FALSE;
 }
 
+#define NEGATIVE_PREFIX "no-"
+
 /*
  * Search for a non-positional (i.e. starting with --) argument matching arg,
  * parse a possible value, and assign to *output_var
@@ -132,6 +138,14 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
     assert(options);
 
     const char *arg = _arg + 2; /* _arg starts with -- */
+    const char *negative_arg = NULL;
+
+    /* See if this is a --no-argument */
+    if (strlen (arg) > strlen (NEGATIVE_PREFIX) &&
+	strncmp (arg, NEGATIVE_PREFIX, strlen (NEGATIVE_PREFIX)) == 0) {
+	negative_arg = arg + strlen (NEGATIVE_PREFIX);
+    }
+
     const notmuch_opt_desc_t *try;
 
     const char *next_arg = NULL;
@@ -148,11 +162,24 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
 	if (! try->name)
 	    continue;
 
-	if (strncmp (arg, try->name, strlen (try->name)) != 0)
+	char next;
+	const char *value;
+	notmuch_bool_t negate = FALSE;
+
+	if (strncmp (arg, try->name, strlen (try->name)) == 0) {
+	    next = arg[strlen (try->name)];
+	    value = arg + strlen (try->name) + 1;
+	} else if (negative_arg &&
+		   strncmp (negative_arg, try->name, strlen (try->name)) == 0 &&
+		   (try->opt_type == NOTMUCH_OPT_BOOLEAN ||
+		    try->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS)) {
+	    next = negative_arg[strlen (try->name)];
+	    value = negative_arg + strlen (try->name) + 1;
+	    /* The argument part of --no-argument matches, negate the result. */
+	    negate = TRUE;
+	} else {
 	    continue;
-
-	char next = arg[strlen (try->name)];
-	const char *value = arg + strlen(try->name) + 1;
+	}
 
 	/*
 	 * If we have not reached the end of the argument (i.e. the
@@ -176,10 +203,10 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
 	switch (try->opt_type) {
 	case NOTMUCH_OPT_KEYWORD:
 	case NOTMUCH_OPT_KEYWORD_FLAGS:
-	    opt_status = _process_keyword_arg (try, next, value);
+	    opt_status = _process_keyword_arg (try, next, value, negate);
 	    break;
 	case NOTMUCH_OPT_BOOLEAN:
-	    opt_status = _process_boolean_arg (try, next, value);
+	    opt_status = _process_boolean_arg (try, next, value, negate);
 	    break;
 	case NOTMUCH_OPT_INT:
 	    opt_status = _process_int_arg (try, next, value);
-- 
2.11.0



More information about the notmuch mailing list