Henri Verbeet : vkd3d-compiler: Use getopt to parse command-line options.

Alexandre Julliard julliard at winehq.org
Thu Jul 2 14:57:09 CDT 2020


Module: vkd3d
Branch: master
Commit: f658190794f02ce035ce12481c1c4135c9ac33d6
URL:    https://source.winehq.org/git/vkd3d.git/?a=commit;h=f658190794f02ce035ce12481c1c4135c9ac33d6

Author: Henri Verbeet <hverbeet at codeweavers.com>
Date:   Thu Jul  2 18:31:13 2020 +0430

vkd3d-compiler: Use getopt to parse command-line options.

Signed-off-by: Henri Verbeet <hverbeet at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 programs/vkd3d-compiler/main.c | 61 ++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 29 deletions(-)

diff --git a/programs/vkd3d-compiler/main.c b/programs/vkd3d-compiler/main.c
index 03dc322..8fa4d4f 100644
--- a/programs/vkd3d-compiler/main.c
+++ b/programs/vkd3d-compiler/main.c
@@ -22,12 +22,18 @@
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <getopt.h>
 
 #include "vkd3d_common.h"
 #include "vkd3d_shader.h"
 
 #define MAX_COMPILE_OPTIONS 1
 
+enum
+{
+    OPTION_STRIP_DEBUG = CHAR_MAX + 1,
+};
+
 static bool read_shader(struct vkd3d_shader_code *shader, const char *filename)
 {
     struct stat st;
@@ -86,23 +92,15 @@ static bool write_shader(const struct vkd3d_shader_code *shader, const char *fil
     return true;
 }
 
-static const struct
-{
-    const char *name;
-    enum vkd3d_shader_compile_option_name compile_option;
-}
-compiler_options[] =
-{
-    {"--strip-debug", VKD3D_SHADER_COMPILE_OPTION_STRIP_DEBUG},
-};
-
 static void print_usage(const char *program_name)
 {
     static const char usage[] =
         "[options...] file\n"
         "Options:\n"
         "  -o <file>            Write the output to <file>.\n"
-        "  --strip-debug        Strip debug information from the output.\n";
+        "  --strip-debug        Strip debug information from the output.\n"
+        "  --                   Stop option processing. Any subsequent argument is\n"
+        "                       interpreted as a filename.\n";
 
     fprintf(stderr, "Usage: %s %s", program_name, usage);
 }
@@ -145,36 +143,41 @@ static void add_compile_option(struct options *options,
 
 static bool parse_command_line(int argc, char **argv, struct options *options)
 {
-    unsigned int i, j;
+    int option;
 
-    if (argc < 2)
-        return false;
+    static struct option long_options[] =
+    {
+        {"strip-debug", no_argument, NULL, OPTION_STRIP_DEBUG},
+        {NULL,          0,           NULL, 0},
+    };
 
     memset(options, 0, sizeof(*options));
 
-    for (i = 1; i < argc - 1; ++i)
+    for (;;)
     {
-        if (!strcmp(argv[i], "-o"))
-        {
-            if (i + 1 >= argc - 1)
-                return false;
-            options->output_filename = argv[++i];
-            continue;
-        }
+        if ((option = getopt_long(argc, argv, "o:", long_options, NULL)) == -1)
+            break;
 
-        for (j = 0; j < ARRAY_SIZE(compiler_options); ++j)
+        switch (option)
         {
-            if (!strcmp(argv[i], compiler_options[j].name))
-            {
-                add_compile_option(options, compiler_options[j].compile_option, 1);
+            case 'o':
+                options->output_filename = optarg;
+                break;
+
+            case OPTION_STRIP_DEBUG:
+                add_compile_option(options, VKD3D_SHADER_COMPILE_OPTION_STRIP_DEBUG, 1);
                 break;
-            }
+
+            default:
+                return false;
         }
-        if (j == ARRAY_SIZE(compiler_options))
-            return false;
     }
 
+    if (optind >= argc)
+        return false;
+
     options->filename = argv[argc - 1];
+
     return true;
 }
 




More information about the wine-cvs mailing list