]> git.notmuchmail.org Git - notmuch/blob - command-line-arguments.c
cli: Parsing. Allow true/false parameter for boolean options.
[notmuch] / command-line-arguments.c
1 #include <assert.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "error_util.h"
5 #include "command-line-arguments.h"
6
7 /*
8   Search the array of keywords for a given argument, assigning the
9   output variable to the corresponding value.  Return FALSE if nothing
10   matches.
11 */
12
13 static notmuch_bool_t
14 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, const char *arg_str) {
15
16     const notmuch_keyword_t *keywords = arg_desc->keywords;
17
18     while (keywords->name) {
19         if (strcmp (arg_str, keywords->name) == 0) {
20             if (arg_desc->output_var) {
21                 *((int *)arg_desc->output_var) = keywords->value;
22             }
23             return TRUE;
24         }
25         keywords++;
26     }
27     fprintf (stderr, "unknown keyword: %s\n", arg_str);
28     return FALSE;
29 }
30
31 static notmuch_bool_t
32 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
33
34     if (next == 0) {
35         *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
36         return TRUE;
37     }
38     if (strcmp (arg_str, "false") == 0) {
39         *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
40         return TRUE;
41     }
42     if (strcmp (arg_str, "true") == 0) {
43         *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
44         return TRUE;
45     }
46     return FALSE;
47 }
48
49 /*
50    Search for the {pos_arg_index}th position argument, return FALSE if
51    that does not exist.
52 */
53
54 notmuch_bool_t
55 parse_position_arg (const char *arg_str, int pos_arg_index,
56                     const notmuch_opt_desc_t *arg_desc) {
57
58     int pos_arg_counter = 0;
59     while (arg_desc->opt_type != NOTMUCH_OPT_END){
60         if (arg_desc->opt_type == NOTMUCH_OPT_POSITION) {
61             if (pos_arg_counter == pos_arg_index) {
62                 if (arg_desc->output_var) {
63                     *((const char **)arg_desc->output_var) = arg_str;
64                 }
65                 return TRUE;
66             }
67             pos_arg_counter++;
68         }
69         arg_desc++;
70     }
71     return FALSE;
72 }
73
74 /*
75  * Search for a non-positional (i.e. starting with --) argument matching arg,
76  * parse a possible value, and assign to *output_var
77  */
78
79 notmuch_bool_t
80 parse_option (const char *arg,
81               const notmuch_opt_desc_t *options) {
82
83     assert(arg);
84     assert(options);
85
86     arg += 2;
87
88     const notmuch_opt_desc_t *try = options;
89     while (try->opt_type != NOTMUCH_OPT_END) {
90         if (try->name && strncmp (arg, try->name, strlen (try->name)) == 0) {
91             char next = arg[strlen (try->name)];
92             const char *value= arg+strlen(try->name)+1;
93
94             char *endptr;
95
96             /* Everything but boolean arguments (switches) needs a
97              * delimiter, and a non-zero length value. Boolean
98              * arguments may take an optional =true or =false value.
99              */
100             if (next != '=' && next != ':' && next != 0) return FALSE;
101             if (next == 0) {
102                 if (try->opt_type != NOTMUCH_OPT_BOOLEAN)
103                     return FALSE;
104             } else {
105                 if (value[0] == 0) return FALSE;
106             }
107
108             if (try->output_var == NULL)
109                 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
110
111             switch (try->opt_type) {
112             case NOTMUCH_OPT_KEYWORD:
113                 return _process_keyword_arg (try, value);
114                 break;
115             case NOTMUCH_OPT_BOOLEAN:
116                 return _process_boolean_arg (try, next, value);
117                 break;
118             case NOTMUCH_OPT_INT:
119                 *((int *)try->output_var) = strtol (value, &endptr, 10);
120                 return (*endptr == 0);
121                 break;
122             case NOTMUCH_OPT_STRING:
123                 *((const char **)try->output_var) = value;
124                 return TRUE;
125                 break;
126             case NOTMUCH_OPT_POSITION:
127             case NOTMUCH_OPT_END:
128             default:
129                 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
130                 /*UNREACHED*/
131             }
132         }
133         try++;
134     }
135     fprintf (stderr, "Unrecognized option: --%s\n", arg);
136     return FALSE;
137 }
138
139 /* See command-line-arguments.h for description */
140 int
141 parse_arguments (int argc, char **argv,
142                  const notmuch_opt_desc_t *options, int opt_index) {
143
144     int pos_arg_index = 0;
145     notmuch_bool_t more_args = TRUE;
146
147     while (more_args && opt_index < argc) {
148         if (strncmp (argv[opt_index],"--",2) != 0) {
149
150             more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
151
152             if (more_args) {
153                 pos_arg_index++;
154                 opt_index++;
155             }
156
157         } else {
158
159             if (strlen (argv[opt_index]) == 2)
160                 return opt_index+1;
161
162             more_args = parse_option (argv[opt_index], options);
163             if (more_args) {
164                 opt_index++;
165             } else {
166                 opt_index = -1;
167             }
168
169         }
170     }
171
172     return opt_index;
173 }