aboutsummaryrefslogtreecommitdiff
path: root/command-line-arguments.h
diff options
context:
space:
mode:
authorJani Nikula <jani@nikula.org>2017-10-01 23:53:11 +0300
committerDavid Bremner <david@tethera.net>2017-10-04 22:00:42 -0300
commit4a6721970a42a9f86149fb5d395d1001fed2d305 (patch)
tree7ee198f5d38f841a580f9b9e30cce5999e8e74cd /command-line-arguments.h
parentd57da17fcd30a5cbe191ccd42906e489dd2bd6a3 (diff)
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.
Diffstat (limited to 'command-line-arguments.h')
-rw-r--r--command-line-arguments.h38
1 files changed, 13 insertions, 25 deletions
diff --git a/command-line-arguments.h b/command-line-arguments.h
index 4c4d240e..ff51abce 100644
--- a/command-line-arguments.h
+++ b/command-line-arguments.h
@@ -3,17 +3,6 @@
#include "notmuch.h"
-enum notmuch_opt_type {
- NOTMUCH_OPT_END = 0,
- NOTMUCH_OPT_INHERIT, /* another options table */
- NOTMUCH_OPT_BOOLEAN, /* --verbose */
- NOTMUCH_OPT_INT, /* --frob=8 */
- NOTMUCH_OPT_KEYWORD, /* --format=raw|json|text */
- NOTMUCH_OPT_KEYWORD_FLAGS, /* the above with values OR'd together */
- NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */
- NOTMUCH_OPT_POSITION /* notmuch dump pos_arg */
-};
-
/*
* Describe one of the possibilities for a keyword option
* 'value' will be copied to the output variable
@@ -24,22 +13,21 @@ typedef struct notmuch_keyword {
int value;
} notmuch_keyword_t;
-/*
- * Describe one option.
- *
- * First two parameters are mandatory.
- *
- * name is mandatory _except_ for positional arguments.
- *
- * arg_id is currently unused, but could define short arguments.
- *
- * keywords is a (possibly NULL) pointer to an array of keywords
- */
+/* Describe one option. */
typedef struct notmuch_opt_desc {
- enum notmuch_opt_type opt_type;
- void *output_var;
+ /* One and only one of opt_* must be set. */
+ const struct notmuch_opt_desc *opt_inherit;
+ notmuch_bool_t *opt_bool;
+ int *opt_int;
+ int *opt_keyword;
+ int *opt_flags;
+ const char **opt_string;
+ const char **opt_position;
+
+ /* Must be set except for opt_inherit and opt_position. */
const char *name;
- int arg_id;
+
+ /* Must be set for opt_keyword and opt_flags. */
const struct notmuch_keyword *keywords;
} notmuch_opt_desc_t;