ifquery: port to use option groups

This commit is contained in:
Ariadne Conill 2020-08-11 23:03:55 -06:00
parent fdfe8ac080
commit f6d9f65248
3 changed files with 45 additions and 49 deletions

View file

@ -164,17 +164,6 @@ ifquery_usage(int status)
fprintf(stderr, "usage: ifquery [options] <interfaces>\n"); fprintf(stderr, "usage: ifquery [options] <interfaces>\n");
fprintf(stderr, " ifquery [options] --list\n"); fprintf(stderr, " ifquery [options] --list\n");
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, " -h, --help this help\n");
fprintf(stderr, " -V, --version show this program's version\n");
fprintf(stderr, " -i, --interfaces FILE use FILE for interface definitions\n");
fprintf(stderr, " -L, --list list matching interfaces\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 show configured state\n");
fprintf(stderr, " -D, --dot generate a dependency graph\n");
fprintf(stderr, " -p, --property PROPERTY print values of properties matching PROPERTY\n");
exit(status); exit(status);
} }
@ -239,28 +228,15 @@ list_state(struct lif_dict *state, struct match_options *opts)
} }
} }
int static bool listing = false, listing_stat = false;
ifquery_main(int argc, char *argv[])
static void
handle_local(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet)
{ {
struct lif_dict state = {}; (void) opt;
struct lif_dict collection = {}; (void) applet;
struct option long_options[] = {
{"list", no_argument, 0, 'L'},
{"pretty-print", no_argument, 0, 'P'},
{"state", no_argument, 0, 's'},
{"dot", no_argument, 0, 'D'},
{"property", required_argument, 0, 'p'},
{NULL, 0, 0, 0}
};
bool listing = false, listing_stat = false;
for (;;) switch (short_opt) {
{
int c = getopt_long(argc, argv, "hVi:LaI:X:PS:sDp:E:", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'L': case 'L':
listing = true; listing = true;
break; break;
@ -274,10 +250,30 @@ ifquery_main(int argc, char *argv[])
match_opts.dot = true; match_opts.dot = true;
break; break;
case 'p': case 'p':
match_opts.property = optarg; match_opts.property = opt_arg;
break; break;
} }
} }
static struct if_option local_options[] = {
{'s', "state", NULL, "show configured state", false, handle_local},
{'p', "property", "property PROPERTY", "print values of properties matching PROPERTY", true, handle_local},
{'D', "dot", NULL, "generate a dependency graph", false, handle_local},
{'L', "list", NULL, "list matching interfaces", false, handle_local},
{'P', "pretty-print", NULL, "pretty print the interfaces instead of just listing", false, handle_local},
};
static struct if_option_group local_option_group = {
.desc = "Program-specific options",
.group_size = ARRAY_SIZE(local_options),
.group = local_options
};
int
ifquery_main(int argc, char *argv[])
{
struct lif_dict state = {};
struct lif_dict collection = {};
if (!lif_state_read_path(&state, exec_opts.state_file)) if (!lif_state_read_path(&state, exec_opts.state_file))
{ {
@ -342,5 +338,5 @@ struct if_applet ifquery_applet = {
.desc = "query interface configuration", .desc = "query interface configuration",
.main = ifquery_main, .main = ifquery_main,
.usage = ifquery_usage, .usage = ifquery_usage,
.groups = { &global_option_group, &match_option_group, &exec_option_group }, .groups = { &global_option_group, &match_option_group, &exec_option_group, &local_option_group },
}; };

View file

@ -52,7 +52,7 @@ static void handle_exec(int short_opt, const struct if_option *opt, const char *
break; break;
case 'f': case 'f':
break; break;
case 'L': case 'l':
exec_opts.no_lock = true; exec_opts.no_lock = true;
break; break;
default: default:
@ -63,10 +63,10 @@ static void handle_exec(int short_opt, const struct if_option *opt, const char *
static struct if_option exec_options[] = { static struct if_option exec_options[] = {
{'f', "force", NULL, "force (de)configuration", true, handle_exec}, {'f', "force", NULL, "force (de)configuration", true, handle_exec},
{'i', "interfaces", "interfaces FILE", "use FILE for interface definitions", true, handle_exec}, {'i', "interfaces", "interfaces FILE", "use FILE for interface definitions", true, handle_exec},
{'l', "no-lock", NULL, "do not use a lockfile to serialize state changes", false, handle_exec},
{'n', "no-act", NULL, "do not actually run any commands", false, handle_exec}, {'n', "no-act", NULL, "do not actually run any commands", false, handle_exec},
{'v', "verbose", NULL, "show what commands are being run", false, handle_exec}, {'v', "verbose", NULL, "show what commands are being run", false, handle_exec},
{'E', "executor-path", "executor-path PATH", "use PATH for executor directory", true, handle_exec}, {'E', "executor-path", "executor-path PATH", "use PATH for executor directory", true, handle_exec},
{'L', "no-lock", NULL, "do not use a lockfile to serialize state changes", false, handle_exec},
{'S', "state-file", "state-file FILE", "use FILE for state", true, handle_exec}, {'S', "state-file", "state-file FILE", "use FILE for state", true, handle_exec},
}; };

View file

@ -44,7 +44,7 @@ struct if_applet {
const char *desc; const char *desc;
int (*const main)(int argc, char *argv[]); int (*const main)(int argc, char *argv[]);
void (*const usage)(int status); void (*const usage)(int status);
const struct if_option_group *groups[4]; const struct if_option_group *groups[16];
}; };
extern char *argv0; extern char *argv0;