]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mount: use getopt_long to parse command options
authorNikhilkumar Shelke <nshelke@redhat.com>
Tue, 25 Jan 2022 18:07:24 +0000 (23:37 +0530)
committerNikhilkumar Shelke <nshelke@redhat.com>
Tue, 25 Jan 2022 18:07:24 +0000 (23:37 +0530)
Fixes: https://tracker.ceph.com/issues/53903
Signed-off-by: Nikhilkumar Shelke <nshelke@redhat.com>
src/mount/mount.ceph.c

index abbbe80cb932f07c50f6b86528676326a6c96867..87557112befc81bef001ed95b5afa25c1796c5fa 100644 (file)
@@ -7,6 +7,7 @@
 #include <sys/mman.h>
 #include <wait.h>
 #include <cap-ng.h>
+#include <getopt.h>
 
 #include "common/module.h"
 #include "common/secret.h"
@@ -620,7 +621,15 @@ out:
 static int parse_arguments(int argc, char *const *const argv,
                const char **src, const char **node, const char **opts)
 {
-       int i;
+       int opt = 0;
+       static struct option long_options[] = {
+               { "help",    no_argument,        0,  'h' },
+               { "no-mtab", no_argument,        0,  'n' },
+               { "verbose", no_argument,        0,  'v' },
+               { "fake",    no_argument,        0,  'f' },
+               { "options", required_argument,  0,  'o' },
+               { 0,         0,                  0,  0   }
+       };
 
        if (argc < 2) {
                // There were no arguments. Just show the usage.
@@ -639,25 +648,25 @@ static int parse_arguments(int argc, char *const *const argv,
 
        // Parse the remaining options
        *opts = EMPTY_STRING;
-       for (i = 3; i < argc; ++i) {
-               if (!strcmp("-h", argv[i]))
-                       return 1;
-               else if (!strcmp("-n", argv[i]))
-                       skip_mtab_flag = true;
-               else if (!strcmp("-v", argv[i]))
-                       verboseflag = true;
-               else if (!strcmp("-f", argv[i]))
-                       fakeflag = true;
-               else if (!strcmp("-o", argv[i])) {
-                       ++i;
-                       if (i >= argc) {
-                               fprintf(stderr, "Option -o requires an argument.\n\n");
+       while ((opt = getopt_long(argc, argv, "hnvfo:",
+                                       long_options, NULL)) != -1) {
+               switch (opt) {
+                       case 'h' : // -h or --help
+                               return 1;
+                       case 'n' : // -n or --no-mtab
+                               skip_mtab_flag = true;
+                               break;
+                       case 'v' : // -v or --verbose
+                               verboseflag = true;
+                               break;
+                       case 'f' : // -f or --fake
+                               fakeflag = true;
+                               break;
+                       case 'o' : // -o or --options
+                               *opts = optarg;
+                               break;
+                       default:
                                return -EINVAL;
-                       }
-                       *opts = argv[i];
-                } else {
-                       fprintf(stderr, "Can't understand option: '%s'\n\n", argv[i]);
-                       return -EINVAL;
                }
        }
        return 0;
@@ -679,11 +688,11 @@ static void usage(const char *prog_name)
        printf("usage: %s [src] [mount-point] [-n] [-v] [-o ceph-options]\n",
                prog_name);
        printf("options:\n");
-       printf("\t-hPrint this help\n");
-       printf("\t-nDo not update /etc/mtab\n");
-       printf("\t-vVerbose\n");
-       printf("\t-fFake mount, do not actually mount\n");
-       printf("\tceph-options: refer to mount.ceph(8)\n");
+       printf("\t-h, --help\tPrint this help\n");
+       printf("\t-n, --no-mtab\tDo not update /etc/mtab\n");
+       printf("\t-v, --verbose\tVerbose\n");
+       printf("\t-f, --fake\tFake mount, do not actually mount\n");
+       printf("ceph-options: refer to mount.ceph(8)\n");
        printf("\n");
 }