]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udevadm-test-builtin: convert to OPTION macros
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Tue, 28 Apr 2026 09:01:28 +0000 (11:01 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Wed, 29 Apr 2026 12:00:41 +0000 (14:00 +0200)
Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
src/udev/udevadm-test-builtin.c

index f17df9a7d51a27e1f9790d390cba51159c9ef9d1..31ac5699570170bdeb44d9bfdae37dbb69bf623b 100644 (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;
 
-        udev_builtin_list();
+        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;
 }