Refactor how options are handled

This commit is contained in:
A. Wilcox 2020-08-17 14:59:19 -05:00
parent 1a11bb768f
commit 327e738a55
No known key found for this signature in database
GPG key ID: CB29CB51922B9D14
6 changed files with 112 additions and 98 deletions

View file

@ -43,6 +43,7 @@ counter_is_valid(const char *candidate)
static void static void
print_counter(const char *iface, const char *name, const char *value) print_counter(const char *iface, const char *name, const char *value)
{ {
(void) iface;
if (show_label) if (show_label)
fprintf(stdout, "%s: %s\n", name, value); fprintf(stdout, "%s: %s\n", name, value);
else else
@ -71,13 +72,10 @@ print_all_counters(const char *iface)
return code; return code;
} }
void static void
ifctrstat_list_counters(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) ifctrstat_list_counters(const char *opt_arg)
{ {
(void) short_opt;
(void) opt;
(void) opt_arg; (void) opt_arg;
(void) applet;
for (int i = 0; i < avail_counters_count; i++) for (int i = 0; i < avail_counters_count; i++)
{ {
@ -87,14 +85,10 @@ ifctrstat_list_counters(int short_opt, const struct if_option *opt, const char *
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
void static void
ifctrstat_set_nolabel(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) ifctrstat_set_nolabel(const char *opt_arg)
{ {
(void) short_opt;
(void) opt;
(void) opt_arg; (void) opt_arg;
(void) applet;
show_label = false; show_label = false;
} }

View file

@ -222,36 +222,45 @@ list_state(struct lif_dict *state, struct match_options *opts)
static bool listing = false, listing_stat = false; static bool listing = false, listing_stat = false;
static void static void
handle_local(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) set_listing(const char *opt_arg)
{ {
(void) opt; (void) opt_arg;
(void) applet; listing = true;
}
switch (short_opt) { static void
case 'L': set_show_state(const char *opt_arg)
listing = true; {
break; (void) opt_arg;
case 'P': listing_stat = true;
match_opts.pretty_print = true; }
break;
case 's': static void
listing_stat = true; set_pretty_print(const char *opt_arg)
break; {
case 'D': (void) opt_arg;
match_opts.dot = true; match_opts.pretty_print = true;
break; }
case 'p':
match_opts.property = opt_arg; static void
break; set_output_dot(const char *opt_arg)
} {
(void) opt_arg;
match_opts.dot = true;
}
static void
set_property(const char *opt_arg)
{
match_opts.property = opt_arg;
} }
static struct if_option local_options[] = { static struct if_option local_options[] = {
{'s', "state", NULL, "show configured state", false, handle_local}, {'s', "state", NULL, "show configured state", false, set_show_state},
{'p', "property", "property PROPERTY", "print values of properties matching PROPERTY", true, handle_local}, {'p', "property", "property PROPERTY", "print values of properties matching PROPERTY", true, set_property},
{'D', "dot", NULL, "generate a dependency graph", false, handle_local}, {'D', "dot", NULL, "generate a dependency graph", false, set_output_dot},
{'L', "list", NULL, "list matching interfaces", false, handle_local}, {'L', "list", NULL, "list matching interfaces", false, set_listing},
{'P', "pretty-print", NULL, "pretty print the interfaces instead of just listing", false, handle_local}, {'P', "pretty-print", NULL, "pretty print the interfaces instead of just listing", false, set_pretty_print},
}; };
static struct if_option_group local_option_group = { static struct if_option_group local_option_group = {

View file

@ -27,47 +27,60 @@ struct lif_execute_opts exec_opts = {
.state_file = STATE_FILE .state_file = STATE_FILE
}; };
static void handle_exec(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) static void
set_interfaces_file(const char *opt_arg)
{ {
(void) opt; exec_opts.interfaces_file = opt_arg;
(void) applet; }
switch (short_opt) static void
{ set_state_file(const char *opt_arg)
case 'i': {
exec_opts.interfaces_file = opt_arg; exec_opts.state_file = opt_arg;
break; }
case 'S':
exec_opts.state_file = opt_arg; static void
break; set_no_act(const char *opt_arg)
case 'n': {
exec_opts.mock = true; (void) opt_arg;
exec_opts.verbose = true; exec_opts.mock = true;
break; exec_opts.verbose = true;
case 'v': }
exec_opts.verbose = true;
break; static void
case 'E': set_verbose(const char *opt_arg)
exec_opts.executor_path = opt_arg; {
break; (void) opt_arg;
case 'f': exec_opts.verbose = true;
break; }
case 'l':
exec_opts.no_lock = true; static void
break; set_executor_path(const char *opt_arg)
default: {
break; exec_opts.executor_path = opt_arg;
} }
static void
set_no_lock(const char *opt_arg)
{
(void) opt_arg;
exec_opts.no_lock = true;
}
static void
no_op(const char *opt_arg)
{
(void) opt_arg;
} }
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, no_op},
{'i', "interfaces", "interfaces FILE", "use FILE for interface definitions", true, handle_exec}, {'i', "interfaces", "interfaces FILE", "use FILE for interface definitions", true, set_interfaces_file},
{'l', "no-lock", NULL, "do not use a lockfile to serialize state changes", false, handle_exec}, {'l', "no-lock", NULL, "do not use a lockfile to serialize state changes", false, set_no_lock},
{'n', "no-act", NULL, "do not actually run any commands", false, handle_exec}, {'n', "no-act", NULL, "do not actually run any commands", false, set_no_act},
{'v', "verbose", NULL, "show what commands are being run", false, handle_exec}, {'v', "verbose", NULL, "show what commands are being run", false, set_verbose},
{'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, set_executor_path},
{'S', "state-file", "state-file FILE", "use FILE for state", true, handle_exec}, {'S', "state-file", "state-file FILE", "use FILE for state", true, set_state_file},
}; };
struct if_option_group exec_option_group = { struct if_option_group exec_option_group = {

View file

@ -23,31 +23,29 @@
struct match_options match_opts = {}; struct match_options match_opts = {};
static void handle_match(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet) static void
set_auto(const char *opt_arg)
{ {
(void) opt; (void) opt_arg;
(void) applet; match_opts.is_auto = true;
}
switch (short_opt) static void
{ set_include_pattern(const char *opt_arg)
case 'a': {
match_opts.is_auto = true; match_opts.include_pattern = opt_arg;
break; }
case 'I':
match_opts.include_pattern = opt_arg; static void
break; set_exclude_pattern(const char *opt_arg)
case 'X': {
match_opts.exclude_pattern = opt_arg; match_opts.include_pattern = opt_arg;
break;
default:
break;
}
} }
static struct if_option match_options[] = { static struct if_option match_options[] = {
{'a', "auto", NULL, "only match against interfaces hinted as 'auto'", false, handle_match}, {'a', "auto", NULL, "only match against interfaces hinted as 'auto'", false, set_auto},
{'I', "include", "include PATTERN", "only match against interfaces matching PATTERN", true, handle_match}, {'I', "include", "include PATTERN", "only match against interfaces matching PATTERN", true, set_include_pattern},
{'X', "exclude", "exclude PATTERN", "never match against interfaces matching PATTERN", true, handle_match}, {'X', "exclude", "exclude PATTERN", "never match against interfaces matching PATTERN", true, set_exclude_pattern},
}; };
struct if_option_group match_option_group = { struct if_option_group match_option_group = {

View file

@ -21,6 +21,8 @@
#include <getopt.h> #include <getopt.h>
#include "cmd/multicall.h" #include "cmd/multicall.h"
extern const struct if_applet *self_applet;
void void
generic_usage(const struct if_applet *applet, int result) generic_usage(const struct if_applet *applet, int result)
{ {
@ -66,13 +68,11 @@ generic_usage(const struct if_applet *applet, int result)
} }
static void static void
generic_usage_request(int short_opt, const struct if_option *option, const char *opt_arg, const struct if_applet *applet) generic_usage_request(const char *opt_arg)
{ {
(void) short_opt;
(void) option;
(void) opt_arg; (void) opt_arg;
generic_usage(applet, EXIT_SUCCESS); generic_usage(self_applet, EXIT_SUCCESS);
} }
static struct if_option global_options[] = { static struct if_option global_options[] = {
@ -154,6 +154,6 @@ process_options(const struct if_applet *applet, int argc, char *argv[])
if (opt == NULL) if (opt == NULL)
break; break;
opt->handle(c, opt, optarg, applet); opt->handle(optarg);
} }
} }

View file

@ -30,7 +30,7 @@ struct if_option {
const char *long_opt_desc; 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)(const char *opt_arg);
}; };
struct if_option_group { struct if_option_group {