yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
eaa8dcfcc
master
1#include "options.h" 2 3#include "diagnostics.h" 4 5namespace CppParse 6{ 7 8SlangResult OptionsParser ::_parseArgFlag (const char * option ,bool & outFlag ) 9{ 10SLANG_ASSERT (UnownedStringSlice (m_args [m_index ])== option ); 11SLANG_ASSERT (m_index < m_argCount ); 12 13m_index ++ ; 14outFlag = true; 15return SLANG_OK ; 16} 17 18SlangResult OptionsParser ::_parseArgWithValue (const char * option ,String & ioValue ) 19{ 20SLANG_ASSERT (UnownedStringSlice (m_args [m_index ])== option ); 21if (m_index + 1 < m_argCount ) 22 { 23// Next parameter is the output path, there can only be one 24if (ioValue .getLength ()) 25 { 26// There already is output 27m_sink -> diagnose (SourceLoc (),CPPDiagnostics ::optionAlreadyDefined ,option ,ioValue ); 28return SLANG_FAIL ; 29 } 30 } 31else 32 { 33m_sink -> diagnose (SourceLoc (),CPPDiagnostics ::requireValueAfterOption ,option ); 34return SLANG_FAIL ; 35 } 36 37ioValue = m_args [m_index + 1 ]; 38m_index += 2 ; 39return SLANG_OK ; 40} 41 42SlangResult OptionsParser ::_parseArgReplaceValue (const char * option ,String & ioValue ) 43{ 44SLANG_ASSERT (UnownedStringSlice (m_args [m_index ])== option ); 45if (m_index + 1 >=m_argCount ) 46 { 47m_sink -> diagnose (SourceLoc (),CPPDiagnostics ::requireValueAfterOption ,option ); 48return SLANG_FAIL ; 49 } 50 51ioValue = m_args [m_index + 1 ]; 52m_index += 2 ; 53return SLANG_OK ; 54} 55 56SlangResult OptionsParser ::parse ( 57int argc , 58const char * const * argv , 59DiagnosticSink * sink , 60Options & outOptions ) 61{ 62outOptions .reset (); 63 64m_index = 0 ; 65m_argCount = argc ; 66m_args = argv ; 67m_sink = sink ; 68 69outOptions .reset (); 70 71while (m_index < m_argCount ) 72 { 73const UnownedStringSlice arg = UnownedStringSlice (argv [m_index ]); 74 75if (arg .getLength ()> 0 && arg [0 ]== '-' ) 76 { 77if (arg == "-d" ) 78 { 79SLANG_RETURN_ON_FAIL (_parseArgWithValue ("-d" ,outOptions .m_inputDirectory )); 80continue ; 81 } 82else if (arg == "-o" ) 83 { 84SLANG_RETURN_ON_FAIL (_parseArgWithValue ("-o" ,outOptions .m_outputPath )); 85continue ; 86 } 87else if (arg == "-dump" ) 88 { 89SLANG_RETURN_ON_FAIL (_parseArgFlag ("-dump" ,outOptions .m_dump )); 90continue ; 91 } 92else if (arg == "-mark-prefix" ) 93 { 94SLANG_RETURN_ON_FAIL ( 95_parseArgReplaceValue ("-mark-prefix" ,outOptions .m_markPrefix )); 96continue ; 97 } 98else if (arg == "-mark-suffix" ) 99 { 100SLANG_RETURN_ON_FAIL ( 101_parseArgReplaceValue ("-mark-suffix" ,outOptions .m_markSuffix )); 102continue ; 103 } 104else if (arg == "-defs" ) 105 { 106SLANG_RETURN_ON_FAIL (_parseArgFlag ("-defs" ,outOptions .m_defs )); 107continue ; 108 } 109else if (arg == "-output-fields" ) 110 { 111SLANG_RETURN_ON_FAIL (_parseArgFlag ("-output-fields" ,outOptions .m_outputFields )); 112continue ; 113 } 114else if (arg == "-strip-prefix" ) 115 { 116SLANG_RETURN_ON_FAIL ( 117_parseArgWithValue ("-strip-prefix" ,outOptions .m_stripFilePrefix )); 118continue ; 119 } 120else if (arg == "-unit-test" ) 121 { 122SLANG_RETURN_ON_FAIL (_parseArgFlag ("-unit-test" ,outOptions .m_runUnitTests )); 123continue ; 124 } 125else if (arg == "-unmarked" ) 126 { 127bool unmarked ; 128SLANG_RETURN_ON_FAIL (_parseArgFlag ("-unmarked" ,unmarked )); 129outOptions .m_requireMark = !unmarked ; 130continue ; 131 } 132 133m_sink -> diagnose (SourceLoc (),CPPDiagnostics ::unknownOption ,arg ); 134return SLANG_FAIL ; 135 } 136else 137 { 138// If it doesn't start with - then it's assumed to be an input path 139outOptions .m_inputPaths .add (arg ); 140m_index ++ ; 141 } 142 } 143 144if (outOptions .m_inputPaths .getCount ()< 0 ) 145 { 146m_sink -> diagnose (SourceLoc (),CPPDiagnostics ::noInputPathsSpecified ); 147return SLANG_FAIL ; 148 } 149 150return SLANG_OK ; 151} 152 153}// namespace CppParse