4 #include "error_util.h"
5 #include "command-line-arguments.h"
8 Search the array of keywords for a given argument, assigning the
9 output variable to the corresponding value. Return FALSE if nothing
14 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
16 const notmuch_keyword_t *keywords = arg_desc->keywords;
19 /* No keyword given */
23 while (keywords->name) {
24 if (strcmp (arg_str, keywords->name) == 0) {
25 if (arg_desc->output_var) {
26 if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS)
27 *((int *)arg_desc->output_var) |= keywords->value;
29 *((int *)arg_desc->output_var) = keywords->value;
36 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
38 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
43 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
46 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
49 if (strcmp (arg_str, "false") == 0) {
50 *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
53 if (strcmp (arg_str, "true") == 0) {
54 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
57 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
62 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
65 if (next == '\0' || arg_str[0] == '\0') {
66 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
70 *((int *)arg_desc->output_var) = strtol (arg_str, &endptr, 10);
74 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
75 arg_str, arg_desc->name);
80 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
83 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
86 if (arg_str[0] == '\0') {
87 fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
90 *((const char **)arg_desc->output_var) = arg_str;
95 Search for the {pos_arg_index}th position argument, return FALSE if
100 parse_position_arg (const char *arg_str, int pos_arg_index,
101 const notmuch_opt_desc_t *arg_desc) {
103 int pos_arg_counter = 0;
104 while (arg_desc->opt_type != NOTMUCH_OPT_END){
105 if (arg_desc->opt_type == NOTMUCH_OPT_POSITION) {
106 if (pos_arg_counter == pos_arg_index) {
107 if (arg_desc->output_var) {
108 *((const char **)arg_desc->output_var) = arg_str;
120 * Search for a non-positional (i.e. starting with --) argument matching arg,
121 * parse a possible value, and assign to *output_var
125 parse_option (const char *_arg, const notmuch_opt_desc_t *options)
130 const char *arg = _arg + 2; /* _arg starts with -- */
131 const notmuch_opt_desc_t *try;
132 for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
133 if (try->opt_type == NOTMUCH_OPT_INHERIT &&
134 parse_option (_arg, try->output_var))
140 if (strncmp (arg, try->name, strlen (try->name)) != 0)
143 char next = arg[strlen (try->name)];
144 const char *value = arg + strlen(try->name) + 1;
147 * If we have not reached the end of the argument (i.e. the
148 * next character is not a space or delimiter) then the
149 * argument could still match a longer option name later in
152 if (next != '=' && next != ':' && next != '\0')
155 if (try->output_var == NULL)
156 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
158 switch (try->opt_type) {
159 case NOTMUCH_OPT_KEYWORD:
160 case NOTMUCH_OPT_KEYWORD_FLAGS:
161 return _process_keyword_arg (try, next, value);
162 case NOTMUCH_OPT_BOOLEAN:
163 return _process_boolean_arg (try, next, value);
164 case NOTMUCH_OPT_INT:
165 return _process_int_arg (try, next, value);
166 case NOTMUCH_OPT_STRING:
167 return _process_string_arg (try, next, value);
168 case NOTMUCH_OPT_POSITION:
169 case NOTMUCH_OPT_END:
171 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
178 /* See command-line-arguments.h for description */
180 parse_arguments (int argc, char **argv,
181 const notmuch_opt_desc_t *options, int opt_index) {
183 int pos_arg_index = 0;
184 notmuch_bool_t more_args = TRUE;
186 while (more_args && opt_index < argc) {
187 if (strncmp (argv[opt_index],"--",2) != 0) {
189 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
198 if (strlen (argv[opt_index]) == 2)
201 more_args = parse_option (argv[opt_index], options);
205 fprintf (stderr, "Unrecognized option: %s\n", argv[opt_index]);