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 *((int *)arg_desc->output_var) = keywords->value;
33 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
35 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
40 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
43 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
46 if (strcmp (arg_str, "false") == 0) {
47 *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
50 if (strcmp (arg_str, "true") == 0) {
51 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
54 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
59 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
62 if (next == '\0' || arg_str[0] == '\0') {
63 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
67 *((int *)arg_desc->output_var) = strtol (arg_str, &endptr, 10);
71 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
72 arg_str, arg_desc->name);
77 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
80 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
83 if (arg_str[0] == '\0') {
84 fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
87 *((const char **)arg_desc->output_var) = arg_str;
92 Search for the {pos_arg_index}th position argument, return FALSE if
97 parse_position_arg (const char *arg_str, int pos_arg_index,
98 const notmuch_opt_desc_t *arg_desc) {
100 int pos_arg_counter = 0;
101 while (arg_desc->opt_type != NOTMUCH_OPT_END){
102 if (arg_desc->opt_type == NOTMUCH_OPT_POSITION) {
103 if (pos_arg_counter == pos_arg_index) {
104 if (arg_desc->output_var) {
105 *((const char **)arg_desc->output_var) = arg_str;
117 * Search for a non-positional (i.e. starting with --) argument matching arg,
118 * parse a possible value, and assign to *output_var
122 parse_option (const char *arg,
123 const notmuch_opt_desc_t *options) {
130 const notmuch_opt_desc_t *try;
131 for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
132 if (try->name && strncmp (arg, try->name, strlen (try->name)) == 0) {
133 char next = arg[strlen (try->name)];
134 const char *value= arg+strlen(try->name)+1;
136 /* If we have not reached the end of the argument
137 (i.e. the next character is not a space or delimiter)
138 then the argument could still match a longer option
139 name later in the option table.
141 if (next != '=' && next != ':' && next != '\0')
144 if (try->output_var == NULL)
145 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
147 switch (try->opt_type) {
148 case NOTMUCH_OPT_KEYWORD:
149 return _process_keyword_arg (try, next, value);
151 case NOTMUCH_OPT_BOOLEAN:
152 return _process_boolean_arg (try, next, value);
154 case NOTMUCH_OPT_INT:
155 return _process_int_arg (try, next, value);
157 case NOTMUCH_OPT_STRING:
158 return _process_string_arg (try, next, value);
160 case NOTMUCH_OPT_POSITION:
161 case NOTMUCH_OPT_END:
163 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
168 fprintf (stderr, "Unrecognized option: --%s\n", arg);
172 /* See command-line-arguments.h for description */
174 parse_arguments (int argc, char **argv,
175 const notmuch_opt_desc_t *options, int opt_index) {
177 int pos_arg_index = 0;
178 notmuch_bool_t more_args = TRUE;
180 while (more_args && opt_index < argc) {
181 if (strncmp (argv[opt_index],"--",2) != 0) {
183 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
192 if (strlen (argv[opt_index]) == 2)
195 more_args = parse_option (argv[opt_index], options);