udevadm-test-builtin: convert to OPTION macros

Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-04-29 14:00:41 +02:00
parent ce4746f228
commit 5751647799
+38 -32
View File
@@ -1,11 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <getopt.h>
#include <stdio.h>
#include "device-private.h"
#include "device-util.h"
#include "format-table.h"
#include "help-util.h"
#include "log.h"
#include "options.h"
#include "udev-builtin.h"
#include "udevadm.h"
#include "udevadm-util.h"
@@ -15,51 +15,57 @@ static const char *arg_command = NULL;
static const char *arg_syspath = NULL;
static int help(void) {
printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
"Test a built-in command.\n\n"
" -h --help Print this message\n"
" -V --version Print version of the program\n"
" -a --action=ACTION|help Set action string\n"
"\nCommands:\n",
program_invocation_short_name);
_cleanup_(table_unrefp) Table *options = NULL;
int r;
r = option_parser_get_help_table_ns("udevadm-test-builtin", &options);
if (r < 0)
return r;
help_cmdline("test-builtin [OPTIONS] COMMAND DEVPATH");
help_abstract("Test a built-in command.");
help_section("Options:");
r = table_print_or_warn(options);
if (r < 0)
return r;
help_section("Commands:");
udev_builtin_list();
return 0;
}
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "action", required_argument, NULL, 'a' },
{ "version", no_argument, NULL, 'V' },
{ "help", no_argument, NULL, 'h' },
{}
};
int r;
int r, c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "a:Vh", options, NULL)) >= 0)
OptionParser opts = { argc, argv, .namespace = "udevadm-test-builtin" };
FOREACH_OPTION(c, &opts, /* on_error= */ return c)
switch (c) {
case 'a':
r = parse_device_action(optarg, &arg_action);
OPTION_NAMESPACE("udevadm-test-builtin"): {}
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 'V':
return print_version();
case 'h':
return help();
case '?':
return -EINVAL;
default:
assert_not_reached();
}
if (argc != optind + 2)
if (option_parser_get_n_args(&opts) != 2)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Expected two arguments: command string and device path.");
arg_command = ASSERT_PTR(argv[optind]);
arg_syspath = ASSERT_PTR(argv[optind+1]);
char **args = option_parser_get_args(&opts);
arg_command = ASSERT_PTR(args[0]);
arg_syspath = ASSERT_PTR(args[1]);
return 1;
}