]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cdrom_id: convert to the new option parser
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sat, 25 Apr 2026 10:33:03 +0000 (12:33 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sun, 26 Apr 2026 10:34:13 +0000 (12:34 +0200)
The commandline check is tightened to reject extra arguments.

Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
src/udev/cdrom_id/cdrom_id.c

index d311f2b3222ec0c8b321fead11fe2af914b7d052..1a167475d20d8c76dfcab365c776fc06e3be1dc7 100644 (file)
@@ -4,7 +4,6 @@
  */
 
 #include <fcntl.h>
-#include <getopt.h>
 #include <linux/cdrom.h>
 #include <scsi/sg.h>
 #include <sys/ioctl.h>
 #include "build.h"
 #include "errno-util.h"
 #include "fd-util.h"
+#include "format-table.h"
+#include "help-util.h"
 #include "main-func.h"
+#include "options.h"
 #include "random-util.h"
 #include "sort-util.h"
 #include "string-table.h"
 #include "string-util.h"
+#include "strv.h"
 #include "time-util.h"
 #include "udev-util.h"
 #include "unaligned.h"
@@ -898,60 +901,63 @@ static void print_properties(const Context *c) {
 }
 
 static int help(void) {
-        printf("%s [OPTIONS...] DEVICE\n\n"
-               "  -l --lock-media    Lock the media (to enable eject request events)\n"
-               "  -u --unlock-media  Unlock the media\n"
-               "  -e --eject-media   Eject the media\n"
-               "  -d --debug         Print debug messages to stderr\n"
-               "  -h --help          Show this help text\n"
-               "     --version       Show package version\n",
-               program_invocation_short_name);
+        _cleanup_(table_unrefp) Table *options = NULL;
+        int r;
 
-        return 0;
+        r = option_parser_get_help_table(&options);
+        if (r < 0)
+                return r;
+
+        help_cmdline("[OPTIONS...] DEVICE");
+        help_section("Options:");
+
+        return table_print_or_warn(options);
 }
 
 static int parse_argv(int argc, char *argv[]) {
-        static const struct option options[] = {
-                { "lock-media",   no_argument, NULL, 'l' },
-                { "unlock-media", no_argument, NULL, 'u' },
-                { "eject-media",  no_argument, NULL, 'e' },
-                { "debug",        no_argument, NULL, 'd' },
-                { "help",         no_argument, NULL, 'h' },
-                { "version",      no_argument, NULL, 'v' },
-                {}
-        };
-        int c;
+        assert(argc >= 0);
+        assert(argv);
+
+        OptionParser state = { argc, argv };
+        const char *arg;
 
-        while ((c = getopt_long(argc, argv, "deluh", options, NULL)) >= 0)
+        FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
                 switch (c) {
-                case 'l':
+
+                OPTION_COMMON_HELP:
+                        return help();
+
+                OPTION_COMMON_VERSION:
+                        return version();
+
+                OPTION('l', "lock-media", NULL,
+                       "Lock the media (to enable eject request events)"):
                         arg_lock = true;
                         break;
-                case 'u':
+
+                OPTION('u', "unlock-media", NULL,
+                       "Unlock the media"):
                         arg_unlock = true;
                         break;
-                case 'e':
+
+                OPTION('e', "eject-media", NULL,
+                       "Eject the media"):
                         arg_eject = true;
                         break;
-                case 'd':
+
+                OPTION('d', "debug", NULL,
+                       "Print debug messages to stderr"):
                         log_set_target(LOG_TARGET_CONSOLE);
                         log_set_max_level(LOG_DEBUG);
                         log_open();
                         break;
-                case 'h':
-                        return help();
-                case 'v':
-                        return version();
-                case '?':
-                        return -EINVAL;
-                default:
-                        assert_not_reached();
                 }
 
-        arg_node = argv[optind];
-        if (!arg_node)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No device specified.");
+        char **args = option_parser_get_args(&state);
+        if (strv_length(args) != 1)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Need exactly one DEVICE argument.");
 
+        arg_node = args[0];
         return 1;
 }