]> rtime.felk.cvut.cz Git - notmuch.git/blob - test/arg-test.c
cli: Add support for parsing command line "flag" options
[notmuch.git] / test / arg-test.c
1 #include <stdio.h>
2 #include "command-line-arguments.h"
3
4
5 int main(int argc, char **argv){
6
7     int opt_index=1;
8
9     int kw_val=0;
10     int fl_val=0;
11     int int_val=0;
12     char *pos_arg1=NULL;
13     char *pos_arg2=NULL;
14     char *string_val=NULL;
15
16     notmuch_opt_desc_t options[] = {
17         { NOTMUCH_OPT_KEYWORD, &kw_val, "keyword", 'k',
18           (notmuch_keyword_t []){ { "one", 1 },
19                                   { "two", 2 },
20                                   { 0, 0 } } },
21         { NOTMUCH_OPT_FLAGS, &fl_val, "flags", 'f',
22           (notmuch_keyword_t []){ { "one", 1 << 0},
23                                   { "two", 1 << 1 },
24                                   { 0, 0 } } },
25         { NOTMUCH_OPT_INT, &int_val, "int", 'i', 0},
26         { NOTMUCH_OPT_STRING, &string_val, "string", 's', 0},
27         { NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0},
28         { NOTMUCH_OPT_POSITION, &pos_arg2, 0,0, 0},
29         { 0, 0, 0, 0, 0 } };
30
31     opt_index = parse_arguments(argc, argv, options, 1);
32
33     if (opt_index < 0)
34         return 1;
35
36     if (kw_val)
37         printf("keyword %d\n", kw_val);
38
39     if (fl_val)
40         printf("flags %d\n", fl_val);
41
42     if (int_val)
43         printf("int %d\n", int_val);
44
45     if (string_val)
46         printf("string %s\n", string_val);
47
48     if (pos_arg1)
49         printf("positional arg 1 %s\n", pos_arg1);
50
51     if (pos_arg2)
52         printf("positional arg 2 %s\n", pos_arg2);
53
54
55     for ( ; opt_index < argc ; opt_index ++) {
56         printf("non parsed arg %d = %s\n", opt_index, argv[opt_index]);
57     }
58
59     return 0;
60 }