From 7c502bc09182958fa7268bfa792a338891c3df7b Mon Sep 17 00:00:00 2001 From: Pierre Lamot Date: Mon, 14 Jan 2019 15:09:36 +0100 Subject: [PATCH 2/4] accept windows style flags and splitted argument value - accept windows style flags: /opt or -opt - accept split arguments : -FhFilename.h or -Fh Filename --- fxc2.cpp | 194 +++++++++++++++++++++++++++---------------------------- 1 file changed, 95 insertions(+), 99 deletions(-) diff --git a/fxc2.cpp b/fxc2.cpp index 066608c..01a8d07 100755 --- a/fxc2.cpp +++ b/fxc2.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include @@ -59,7 +58,41 @@ void print_usage_toomany() { exit(1); } -int main(int argc, char* argv[]) +bool parseOpt( const char* option, int argc, const char** argv, int* index, char** argumentOption ) +{ + assert(option != NULL); + if (!index || *index >= argc) { + return false; + } + const char* argument = argv[*index]; + if (argument[0] == '-' || argument[0] == '/') + argument++; + else + return false; + + size_t optionSize = strlen(option); + if (strncmp(argument, option, optionSize) != 0) { + return false; + } + + if (argumentOption) { + argument += optionSize; + if (*argument == '\0') { + *index += 1; + if (*index >= argc) { + printf("Error: missing required argument for option %s\n", option); + return false; + } + *argumentOption = strdup(argv[*index]); + } else { + *argumentOption = strdup(argument); + } + } + *index += 1; + return true; +} + +int main(int argc, const char* argv[]) { // ==================================================================================== // Process Command Line Arguments @@ -71,114 +104,77 @@ int main(int argc, char* argv[]) char* entryPoint = NULL; char* variableName = NULL; char* outputFile = NULL; + char* defineOption = NULL; int numDefines = 1; D3D_SHADER_MACRO* defines = new D3D_SHADER_MACRO[numDefines]; defines[numDefines-1].Name = NULL; defines[numDefines-1].Definition = NULL; - int i, c; - static struct option longOptions[] = - { - /* These options set a flag. */ - {"nologo", no_argument, &verbose, 0}, - {0, 0, 0, 0} - }; - + int index = 1; while (1) { D3D_SHADER_MACRO* newDefines; - int optionIndex = 0; - c = getopt_long_only (argc, argv, "T:E:D:V:F:", - longOptions, &optionIndex); - /* Detect the end of the options. */ - if (c == -1) + if (index >= argc) break; - switch (c) - { - case 0: - //printf ("option -nologo (quiet)\n"); - //Technically, this is any flag we define in longOptions - break; - case 'T': - model = strdup(optarg); - if(verbose) { - printf ("option -T (Shader Model/Profile) with arg %s\n", optarg); - } - break; - case 'E': - entryPoint = strdup(optarg); - if(verbose) { - printf ("option -E (Entry Point) with arg %s\n", optarg); - } - break; - case 'D': - numDefines++; - //Copy the old array into the new array, but put the new definition at the beginning - newDefines = new D3D_SHADER_MACRO[numDefines]; - for(i=1; i