multicall: migrate match options to shared collection

This commit is contained in:
Ariadne Conill 2020-08-11 22:37:36 -06:00
parent f871002ecc
commit 52509c508c
5 changed files with 17 additions and 33 deletions

View file

@ -41,7 +41,8 @@ LIBIFUPDOWN_LIB = libifupdown.a
MULTICALL_SRC = \ MULTICALL_SRC = \
cmd/multicall.c \ cmd/multicall.c \
cmd/multicall-options.c cmd/multicall-options.c \
cmd/multicall-match-options.c
MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o} MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o}
MULTICALL = ifupdown MULTICALL = ifupdown

View file

@ -174,9 +174,6 @@ ifquery_usage(int status)
fprintf(stderr, " -V, --version show this program's version\n"); fprintf(stderr, " -V, --version show this program's version\n");
fprintf(stderr, " -i, --interfaces FILE use FILE for interface definitions\n"); fprintf(stderr, " -i, --interfaces FILE use FILE for interface definitions\n");
fprintf(stderr, " -L, --list list matching interfaces\n"); fprintf(stderr, " -L, --list list matching interfaces\n");
fprintf(stderr, " -a, --auto only match against interfaces hinted as 'auto'\n");
fprintf(stderr, " -I, --include PATTERN only match against interfaces matching PATTERN\n");
fprintf(stderr, " -X, --exclude PATTERN never match against interfaces matching PATTERN\n");
fprintf(stderr, " -P, --pretty-print pretty print the interfaces instead of just listing\n"); fprintf(stderr, " -P, --pretty-print pretty print the interfaces instead of just listing\n");
fprintf(stderr, " -S, --state-file FILE use FILE for state\n"); fprintf(stderr, " -S, --state-file FILE use FILE for state\n");
fprintf(stderr, " -s, --state show configured state\n"); fprintf(stderr, " -s, --state show configured state\n");
@ -253,13 +250,8 @@ ifquery_main(int argc, char *argv[])
struct lif_dict state = {}; struct lif_dict state = {};
struct lif_dict collection = {}; struct lif_dict collection = {};
struct option long_options[] = { struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"interfaces", required_argument, 0, 'i'}, {"interfaces", required_argument, 0, 'i'},
{"list", no_argument, 0, 'L'}, {"list", no_argument, 0, 'L'},
{"auto", no_argument, 0, 'a'},
{"include", required_argument, 0, 'I'},
{"exclude", required_argument, 0, 'X'},
{"pretty-print", no_argument, 0, 'P'}, {"pretty-print", no_argument, 0, 'P'},
{"state-file", required_argument, 0, 'S'}, {"state-file", required_argument, 0, 'S'},
{"state", no_argument, 0, 's'}, {"state", no_argument, 0, 's'},
@ -268,7 +260,6 @@ ifquery_main(int argc, char *argv[])
{"executor-path", required_argument, 0, 'E'}, {"executor-path", required_argument, 0, 'E'},
{NULL, 0, 0, 0} {NULL, 0, 0, 0}
}; };
struct match_options match_opts = {};
bool listing = false, listing_stat = false; bool listing = false, listing_stat = false;
char *state_file = STATE_FILE; char *state_file = STATE_FILE;
@ -279,27 +270,12 @@ ifquery_main(int argc, char *argv[])
break; break;
switch (c) { switch (c) {
case 'h':
ifquery_usage(EXIT_SUCCESS);
break;
case 'V':
lif_common_version();
break;
case 'i': case 'i':
exec_opts.interfaces_file = optarg; exec_opts.interfaces_file = optarg;
break; break;
case 'L': case 'L':
listing = true; listing = true;
break; break;
case 'a':
match_opts.is_auto = true;
break;
case 'I':
match_opts.include_pattern = optarg;
break;
case 'X':
match_opts.exclude_pattern = optarg;
break;
case 'P': case 'P':
match_opts.pretty_print = true; match_opts.pretty_print = true;
break; break;
@ -381,6 +357,8 @@ ifquery_main(int argc, char *argv[])
struct if_applet ifquery_applet = { struct if_applet ifquery_applet = {
.name = "ifquery", .name = "ifquery",
.desc = "query interface configuration",
.main = ifquery_main, .main = ifquery_main,
.usage = ifquery_usage .usage = ifquery_usage,
.groups = { &global_option_group, &match_option_group, NULL },
}; };

View file

@ -204,7 +204,6 @@ ifupdown_main(int argc, char *argv[])
{"no-lock", no_argument, 0, 'L'}, {"no-lock", no_argument, 0, 'L'},
{NULL, 0, 0, 0} {NULL, 0, 0, 0}
}; };
struct match_options match_opts = {};
for (;;) for (;;)
{ {

View file

@ -51,7 +51,7 @@ generic_usage(const struct if_applet *applet, int result)
if (opt->long_opt) if (opt->long_opt)
fprintf(stderr, "%c --%-30s", opt->short_opt ? ',' : ' ', fprintf(stderr, "%c --%-30s", opt->short_opt ? ',' : ' ',
opt->long_opt); opt->long_opt_desc ? opt->long_opt_desc : opt->long_opt);
else else
fprintf(stderr, "%34s", ""); fprintf(stderr, "%34s", "");
@ -73,8 +73,8 @@ generic_usage_request(int short_opt, const struct if_option *option, const char
} }
static struct if_option global_options[] = { static struct if_option global_options[] = {
{'h', "help", "displays program help", false, generic_usage_request}, {'h', "help", NULL, "this help", false, generic_usage_request},
{'V', "version", "displays program version", false, (void *) lif_common_version}, {'V', "version", NULL, "show this program's version", false, (void *) lif_common_version},
}; };
struct if_option_group global_option_group = { struct if_option_group global_option_group = {

View file

@ -27,6 +27,7 @@ struct if_applet;
struct if_option { struct if_option {
char short_opt; char short_opt;
const char *long_opt; const char *long_opt;
const char *long_opt_desc;
const char *desc; const char *desc;
bool require_argument; bool require_argument;
void (*const handle)(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet); void (*const handle)(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet);
@ -47,18 +48,23 @@ struct if_applet {
}; };
extern char *argv0; extern char *argv0;
extern const struct if_applet *self_applet;
extern struct if_option_group global_option_group; extern struct if_option_group global_option_group;
struct match_options { struct match_options {
bool is_auto; bool is_auto;
char *exclude_pattern; const char *exclude_pattern;
char *include_pattern; const char *include_pattern;
bool pretty_print; bool pretty_print;
bool dot; bool dot;
char *property; const char *property;
}; };
extern struct match_options match_opts;
extern void process_options(const struct if_applet *applet, int argc, char *argv[]); extern void process_options(const struct if_applet *applet, int argc, char *argv[]);
extern const struct if_option *lookup_option(const struct if_applet *applet, int opt); extern const struct if_option *lookup_option(const struct if_applet *applet, int opt);
extern struct if_option_group match_option_group;
#endif #endif