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, const char *arg_str) {
16 const notmuch_keyword_t *keywords = arg_desc->keywords;
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;
27 fprintf (stderr, "unknown keyword: %s\n", arg_str);
32 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
35 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
38 if (strcmp (arg_str, "false") == 0) {
39 *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
42 if (strcmp (arg_str, "true") == 0) {
43 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
50 Search for the {pos_arg_index}th position argument, return FALSE if
55 parse_position_arg (const char *arg_str, int pos_arg_index,
56 const notmuch_opt_desc_t *arg_desc) {
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;
75 * Search for a non-positional (i.e. starting with --) argument matching arg,
76 * parse a possible value, and assign to *output_var
80 parse_option (const char *arg,
81 const notmuch_opt_desc_t *options) {
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;
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.
100 if (next != '=' && next != ':' && next != 0) return FALSE;
102 if (try->opt_type != NOTMUCH_OPT_BOOLEAN)
105 if (value[0] == 0) return FALSE;
108 if (try->output_var == NULL)
109 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
111 switch (try->opt_type) {
112 case NOTMUCH_OPT_KEYWORD:
113 return _process_keyword_arg (try, value);
115 case NOTMUCH_OPT_BOOLEAN:
116 return _process_boolean_arg (try, next, value);
118 case NOTMUCH_OPT_INT:
119 *((int *)try->output_var) = strtol (value, &endptr, 10);
120 return (*endptr == 0);
122 case NOTMUCH_OPT_STRING:
123 *((const char **)try->output_var) = value;
126 case NOTMUCH_OPT_POSITION:
127 case NOTMUCH_OPT_END:
129 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
135 fprintf (stderr, "Unrecognized option: --%s\n", arg);
139 /* See command-line-arguments.h for description */
141 parse_arguments (int argc, char **argv,
142 const notmuch_opt_desc_t *options, int opt_index) {
144 int pos_arg_index = 0;
145 notmuch_bool_t more_args = TRUE;
147 while (more_args && opt_index < argc) {
148 if (strncmp (argv[opt_index],"--",2) != 0) {
150 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
159 if (strlen (argv[opt_index]) == 2)
162 more_args = parse_option (argv[opt_index], options);