]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev/mtd_probe: implement --help
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 13 Mar 2023 10:46:01 +0000 (11:46 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 16 Mar 2023 15:46:44 +0000 (16:46 +0100)
src/udev/mtd_probe/mtd_probe.c

index a7210a05e3cc4f28bb73fa22cc96aee1054d5c8e..fd6267feb5015526e958a1203f6f7ef97e29359b 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <mtd/mtd-user.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "fd-util.h"
 #include "mtd_probe.h"
 
+static const char *arg_device = NULL;
+
+static int parse_argv(int argc, char *argv[]) {
+        static const struct option options[] = {
+                { "help",   no_argument, NULL, 'h' },
+                {}
+        };
+        int c;
+
+        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
+                switch (c) {
+                case 'h':
+                        printf("%s /dev/mtd[n]\n\n"
+                               "  -h --help     Show this help text\n",
+                               program_invocation_short_name);
+                        return 0;
+                case '?':
+                        return -EINVAL;
+                default:
+                        assert_not_reached();
+                }
+
+        if (argc > 2)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error: unexpected argument.");
+
+        arg_device = argv[optind];
+        return 1;
+}
+
 int main(int argc, char** argv) {
         _cleanup_close_ int mtd_fd = -EBADF;
         mtd_info_t mtd_info;
+        int r;
 
-        if (argc != 2) {
-                printf("usage: mtd_probe /dev/mtd[n]\n");
-                return EXIT_FAILURE;
-        }
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                return r < 0 ? EXIT_FAILURE : 0;
 
         mtd_fd = open(argv[1], O_RDONLY|O_CLOEXEC|O_NOCTTY);
         if (mtd_fd < 0) {