2011-12-01 01:27:26 +01:00
|
|
|
#ifndef NOTMUCH_OPTS_H
|
|
|
|
#define NOTMUCH_OPTS_H
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2011-12-01 01:27:26 +01:00
|
|
|
#include "notmuch.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Describe one of the possibilities for a keyword option
|
|
|
|
* 'value' will be copied to the output variable
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct notmuch_keyword {
|
|
|
|
const char *name;
|
|
|
|
int value;
|
|
|
|
} notmuch_keyword_t;
|
|
|
|
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
/* Describe one option. */
|
2011-12-01 01:27:26 +01:00
|
|
|
typedef struct notmuch_opt_desc {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
/* One and only one of opt_* must be set. */
|
|
|
|
const struct notmuch_opt_desc *opt_inherit;
|
2017-10-07 10:44:04 +02:00
|
|
|
bool *opt_bool;
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
int *opt_int;
|
|
|
|
int *opt_keyword;
|
|
|
|
int *opt_flags;
|
|
|
|
const char **opt_string;
|
|
|
|
const char **opt_position;
|
|
|
|
|
2017-12-25 19:42:26 +01:00
|
|
|
/* for opt_keyword only: if no matching arguments were found, and
|
|
|
|
* keyword_no_arg_value is set, then use keyword_no_arg_value instead. */
|
|
|
|
const char *keyword_no_arg_value;
|
|
|
|
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
/* Must be set except for opt_inherit and opt_position. */
|
2011-12-01 01:27:26 +01:00
|
|
|
const char *name;
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
/* Optional, if non-NULL, set to true if the option is present. */
|
|
|
|
bool *present;
|
2017-10-01 22:53:14 +02:00
|
|
|
|
2017-10-14 15:15:43 +02:00
|
|
|
/* Optional, allow empty strings for opt_string. */
|
|
|
|
bool allow_empty;
|
|
|
|
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
/* Must be set for opt_keyword and opt_flags. */
|
2011-12-13 04:54:24 +01:00
|
|
|
const struct notmuch_keyword *keywords;
|
2011-12-01 01:27:26 +01:00
|
|
|
} notmuch_opt_desc_t;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is the main entry point for command line argument parsing.
|
|
|
|
|
|
|
|
Parse command line arguments according to structure options,
|
|
|
|
starting at position opt_index.
|
|
|
|
|
|
|
|
All output of parsed values is via pointers in options.
|
|
|
|
|
|
|
|
Parsing stops at -- (consumed) or at the (k+1)st argument
|
|
|
|
not starting with -- (a "positional argument") if options contains
|
|
|
|
k positional argument descriptors.
|
|
|
|
|
|
|
|
Returns the index of first non-parsed argument, or -1 in case of error.
|
|
|
|
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
parse_arguments (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the argument parsing loop provided by parse_arguments is not
|
|
|
|
* flexible enough, then the user might be interested in the following
|
|
|
|
* routines, but note that the API to parse_option might have to
|
|
|
|
* change. See command-line-arguments.c for descriptions of these
|
|
|
|
* functions.
|
|
|
|
*/
|
|
|
|
|
2017-07-01 17:18:44 +02:00
|
|
|
int
|
|
|
|
parse_option (int argc, char **argv, const notmuch_opt_desc_t* options, int opt_index);
|
2011-12-01 01:27:26 +01:00
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
bool
|
2011-12-01 01:27:26 +01:00
|
|
|
parse_position_arg (const char *arg,
|
|
|
|
int position_arg_index,
|
|
|
|
const notmuch_opt_desc_t* options);
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|