]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udevadm-lock: convert to OPTION macros
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Tue, 28 Apr 2026 09:31:51 +0000 (11:31 +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-lock.c

index 483b64973d40160c12c35c5cd58802b0e0dc2f43..cebce08007eb0f29022bd0ead94b442bbf1c1fba 100644 (file)
@@ -1,6 +1,5 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 
-#include <getopt.h>
 #include <sys/file.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -9,11 +8,14 @@
 #include "device-util.h"
 #include "fd-util.h"
 #include "fdset.h"
+#include "format-table.h"
+#include "glyph-util.h"
 #include "hash-funcs.h"
+#include "help-util.h"
 #include "lock-util.h"
+#include "options.h"
 #include "path-util.h"
 #include "pidref.h"
-#include "pretty-print.h"
 #include "process-util.h"
 #include "signal-util.h"
 #include "sort-util.h"
@@ -33,70 +35,52 @@ STATIC_DESTRUCTOR_REGISTER(arg_backing, strv_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_cmdline, strv_freep);
 
 static int help(void) {
-        _cleanup_free_ char *link = NULL;
+        _cleanup_(table_unrefp) Table *options = NULL;
         int r;
 
-        r = terminal_urlify_man("udevadm", "8", &link);
+        r = option_parser_get_help_table_ns("udevadm-lock", &options);
         if (r < 0)
-                return log_oom();
+                return r;
 
-        printf("%s [OPTIONS...] COMMAND\n"
-               "%s [OPTIONS...] --print\n"
-               "\n%sLock a block device and run a command.%s\n\n"
-               "  -h --help            Print this message\n"
-               "  -V --version         Print version of the program\n"
-               "  -d --device=DEVICE   Block device to lock\n"
-               "  -b --backing=FILE    File whose backing block device to lock\n"
-               "  -t --timeout=SECS    Block at most the specified time waiting for lock\n"
-               "  -p --print           Only show which block device the lock would be taken on\n"
-               "\nSee the %s for details.\n",
-               program_invocation_short_name,
-               program_invocation_short_name,
-               ansi_highlight(),
-               ansi_normal(),
-               link);
+        help_cmdline("lock [OPTIONS...] COMMAND");
+        help_cmdline("lock [OPTIONS...] --print");
+        help_abstract("Lock a block device and run a command.");
+        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[]) {
-
-        static const struct option options[] = {
-                { "help",    no_argument,       NULL, 'h'      },
-                { "version", no_argument,       NULL, 'V'      },
-                { "device",  required_argument, NULL, 'd'      },
-                { "backing", required_argument, NULL, 'b'      },
-                { "timeout", required_argument, NULL, 't'      },
-                { "print",   no_argument,       NULL, 'p'      },
-                {}
-        };
-
-        int c, r;
+        int r;
 
         assert(argc >= 0);
         assert(argv);
 
-        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
-         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
-        optind = 0;
-        while ((c = getopt_long(argc, argv, arg_print ? "hVd:b:t:p" : "+hVd:b:t:p", options, NULL)) >= 0)
+        OptionParser opts = { argc, argv, OPTION_PARSER_STOP_AT_FIRST_NONOPTION, "udevadm-lock" };
 
+        FOREACH_OPTION(c, &opts, /* on_error= */ return c)
                 switch (c) {
 
-                case 'h':
+                OPTION_NAMESPACE("udevadm-lock"): {}
+
+                OPTION_COMMON_HELP:
                         return help();
 
-                case 'V':
+                OPTION('V', "version", NULL, "Show package version"):
                         return print_version();
 
-                case 'd':
-                case 'b': {
+                OPTION('d', "device", "DEVICE", "Block device to lock"): {} /* fall through */
+                OPTION('b', "backing", "FILE", "File whose backing block device to lock"): {
                         _cleanup_free_ char *s = NULL;
-                        char ***l = c == 'd' ? &arg_devices : &arg_backing;
+                        char ***l = opts.opt->short_code == 'd' ? &arg_devices : &arg_backing;
 
-                        r = path_make_absolute_cwd(optarg, &s);
+                        r = path_make_absolute_cwd(opts.arg, &s);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to make path '%s' absolute: %m", optarg);
+                                return log_error_errno(r, "Failed to make path '%s' absolute: %m", opts.arg);
 
                         path_simplify(s);
 
@@ -107,31 +91,26 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
                 }
 
-                case 't':
-                        r = parse_sec(optarg, &arg_timeout_usec);
+                OPTION('t', "timeout", "SECS", "Block at most the specified time waiting for lock"):
+                        r = parse_sec(opts.arg, &arg_timeout_usec);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to parse --timeout= parameter: %s", optarg);
+                                return log_error_errno(r, "Failed to parse --timeout= parameter: %s", opts.arg);
                         break;
 
-                case 'p':
+                OPTION('p', "print", NULL, "Only show which block device the lock would be taken on"):
                         arg_print = true;
                         break;
-
-                case '?':
-                        return -EINVAL;
-
-                default:
-                        assert_not_reached();
                 }
 
+        char **args = option_parser_get_args(&opts);
         if (arg_print) {
-                if (optind != argc)
+                if (!strv_isempty(args))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No arguments expected.");
         } else {
-                if (optind + 1 > argc)
+                if (strv_isempty(args))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments, command to execute.");
 
-                arg_cmdline = strv_copy(argv + optind);
+                arg_cmdline = strv_copy(args);
                 if (!arg_cmdline)
                         return log_oom();
         }