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
print_counter(const char *iface, const char *name, const char *value)
{
(void) iface;
if (show_label)
fprintf(stdout, "%s: %s\n", name, value);
else
@ -71,13 +72,10 @@ print_all_counters(const char *iface)
return code;
}
void
ifctrstat_list_counters(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet)
static void
ifctrstat_list_counters(const char *opt_arg)
{
(void) short_opt;
(void) opt;
(void) opt_arg;
(void) applet;
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);
}
void
ifctrstat_set_nolabel(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet)
static void
ifctrstat_set_nolabel(const char *opt_arg)
{
(void) short_opt;
(void) opt;
(void) opt_arg;
(void) applet;
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 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) applet;
switch (short_opt) {
case 'L':
(void) opt_arg;
listing = true;
break;
case 'P':
match_opts.pretty_print = true;
break;
case 's':
}
static void
set_show_state(const char *opt_arg)
{
(void) opt_arg;
listing_stat = true;
break;
case 'D':
}
static void
set_pretty_print(const char *opt_arg)
{
(void) opt_arg;
match_opts.pretty_print = true;
}
static void
set_output_dot(const char *opt_arg)
{
(void) opt_arg;
match_opts.dot = true;
break;
case 'p':
}
static void
set_property(const char *opt_arg)
{
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},
{'s', "state", NULL, "show configured state", false, set_show_state},
{'p', "property", "property PROPERTY", "print values of properties matching PROPERTY", true, set_property},
{'D', "dot", NULL, "generate a dependency graph", false, set_output_dot},
{'L', "list", NULL, "list matching interfaces", false, set_listing},
{'P', "pretty-print", NULL, "pretty print the interfaces instead of just listing", false, set_pretty_print},
};
static struct if_option_group local_option_group = {

View file

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

View file

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

View file

@ -21,6 +21,8 @@
#include <getopt.h>
#include "cmd/multicall.h"
extern const struct if_applet *self_applet;
void
generic_usage(const struct if_applet *applet, int result)
{
@ -66,13 +68,11 @@ generic_usage(const struct if_applet *applet, int result)
}
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;
generic_usage(applet, EXIT_SUCCESS);
generic_usage(self_applet, EXIT_SUCCESS);
}
static struct if_option global_options[] = {
@ -154,6 +154,6 @@ process_options(const struct if_applet *applet, int argc, char *argv[])
if (opt == NULL)
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 *desc;
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 {