diff options
| author | Jani Nikula <jani@nikula.org> | 2017-10-01 23:53:11 +0300 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2017-10-04 22:00:42 -0300 |
| commit | 4a6721970a42a9f86149fb5d395d1001fed2d305 (patch) | |
| tree | 7ee198f5d38f841a580f9b9e30cce5999e8e74cd /test | |
| parent | d57da17fcd30a5cbe191ccd42906e489dd2bd6a3 (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 'test')
| -rw-r--r-- | test/arg-test.c | 21 | ||||
| -rw-r--r-- | test/hex-xcode.c | 10 | ||||
| -rw-r--r-- | test/random-corpus.c | 20 |
3 files changed, 26 insertions, 25 deletions
diff --git a/test/arg-test.c b/test/arg-test.c index 736686de..10dc0683 100644 --- a/test/arg-test.c +++ b/test/arg-test.c @@ -9,25 +9,26 @@ int main(int argc, char **argv){ int kw_val=0; int fl_val=0; int int_val=0; - char *pos_arg1=NULL; - char *pos_arg2=NULL; - char *string_val=NULL; + const char *pos_arg1=NULL; + const char *pos_arg2=NULL; + const char *string_val=NULL; notmuch_opt_desc_t options[] = { - { NOTMUCH_OPT_KEYWORD, &kw_val, "keyword", 'k', + { .opt_keyword = &kw_val, .name = "keyword", .keywords = (notmuch_keyword_t []){ { "one", 1 }, { "two", 2 }, { 0, 0 } } }, - { NOTMUCH_OPT_KEYWORD_FLAGS, &fl_val, "flag", 'f', + { .opt_flags = &fl_val, .name = "flag", .keywords = (notmuch_keyword_t []){ { "one", 1 << 0}, { "two", 1 << 1 }, { "three", 1 << 2 }, { 0, 0 } } }, - { NOTMUCH_OPT_INT, &int_val, "int", 'i', 0}, - { NOTMUCH_OPT_STRING, &string_val, "string", 's', 0}, - { NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0}, - { NOTMUCH_OPT_POSITION, &pos_arg2, 0,0, 0}, - { 0, 0, 0, 0, 0 } }; + { .opt_int = &int_val, .name = "int" }, + { .opt_string = &string_val, .name = "string" }, + { .opt_position = &pos_arg1 }, + { .opt_position = &pos_arg2 }, + { } + }; opt_index = parse_arguments(argc, argv, options, 1); diff --git a/test/hex-xcode.c b/test/hex-xcode.c index 65d49564..bc2df713 100644 --- a/test/hex-xcode.c +++ b/test/hex-xcode.c @@ -44,17 +44,17 @@ int main (int argc, char **argv) { - enum direction dir = DECODE; + int dir = DECODE; int omit_newline = FALSE; notmuch_opt_desc_t options[] = { - { NOTMUCH_OPT_KEYWORD, &dir, "direction", 'd', + { .opt_keyword = &dir, .name = "direction", .keywords = (notmuch_keyword_t []){ { "encode", ENCODE }, { "decode", DECODE }, { 0, 0 } } }, - { NOTMUCH_OPT_BOOLEAN, &omit_newline, "omit-newline", 'n', 0 }, - { NOTMUCH_OPT_BOOLEAN, &inplace, "in-place", 'i', 0 }, - { 0, 0, 0, 0, 0 } + { .opt_bool = &omit_newline, .name = "omit-newline" }, + { .opt_bool = &inplace, .name = "in-place" }, + { } }; int opt_index = parse_arguments (argc, argv, options, 1); diff --git a/test/random-corpus.c b/test/random-corpus.c index aca694a3..e3b855e1 100644 --- a/test/random-corpus.c +++ b/test/random-corpus.c @@ -116,10 +116,10 @@ random_utf8_string (void *ctx, size_t char_count) /* stubs since we cannot link with notmuch.o */ const notmuch_opt_desc_t notmuch_shared_options[] = { - { 0, 0, 0, 0, 0 } + { } }; -char *notmuch_requested_db_uuid = NULL; +const char *notmuch_requested_db_uuid = NULL; void notmuch_process_shared_options (unused (const char *dummy)) @@ -140,7 +140,7 @@ main (int argc, char **argv) void *ctx = talloc_new (NULL); - char *config_path = NULL; + const char *config_path = NULL; notmuch_config_t *config; notmuch_database_t *notmuch; @@ -155,13 +155,13 @@ main (int argc, char **argv) int seed = 734569; notmuch_opt_desc_t options[] = { - { NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 }, - { NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 }, - { NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 }, - { NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 }, - { NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 }, - { NOTMUCH_OPT_INT, &seed, "seed", 's', 0 }, - { 0, 0, 0, 0, 0 } + { .opt_string = &config_path, .name = "config-path" }, + { .opt_int = &num_messages, .name = "num-messages" }, + { .opt_int = &max_tags, .name = "max-tags" }, + { .opt_int = &message_id_len, .name = "message-id-len" }, + { .opt_int = &tag_len, .name = "tag-len" }, + { .opt_int = &seed, .name = "seed" }, + { } }; int opt_index = parse_arguments (argc, argv, options, 1); |
