From f6d9f652485b3681210fe4f4af357761cc38a7aa Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Tue, 11 Aug 2020 23:03:55 -0600 Subject: [PATCH] ifquery: port to use option groups --- cmd/ifquery.c | 88 +++++++++++++++++------------------- cmd/multicall-exec-options.c | 4 +- cmd/multicall.h | 2 +- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/cmd/ifquery.c b/cmd/ifquery.c index 89c84d4..dc9cfbe 100644 --- a/cmd/ifquery.c +++ b/cmd/ifquery.c @@ -164,17 +164,6 @@ ifquery_usage(int status) fprintf(stderr, "usage: ifquery [options] \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); } @@ -239,45 +228,52 @@ list_state(struct lif_dict *state, struct match_options *opts) } } +static bool listing = false, listing_stat = false; + +static void +handle_local(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) +{ + (void) opt; + (void) applet; + + switch (short_opt) { + case 'L': + listing = true; + break; + case 'P': + match_opts.pretty_print = true; + break; + case 's': + listing_stat = true; + break; + case 'D': + match_opts.dot = true; + break; + case 'p': + match_opts.property = opt_arg; + 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 = {}; - 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 (;;) - { - int c = getopt_long(argc, argv, "hVi:LaI:X:PS:sDp:E:", long_options, NULL); - if (c == -1) - break; - - switch (c) { - case 'L': - listing = true; - break; - case 'P': - match_opts.pretty_print = true; - break; - case 's': - listing_stat = true; - break; - case 'D': - match_opts.dot = true; - break; - case 'p': - match_opts.property = optarg; - break; - } - } if (!lif_state_read_path(&state, exec_opts.state_file)) { @@ -342,5 +338,5 @@ struct if_applet ifquery_applet = { .desc = "query interface configuration", .main = ifquery_main, .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 }, }; diff --git a/cmd/multicall-exec-options.c b/cmd/multicall-exec-options.c index f043167..0ebe85d 100644 --- a/cmd/multicall-exec-options.c +++ b/cmd/multicall-exec-options.c @@ -52,7 +52,7 @@ static void handle_exec(int short_opt, const struct if_option *opt, const char * break; case 'f': break; - case 'L': + case 'l': exec_opts.no_lock = true; break; 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[] = { {'f', "force", NULL, "force (de)configuration", 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}, {'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}, - {'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}, }; diff --git a/cmd/multicall.h b/cmd/multicall.h index 20d2c20..cfbadd5 100644 --- a/cmd/multicall.h +++ b/cmd/multicall.h @@ -44,7 +44,7 @@ struct if_applet { const char *desc; int (*const main)(int argc, char *argv[]); void (*const usage)(int status); - const struct if_option_group *groups[4]; + const struct if_option_group *groups[16]; }; extern char *argv0;