Commit graph

19 commits

Author SHA1 Message Date
Daniel Kahn Gillmor
0ada2a05c9 cli: some keyword options can be supplied with no argument
We might change some notmuch command line tools that used to be
booleans into keyword arguments.

In that case, there are some legacy tools that will expect to be able
to do "notmuch foo --bar" instead of "notmuch foo --bar=baz".

This patch makes it possible to support that older API, while
providing a warning and an encouragement to upgrade.
2017-12-29 16:45:35 -04:00
Jani Nikula
39abd3b522 cli: add support for --no- prefixed boolean and keyword flag arguments
Add transparent support for negating boolean and keyword flag
arguments using --no-argument style on the command line. That is, if
the option description contains a boolean or a keyword flag argument
named "argument", --no-argument will match and negate it.

For boolean arguments this obviously means the logical NOT. For
keyword flag arguments this means bitwise AND of the bitwise NOT,
i.e. masking out the specified bits instead of OR'ing them in.

For example, you can use --no-exclude instead of --exclude=false in
notmuch show. If we had keyword flag arguments with some flags
defaulting to on, say --include=tags in notmuch dump/restore, this
would allow --no-include=tags to switch that off while not affecting
other flags.

As a curiosity, you should be able to warp your brain using
--no-exclude=true meaning false and --no-exclude=false meaning true if
you wish.

Specifying both "argument" and "no-argument" style arguments in the
same option description should be avoided. In this case, --no-argument
would match whichever is specified first, and --argument would only
match "argument".
2017-12-13 08:21:13 -04:00
Jani Nikula
733ccfabca cli: allow empty strings for notmuch insert --folder argument
Now that it's easy to add argument specific modifiers in opt
descriptions, add a new .allow_empty field to allow empty strings for
individual string arguments while retaining strict checks
elsewhere. Use this for notmuch insert --folder, where the empty
string means top level folder.
2017-11-08 09:46:44 -05:00
Jani Nikula
0f314c0c99 cli: convert notmuch_bool_t to stdbool
C99 stdbool turned 18 this year. There really is no reason to use our
own, except in the library interface for backward
compatibility. Convert the cli and test binaries to stdbool.
2017-10-09 22:24:02 -03:00
Jani Nikula
288c3660d8 cli: reduce indent in keyword argument processing
Reducing indent makes future changes easier. No functional changes.
2017-10-04 22:03:52 -03:00
Jani Nikula
4dc3291199 cli: change while to for in keyword argument processing
Using a for loop makes it easier to use continue, in preparation for
future changes. No functional changes.
2017-10-04 22:03:21 -03:00
Jani Nikula
e704947855 cli: refactor boolean argument processing
Clean up the control flow to prepare for future changes. No functional
changes.
2017-10-04 22:03:10 -03:00
Jani Nikula
bc99087ff9 cli: add .present field to opt desc to check if the arg was present
Add pointer to boolean .present field to opt desc, which (if non-NULL)
will be set to TRUE if the argument in question is present on the
command line. Unchanged otherwise.
2017-10-04 22:01:40 -03:00
Jani Nikula
4a6721970a 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-04 22:00:42 -03:00
David Bremner
a32dd81a55 cli: add space separator for keyword, string, and int arguments
Defer the complication of optional boolean arguments for later (never?).
2017-07-12 17:46:29 -03:00
David Bremner
de9baa29dc cli: change api of parse_option
The idea is to allow it (in a future commit) advance to the next argv
element to get a value
2017-07-12 17:44:15 -03:00
Jani Nikula
4387112de0 cli: add support for hierarchical command line option arrays
NOTMUCH_OPT_INHERIT expects a notmuch_opt_desc_t * pointer in
output_var.

The "Unrecognized option" message was moved out of parse_option() to
not be emitted twice or when parsing a non-inherited option.
2014-11-05 23:17:00 +01:00
Jani Nikula
ecc4a9a644 cli: Add support for parsing keyword-flag arguments
This allows having multiple --foo=bar --foo=baz options on the command
line, with the corresponding values OR'd together.

[Test added by Michal Sojka]
2014-11-01 08:02:21 +01:00
Jani Nikula
6960b92bb6 cli: command line option parser cleanup
Reduce the indentation for clarity. No functional changes.
2014-02-25 20:53:41 -04:00
Mark Walters
ab1487363e cli: make the command line parser's errors more informative.
Previously, the cli parser was a little erratic in what errors it
reported and would fail silently in many cases (for example, when no
argument was passed to an integer option). This was particularly
annoying as the user could not (easily) tell whether the command
failed or just there were no search results.

This patch tries to make the handling consistent and return a helpful
error message in all cases.
2012-09-01 23:03:36 -03:00
Mark Walters
779ce3e930 cli: command line parsing: allow default for keyword options
This changes the parsing for "keyword" options so that if the option
is specified with no argument the argument is parsed as if it were
passed an empty string. This make it easier to add options to existing
boolean arguments (the existing --option can default to TRUE).
2012-06-29 22:31:40 -03:00
Mark Walters
b3e4417897 cli: Parsing. Allow true/false parameter for boolean options.
Allow NOTMUCH_OPT_BOOLEAN to take a true or false parameter.  In
particular it allows the user to turn off a boolean option with
--option=false.
2012-03-18 09:14:22 -03:00
David Bremner
f717d2ece1 command-line-arguments.[ch]: make arrays of keyword descriptors const
It seems like it should never be necessary to modify these arrays after
initialization.
2011-12-13 00:00:44 -04:00
David Bremner
2cf7b27a0c command-line-arguments.[ch]: new argument parsing framework for notmuch.
As we noticed when Jani kindly converted things to getopt_long, much
of the work in argument parsing in notmuch is due to the the key-value
style arguments like --format=(raw|json|text).

The framework here provides positional arguments, simple switches,
and --key=value style arguments that can take a value being an integer,
a string, or one of a set of keywords.
2011-12-08 20:24:24 -04:00