]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
v4l_id: convert to the new option parser
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sat, 25 Apr 2026 09:58:50 +0000 (11:58 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sat, 25 Apr 2026 12:50:46 +0000 (14:50 +0200)
The commandline check is tightened to reject extra arguments.

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

index dc4d41af2bfab6c0fdc355999b8c58d474288e80..c5bfa935b25369eed8546ebfe1e447cf887be92c 100644 (file)
@@ -4,7 +4,6 @@
  */
 
 #include <fcntl.h>
-#include <getopt.h>
 #include <linux/videodev2.h>
 #include <stdio.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 "log.h"
 #include "main-func.h"
+#include "options.h"
 #include "string-util.h"
+#include "strv.h"
 #include "utf8.h"
 
 static const char *arg_device = NULL;
 
+static int help(void) {
+        _cleanup_(table_unrefp) Table *options = NULL;
+        int r;
+
+        r = option_parser_get_help_table(&options);
+        if (r < 0)
+                return r;
+
+        help_cmdline("[OPTIONS...] DEVICE");
+        help_abstract("Video4Linux device identification.");
+        help_section("Options:");
+
+        return table_print_or_warn(options);
+}
+
 static int parse_argv(int argc, char *argv[]) {
-        static const struct option options[] = {
-                { "help",     no_argument, NULL, 'h' },
-                { "version",  no_argument, NULL, 'v' },
-                {}
-        };
-        int c;
-
-        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
+        assert(argc >= 0);
+        assert(argv);
+
+        OptionParser state = { argc, argv };
+        const char *arg;
+
+        FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
                 switch (c) {
-                case 'h':
-                        printf("%s [OPTIONS...] DEVICE\n\n"
-                               "Video4Linux device identification.\n\n"
-                               "  -h --help     Show this help text\n"
-                               "     --version  Show package version\n",
-                               program_invocation_short_name);
-                        return 0;
-                case 'v':
+
+                OPTION_COMMON_HELP:
+                        return help();
+
+                OPTION_COMMON_VERSION:
                         return version();
-                case '?':
-                        return -EINVAL;
-                default:
-                        assert_not_reached();
                 }
 
-        if (!argv[optind])
+        char **args = option_parser_get_args(&state);
+        if (strv_length(args) != 1)
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                       "DEVICE argument missing.");
+                                       "Need exactly one DEVICE argument.");
 
-        arg_device = argv[optind];
+        arg_device = args[0];
         return 1;
 }