udevadm-test: convert to OPTION macros
Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+45
-44
@@ -3,7 +3,6 @@
|
||||
* Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
|
||||
*/
|
||||
|
||||
#include <getopt.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -12,7 +11,10 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "device-private.h"
|
||||
#include "format-table.h"
|
||||
#include "help-util.h"
|
||||
#include "log.h"
|
||||
#include "options.h"
|
||||
#include "parse-argument.h"
|
||||
#include "static-destruct.h"
|
||||
#include "strv.h"
|
||||
@@ -33,55 +35,59 @@ static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_extra_rules_dir, strv_freep);
|
||||
|
||||
static int help(void) {
|
||||
_cleanup_(table_unrefp) Table *options = NULL;
|
||||
int r;
|
||||
|
||||
printf("%s test [OPTIONS] DEVPATH\n\n"
|
||||
"Test an event run.\n\n"
|
||||
" -h --help Show this help\n"
|
||||
" -V --version Show package version\n"
|
||||
" -a --action=ACTION|help Set action string\n"
|
||||
" -N --resolve-names=early|late|never When to resolve names\n"
|
||||
" -D --extra-rules-dir=DIR Also load rules from the directory\n"
|
||||
" -v --verbose Show verbose logs\n"
|
||||
" --json=pretty|short|off Generate JSON output\n",
|
||||
program_invocation_short_name);
|
||||
r = option_parser_get_help_table_ns("udevadm-test", &options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
help_cmdline("test [OPTIONS] DEVPATH");
|
||||
help_abstract("Test an event run.");
|
||||
help_section("Options:");
|
||||
r = table_print_or_warn(options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
help_man_page_reference("udevadm", "8");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_argv(int argc, char *argv[]) {
|
||||
enum {
|
||||
ARG_JSON = 0x100,
|
||||
};
|
||||
int r;
|
||||
|
||||
static const struct option options[] = {
|
||||
{ "action", required_argument, NULL, 'a' },
|
||||
{ "resolve-names", required_argument, NULL, 'N' },
|
||||
{ "extra-rules-dir", required_argument, NULL, 'D' },
|
||||
{ "verbose", no_argument, NULL, 'v' },
|
||||
{ "json", required_argument, NULL, ARG_JSON },
|
||||
{ "version", no_argument, NULL, 'V' },
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{}
|
||||
};
|
||||
assert(argc >= 0);
|
||||
assert(argv);
|
||||
|
||||
int r, c;
|
||||
OptionParser opts = { argc, argv, .namespace = "udevadm-test" };
|
||||
|
||||
while ((c = getopt_long(argc, argv, "a:N:D:vVh", options, NULL)) >= 0)
|
||||
FOREACH_OPTION(c, &opts, /* on_error= */ return c)
|
||||
switch (c) {
|
||||
case 'a':
|
||||
r = parse_device_action(optarg, &arg_action);
|
||||
|
||||
OPTION_NAMESPACE("udevadm-test"): {}
|
||||
|
||||
OPTION_COMMON_HELP:
|
||||
return help();
|
||||
|
||||
OPTION('V', "version", NULL, "Show package version"):
|
||||
return print_version();
|
||||
|
||||
OPTION('a', "action", "ACTION|help", "Set action string"):
|
||||
r = parse_device_action(opts.arg, &arg_action);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
break;
|
||||
case 'N':
|
||||
r = parse_resolve_name_timing(optarg, &arg_resolve_name_timing);
|
||||
|
||||
OPTION_COMMON_RESOLVE_NAMES:
|
||||
r = parse_resolve_name_timing(opts.arg, &arg_resolve_name_timing);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
break;
|
||||
case 'D': {
|
||||
|
||||
OPTION('D', "extra-rules-dir", "DIR", "Also load rules from the directory"): {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
|
||||
r = parse_path_argument(optarg, /* suppress_root= */ false, &p);
|
||||
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &p);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -90,25 +96,20 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
return log_oom();
|
||||
break;
|
||||
}
|
||||
case 'v':
|
||||
|
||||
OPTION('v', "verbose", NULL, "Show verbose logs"):
|
||||
arg_verbose = true;
|
||||
break;
|
||||
case ARG_JSON:
|
||||
r = parse_json_argument(optarg, &arg_json_format_flags);
|
||||
|
||||
OPTION_COMMON_JSON:
|
||||
r = parse_json_argument(opts.arg, &arg_json_format_flags);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
break;
|
||||
case 'V':
|
||||
return print_version();
|
||||
case 'h':
|
||||
return help();
|
||||
case '?':
|
||||
return -EINVAL;
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
arg_syspath = argv[optind];
|
||||
char **args = option_parser_get_args(&opts);
|
||||
arg_syspath = args[0];
|
||||
if (!arg_syspath)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "syspath parameter missing.");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user