]> rtime.felk.cvut.cz Git - notmuch.git/commitdiff
cli: Add support for parsing command line "flag" options
authorMichal Sojka <sojkam1@fel.cvut.cz>
Sun, 21 Sep 2014 18:37:57 +0000 (20:37 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 22 Sep 2014 08:36:36 +0000 (10:36 +0200)
This allows having multiple flags as a value of a command line
option (e.g. --foo=bar,baz). The values of the given flags are OR'd
together.

This was inspired by a similar patch from Jani Nikula.

command-line-arguments.c
command-line-arguments.h
test/Makefile.local
test/T410-argument-parsing.sh
test/arg-test.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:
index de1734ad2fdb3f0c5444c73abc5f74b7cbd99cf2..192ce06410c5b8149fbb799925f589e66d4523b0 100644 (file)
@@ -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_FLAGS,         /* --flags=name,addr,casefold */
     NOTMUCH_OPT_STRING,                /* --file=/tmp/gnarf.txt  */
     NOTMUCH_OPT_POSITION       /* notmuch dump pos_arg   */
 };
index a2d58fc1a1b0f01677498afa8676549acecc9d1e..efa34107694d5791ece1c31da68957fed529a49b 100644 (file)
@@ -13,7 +13,7 @@ smtp_dummy_srcs =             \
 smtp_dummy_modules = $(smtp_dummy_srcs:.c=.o)
 
 $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a
-       $(call quiet,CC) $^ -o $@
+       $(call quiet,CC) $^ -o $@ -ltalloc
 
 $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a
        $(call quiet,CC) $^ $(TALLOC_LDFLAGS) -o $@
index 94e90874d2e0e422c433e0601b9e88f0342a618e..9c4cc8431710ec0f992fedc4845d4c9ed063b89d 100755 (executable)
@@ -3,9 +3,10 @@ test_description="argument parsing"
 . ./test-lib.sh
 
 test_begin_subtest "sanity check"
-$TEST_DIRECTORY/arg-test  pos1  --keyword=one --string=foo pos2 --int=7 > OUTPUT
+$TEST_DIRECTORY/arg-test  pos1  --keyword=one --flags=one,two --string=foo pos2 --int=7 > OUTPUT
 cat <<EOF > EXPECTED
 keyword 1
+flags 3
 int 7
 string foo
 positional arg 1 pos1
index 6c49eacd7f3a9bab7537ee82059b6c66a5a76d7a..0d75fe77a873ff53e682fb9508b0035ccc4a189f 100644 (file)
@@ -7,6 +7,7 @@ int main(int argc, char **argv){
     int opt_index=1;
 
     int kw_val=0;
+    int fl_val=0;
     int int_val=0;
     char *pos_arg1=NULL;
     char *pos_arg2=NULL;
@@ -17,6 +18,10 @@ int main(int argc, char **argv){
          (notmuch_keyword_t []){ { "one", 1 },
                                  { "two", 2 },
                                  { 0, 0 } } },
+       { NOTMUCH_OPT_FLAGS, &fl_val, "flags", 'f',
+         (notmuch_keyword_t []){ { "one", 1 << 0},
+                                 { "two", 1 << 1 },
+                                 { 0, 0 } } },
        { NOTMUCH_OPT_INT, &int_val, "int", 'i', 0},
        { NOTMUCH_OPT_STRING, &string_val, "string", 's', 0},
        { NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0},
@@ -31,6 +36,9 @@ int main(int argc, char **argv){
     if (kw_val)
        printf("keyword %d\n", kw_val);
 
+    if (fl_val)
+       printf("flags %d\n", fl_val);
+
     if (int_val)
        printf("int %d\n", int_val);