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 Search for the {pos_arg_index}th position argument, return FALSE if
37 parse_position_arg (const char *arg_str, int pos_arg_index,
38 const notmuch_opt_desc_t *arg_desc) {
40 int pos_arg_counter = 0;
41 while (arg_desc->opt_type != NOTMUCH_OPT_END){
42 if (arg_desc->opt_type == NOTMUCH_OPT_POSITION) {
43 if (pos_arg_counter == pos_arg_index) {
44 if (arg_desc->output_var) {
45 *((const char **)arg_desc->output_var) = arg_str;
57 * Search for a non-positional (i.e. starting with --) argument matching arg,
58 * parse a possible value, and assign to *output_var
62 parse_option (const char *arg,
63 const notmuch_opt_desc_t *options) {
70 const notmuch_opt_desc_t *try = options;
71 while (try->opt_type != NOTMUCH_OPT_END) {
72 if (try->name && strncmp (arg, try->name, strlen (try->name)) == 0) {
73 char next = arg[strlen (try->name)];
74 const char *value= arg+strlen(try->name)+1;
78 /* Everything but boolean arguments (switches) needs a
79 * delimiter, and a non-zero length value
82 if (try->opt_type != NOTMUCH_OPT_BOOLEAN) {
83 if (next != '=' && next != ':') return FALSE;
84 if (value[0] == 0) return FALSE;
86 if (next != 0) return FALSE;
89 if (try->output_var == NULL)
90 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
92 switch (try->opt_type) {
93 case NOTMUCH_OPT_KEYWORD:
94 return _process_keyword_arg (try, value);
96 case NOTMUCH_OPT_BOOLEAN:
97 *((notmuch_bool_t *)try->output_var) = TRUE;
100 case NOTMUCH_OPT_INT:
101 *((int *)try->output_var) = strtol (value, &endptr, 10);
102 return (*endptr == 0);
104 case NOTMUCH_OPT_STRING:
105 *((const char **)try->output_var) = value;
108 case NOTMUCH_OPT_POSITION:
109 case NOTMUCH_OPT_END:
111 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
117 fprintf (stderr, "Unrecognized option: --%s\n", arg);
121 /* See command-line-arguments.h for description */
123 parse_arguments (int argc, char **argv,
124 const notmuch_opt_desc_t *options, int opt_index) {
126 int pos_arg_index = 0;
127 notmuch_bool_t more_args = TRUE;
129 while (more_args && opt_index < argc) {
130 if (strncmp (argv[opt_index],"--",2) != 0) {
132 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
141 if (strlen (argv[opt_index]) == 2)
144 more_args = parse_option (argv[opt_index], options);