]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
pki: Pre-process common arguments
authorTobias Brunner <tobias@strongswan.org>
Tue, 31 Oct 2023 08:17:05 +0000 (09:17 +0100)
committerTobias Brunner <tobias@strongswan.org>
Mon, 13 Nov 2023 11:50:47 +0000 (12:50 +0100)
This way the position of --debug doesn't matter for it to apply to the
parsing of all command-specific arguments.

src/pki/command.c

index 5dd53e9dec572245581a8089a994b3f91276a279..accec5fe51b59c7fa088596ece5899ee586d0a75 100644 (file)
@@ -116,20 +116,41 @@ int command_getopt(char **arg)
        {
                op = getopt_long(argc, argv, command_optstring, command_opts, NULL);
                switch (op)
+               {
+                       case '+':
+                       case 'v':
+                               continue;
+                       default:
+                               *arg = optarg;
+                               return op;
+               }
+       }
+}
+
+/**
+ * Process options common for all commands
+ */
+static bool process_common_opts()
+{
+       while (TRUE)
+       {
+               switch (getopt_long(argc, argv, command_optstring, command_opts, NULL))
                {
                        case '+':
                                if (!options->from(options, optarg, &argc, &argv, optind))
                                {
-                                       /* a error value */
-                                       return 255;
+                                       return FALSE;
                                }
                                continue;
                        case 'v':
                                dbg_default_set_level(atoi(optarg));
                                continue;
                        default:
-                               *arg = optarg;
-                               return op;
+                               continue;
+                       case '?':
+                               return FALSE;
+                       case EOF:
+                               return TRUE;
                }
        }
 }
@@ -279,6 +300,11 @@ int command_dispatch(int c, char *v[])
                {
                        active = i;
                        build_opts();
+                       if (!process_common_opts())
+                       {
+                               return command_usage("invalid options");
+                       }
+                       optind = 2;
                        return cmds[i].call();
                }
        }