]> rtime.felk.cvut.cz Git - notmuch.git/blobdiff - command-line-arguments.c
cli: Add support for parsing command line "flag" options
[notmuch.git] / command-line-arguments.c
index 844d6c3d18bf9db03be5368cbd20c66cc9e058b9..50723e424ce87d534031f3c897c7035e748c34ad 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include "error_util.h"
 #include "command-line-arguments.h"
+#include "string-util.h"
 
 /*
   Search the array of keywords for a given argument, assigning the
@@ -36,6 +37,43 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
     return FALSE;
 }
 
+/*
+  An arguments composed of comma-separated flags is parsed and the
+  output variable is assigned the ORed flag values. Return FALSE in
+  case of error.
+*/
+static notmuch_bool_t
+_process_flags_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
+
+    const notmuch_keyword_t *keyword;
+    const char *flag = arg_str;
+    size_t flen = 0;
+    notmuch_bool_t match;
+
+    if (next == '\0' || *arg_str == '\0') {
+       /* No flag given */
+       fprintf (stderr, "Option \"%s\" needs an flags argument.\n", arg_desc->name);
+       return FALSE;
+    }
+
+    while ((flag = strtok_len_c (flag + flen, ",", &flen))) {
+       for (keyword = arg_desc->keywords, match = FALSE; keyword->name; keyword++) {
+           if (strncmp (flag, keyword->name, flen) == 0 &&
+               flen == strlen (keyword->name)) {
+               *((int *)arg_desc->output_var) |= keyword->value;
+               match = TRUE;
+               break;
+           }
+       }
+       if (! match) {
+           fprintf (stderr, "Unknown flag argument \"%.*s\" for option \"%s\".\n", (int)flen, flag, arg_desc->name);
+           return FALSE;
+       }
+    }
+    return TRUE;
+}
+
+
 static notmuch_bool_t
 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
 
@@ -153,6 +191,8 @@ parse_option (const char *arg,
        switch (try->opt_type) {
        case NOTMUCH_OPT_KEYWORD:
            return _process_keyword_arg (try, next, value);
+       case NOTMUCH_OPT_FLAGS:
+           return _process_flags_arg (try, next, value);
        case NOTMUCH_OPT_BOOLEAN:
            return _process_boolean_arg (try, next, value);
        case NOTMUCH_OPT_INT: