4 #include "error_util.h"
5 #include "command-line-arguments.h"
8 OPT_FAILED, /* false */
10 OPT_GIVEBACK, /* pop one of the arguments you thought you were getting off the stack */
14 Search the array of keywords for a given argument, assigning the
15 output variable to the corresponding value. Return false if nothing
20 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next,
21 const char *arg_str, bool negate)
23 const notmuch_keyword_t *keywords;
26 /* No keyword given */
30 for (keywords = arg_desc->keywords; keywords->name; keywords++) {
31 if (strcmp (arg_str, keywords->name) != 0)
34 if (arg_desc->opt_flags && negate)
35 *arg_desc->opt_flags &= ~keywords->value;
36 else if (arg_desc->opt_flags)
37 *arg_desc->opt_flags |= keywords->value;
39 *arg_desc->opt_keyword = keywords->value;
44 if (arg_desc->opt_keyword && arg_desc->keyword_no_arg_value && next != ':' && next != '=') {
45 for (keywords = arg_desc->keywords; keywords->name; keywords++) {
46 if (strcmp (arg_desc->keyword_no_arg_value, keywords->name) != 0)
49 *arg_desc->opt_keyword = keywords->value;
50 fprintf (stderr, "Warning: No known keyword option given for \"%s\", choosing value \"%s\"."
51 " Please specify the argument explicitly!\n", arg_desc->name, arg_desc->keyword_no_arg_value);
55 fprintf (stderr, "No matching keyword for option \"%s\" and default value \"%s\" is invalid.\n", arg_str, arg_desc->name);
60 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
62 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
67 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
68 const char *arg_str, bool negate)
72 if (next == '\0' || strcmp (arg_str, "true") == 0) {
74 } else if (strcmp (arg_str, "false") == 0) {
77 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
81 *arg_desc->opt_bool = negate ? !value : value;
87 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
90 if (next == '\0' || arg_str[0] == '\0') {
91 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
95 *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
99 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
100 arg_str, arg_desc->name);
105 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
108 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
111 if (arg_str[0] == '\0' && ! arg_desc->allow_empty) {
112 fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
115 *arg_desc->opt_string = arg_str;
119 /* Return number of non-NULL opt_* fields in opt_desc. */
120 static int _opt_set_count (const notmuch_opt_desc_t *opt_desc)
123 !!opt_desc->opt_inherit +
124 !!opt_desc->opt_bool +
125 !!opt_desc->opt_int +
126 !!opt_desc->opt_keyword +
127 !!opt_desc->opt_flags +
128 !!opt_desc->opt_string +
129 !!opt_desc->opt_position;
132 /* Return true if opt_desc is valid. */
133 static bool _opt_valid (const notmuch_opt_desc_t *opt_desc)
135 int n = _opt_set_count (opt_desc);
138 INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
145 Search for the {pos_arg_index}th position argument, return false if
150 parse_position_arg (const char *arg_str, int pos_arg_index,
151 const notmuch_opt_desc_t *arg_desc) {
153 int pos_arg_counter = 0;
154 while (_opt_valid (arg_desc)) {
155 if (arg_desc->opt_position) {
156 if (pos_arg_counter == pos_arg_index) {
157 *arg_desc->opt_position = arg_str;
158 if (arg_desc->present)
159 *arg_desc->present = true;
169 #define NEGATIVE_PREFIX "no-"
172 * Search for a non-positional (i.e. starting with --) argument matching arg,
173 * parse a possible value, and assign to *output_var
177 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
181 const char *_arg = argv[opt_index];
186 const char *arg = _arg + 2; /* _arg starts with -- */
187 const char *negative_arg = NULL;
189 /* See if this is a --no-argument */
190 if (strlen (arg) > strlen (NEGATIVE_PREFIX) &&
191 strncmp (arg, NEGATIVE_PREFIX, strlen (NEGATIVE_PREFIX)) == 0) {
192 negative_arg = arg + strlen (NEGATIVE_PREFIX);
195 const notmuch_opt_desc_t *try;
197 const char *next_arg = NULL;
198 if (opt_index < argc - 1 && strncmp (argv[opt_index + 1], "--", 2) != 0)
199 next_arg = argv[opt_index + 1];
201 for (try = options; _opt_valid (try); try++) {
202 if (try->opt_inherit) {
203 int new_index = parse_option (argc, argv, try->opt_inherit, opt_index);
215 if (strncmp (arg, try->name, strlen (try->name)) == 0) {
216 next = arg[strlen (try->name)];
217 value = arg + strlen (try->name) + 1;
218 } else if (negative_arg && (try->opt_bool || try->opt_flags) &&
219 strncmp (negative_arg, try->name, strlen (try->name)) == 0) {
220 next = negative_arg[strlen (try->name)];
221 value = negative_arg + strlen (try->name) + 1;
222 /* The argument part of --no-argument matches, negate the result. */
229 * If we have not reached the end of the argument (i.e. the
230 * next character is not a space or delimiter) then the
231 * argument could still match a longer option name later in
234 if (next != '=' && next != ':' && next != '\0')
237 bool lookahead = (next == '\0' && next_arg != NULL && ! try->opt_bool);
245 opt_handled opt_status = OPT_FAILED;
246 if (try->opt_keyword || try->opt_flags)
247 opt_status = _process_keyword_arg (try, next, value, negate);
248 else if (try->opt_bool)
249 opt_status = _process_boolean_arg (try, next, value, negate);
250 else if (try->opt_int)
251 opt_status = _process_int_arg (try, next, value);
252 else if (try->opt_string)
253 opt_status = _process_string_arg (try, next, value);
255 INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
257 if (opt_status == OPT_FAILED)
260 if (lookahead && opt_status == OPT_GIVEBACK)
264 *try->present = true;
271 /* See command-line-arguments.h for description */
273 parse_arguments (int argc, char **argv,
274 const notmuch_opt_desc_t *options, int opt_index) {
276 int pos_arg_index = 0;
277 bool more_args = true;
279 while (more_args && opt_index < argc) {
280 if (strncmp (argv[opt_index],"--",2) != 0) {
282 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
290 int prev_opt_index = opt_index;
292 if (strlen (argv[opt_index]) == 2)
295 opt_index = parse_option (argc, argv, options, opt_index);
297 fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);