]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fido_id: convert to the new option parser
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sat, 25 Apr 2026 10:09:05 +0000 (12:09 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sun, 26 Apr 2026 10:34:07 +0000 (12:34 +0200)
The commandline check is tightened. Previously the program would
crash if called without an argument.

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

index aa918a9798460fe0c1b6d1acb5eff1facf8e9ebf..a301b9acdb0870f03cf45b96bd7c1a8e2843ee65 100644 (file)
@@ -7,7 +7,6 @@
  */
 
 #include <fcntl.h>
-#include <getopt.h>
 #include <linux/hid.h>
 #include <stdio.h>
 #include <unistd.h>
 #include "device-util.h"
 #include "fd-util.h"
 #include "fido_id_desc.h"
+#include "format-table.h"
+#include "help-util.h"
 #include "log.h"
 #include "main-func.h"
+#include "options.h"
 #include "path-util.h"
+#include "strv.h"
 #include "udev-util.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...] SYSFS_PATH");
+        help_abstract("Identify FIDO security tokens.");
+        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...] SYSFS_PATH\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 (argc > 2)
+        char **args = option_parser_get_args(&state);
+        if (strv_length(args) > 1)
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error: unexpected argument.");
 
-        arg_device = argv[optind];
+        arg_device = args[0];
         return 1;
 }