From: Henri Verbeet Subject: [PATCH vkd3d 2/5] vkd3d-compiler: Use getopt to parse command-line options. Message-Id: <20200702134316.14971-2-hverbeet@codeweavers.com> Date: Thu, 2 Jul 2020 18:13:13 +0430 Signed-off-by: Henri Verbeet --- 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 #include #include +#include #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 Write the output to .\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; } -- 2.11.0