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