2011-12-07 19:37:13 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "command-line-arguments.h"
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv){
|
|
|
|
|
|
|
|
int opt_index=1;
|
|
|
|
|
|
|
|
int kw_val=0;
|
2014-10-31 22:53:56 +01:00
|
|
|
int fl_val=0;
|
2011-12-07 19:37:13 +01:00
|
|
|
int int_val=0;
|
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
|
|
|
const char *pos_arg1=NULL;
|
|
|
|
const char *pos_arg2=NULL;
|
|
|
|
const char *string_val=NULL;
|
2011-12-07 19:37:13 +01:00
|
|
|
|
|
|
|
notmuch_opt_desc_t options[] = {
|
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
|
|
|
{ .opt_keyword = &kw_val, .name = "keyword", .keywords =
|
2011-12-07 19:37:13 +01:00
|
|
|
(notmuch_keyword_t []){ { "one", 1 },
|
|
|
|
{ "two", 2 },
|
|
|
|
{ 0, 0 } } },
|
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
|
|
|
{ .opt_flags = &fl_val, .name = "flag", .keywords =
|
2014-10-31 22:53:56 +01:00
|
|
|
(notmuch_keyword_t []){ { "one", 1 << 0},
|
|
|
|
{ "two", 1 << 1 },
|
|
|
|
{ "three", 1 << 2 },
|
|
|
|
{ 0, 0 } } },
|
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
|
|
|
{ .opt_int = &int_val, .name = "int" },
|
|
|
|
{ .opt_string = &string_val, .name = "string" },
|
|
|
|
{ .opt_position = &pos_arg1 },
|
|
|
|
{ .opt_position = &pos_arg2 },
|
|
|
|
{ }
|
|
|
|
};
|
2011-12-07 19:37:13 +01:00
|
|
|
|
|
|
|
opt_index = parse_arguments(argc, argv, options, 1);
|
|
|
|
|
|
|
|
if (opt_index < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (kw_val)
|
|
|
|
printf("keyword %d\n", kw_val);
|
|
|
|
|
2014-10-31 22:53:56 +01:00
|
|
|
if (fl_val)
|
|
|
|
printf("flags %d\n", fl_val);
|
|
|
|
|
2011-12-07 19:37:13 +01:00
|
|
|
if (int_val)
|
|
|
|
printf("int %d\n", int_val);
|
|
|
|
|
|
|
|
if (string_val)
|
|
|
|
printf("string %s\n", string_val);
|
|
|
|
|
|
|
|
if (pos_arg1)
|
|
|
|
printf("positional arg 1 %s\n", pos_arg1);
|
|
|
|
|
|
|
|
if (pos_arg2)
|
2012-12-03 23:54:52 +01:00
|
|
|
printf("positional arg 2 %s\n", pos_arg2);
|
2011-12-07 19:37:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
for ( ; opt_index < argc ; opt_index ++) {
|
|
|
|
printf("non parsed arg %d = %s\n", opt_index, argv[opt_index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|