-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.cpp
More file actions
479 lines (415 loc) · 12.3 KB
/
Copy pathCommandLine.cpp
File metadata and controls
479 lines (415 loc) · 12.3 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "CommandLine.h"
#include "Conversion.h"
#include "version.h"
#include <set>
CommandLine::CommandLine() : mHelp(false)
{
}
CommandLine::ParamDef::ParamDef()
: type(ParamType::BOOL),
required(RequiredParam::Optional), hasDefault(false),
outBool(nullptr), outInt(nullptr), outString(nullptr), outEnum(nullptr), outChar(nullptr), outColor(nullptr),
defaultBool(false),
defaultInt(0),
defaultEnum(0),
defaultChar(0),
description(L""),
defaultString(L""),
seen(false)
{}
void CommandLine::AddParamBase(ParamDef& _p)
{
// check duplicates inside this parameter
set<wstring> localSet;
for (auto& nm : _p.names) {
wstring key = Conversion::ToLower(nm);
if (!localSet.insert(key).second) {
wprintf(L"Duplicate alias '%ls' in parameter definition\n", nm.c_str());
throw runtime_error("Duplicate alias in parameter");
}
}
// check duplicates across all parameters
for (auto& nm : _p.names) {
wstring key = Conversion::ToLower(nm);
if (mAliasMap.count(key)) {
size_t other = mAliasMap[key];
wprintf(L"Parameter alias conflict: '%ls' is already used by parameter '%ls'\n", nm.c_str(), mParams[other].names[0].c_str());
throw runtime_error("Duplicate parameter alias");
}
mAliasMap[key] = mParams.size();
}
mParams.push_back(_p);
}
void CommandLine::AddHelp(const vector<wstring>& _names, const wstring& _desc, bool& _outVar)
{
mHelp = true;
AddBool(_names, _desc, _outVar);
}
void CommandLine::AddBool(const vector<wstring>& _names, const wstring& _desc, bool& _outVar)
{
AddBool(_names, _desc, _outVar, RequiredParam::Optional, _outVar);
}
void CommandLine::AddBool(const vector<wstring>& _names, const wstring& _desc, bool& _outVar, RequiredParam _required, bool _defaultValue)
{
ParamDef p;
p.names = _names;
p.description = _desc;
p.type = ParamType::BOOL;
p.outBool = &_outVar;
p.required = _required;
p.hasDefault = (_required == RequiredParam::Optional);
p.defaultBool = _defaultValue;
AddParamBase(p);
}
void CommandLine::AddInt(const vector<wstring>& _names, const wstring& _desc, int& _outVar)
{
AddInt(_names, _desc, _outVar, RequiredParam::Optional, _outVar);
}
void CommandLine::AddInt(const vector<wstring>& _names, const wstring& _desc, int& _outVar, RequiredParam _required, int _defaultValue)
{
ParamDef p;
p.names = _names;
p.description = _desc;
p.type = ParamType::INT;
p.outInt = &_outVar;
p.required = _required;
p.hasDefault = (_required == RequiredParam::Optional);
p.defaultInt = _defaultValue;
AddParamBase(p);
}
void CommandLine::AddString(const vector<wstring>& _names, const wstring& _desc, wstring& _outVar)
{
AddString(_names, _desc, _outVar, RequiredParam::Optional, _outVar);
}
void CommandLine::AddString(const vector<wstring>& _names, const wstring& _desc, wstring& _outVar, RequiredParam _required, const wstring& _defaultValue)
{
ParamDef p;
p.names = _names;
p.description = _desc;
p.type = ParamType::STRING;
p.outString = &_outVar;
p.required = _required;
p.hasDefault = (_required == RequiredParam::Optional);
p.defaultString = _defaultValue;
AddParamBase(p);
}
void CommandLine::AddEnum(const vector<wstring>& _names, const wstring& _desc, const map<wstring, int>& _enumMap, int& _outVar)
{
AddEnum(_names, _desc, _enumMap, _outVar, RequiredParam::Optional, _outVar);
}
void CommandLine::AddEnum(const vector<wstring>& _names, const wstring& _desc, const map<wstring, int>& _enumMap, int& _outVar, RequiredParam _required, int _defaultValue)
{
ParamDef p;
p.names = _names;
p.description = _desc;
p.type = ParamType::ENUM;
p.enumMap = _enumMap;
p.outEnum = &_outVar;
p.required = _required;
p.hasDefault = (_required == RequiredParam::Optional);
p.defaultEnum = _defaultValue;
AddParamBase(p);
}
void CommandLine::AddChar(const vector<wstring>& _names, const wstring& _desc, wchar_t& _outVar)
{
AddChar(_names, _desc, _outVar, RequiredParam::Optional, _outVar);
}
void CommandLine::AddChar(const vector<wstring>& _names, const wstring& _desc, wchar_t& _outVar, RequiredParam _required, wchar_t _defaultValue)
{
ParamDef p;
p.names = _names;
p.description = _desc;
p.type = ParamType::CHAR;
p.outChar = &_outVar;
p.required = _required;
p.hasDefault = (_required == RequiredParam::Optional);
p.defaultChar = _defaultValue;
AddParamBase(p);
}
void CommandLine::AddColor(const vector<wstring>& _names, const wstring& _desc, ColorRGB& _outVar)
{
AddColor(_names, _desc, _outVar, RequiredParam::Optional, _outVar);
}
void CommandLine::AddColor(const vector<wstring>& _names, const wstring& _desc, ColorRGB& _outVar, RequiredParam _required, const ColorRGB& _defaultValue)
{
ParamDef p;
p.names = _names;
p.description = _desc;
p.type = ParamType::COLOR;
p.outColor = &_outVar;
p.required = _required;
p.hasDefault = (_required == RequiredParam::Optional);
p.defaultColor = _defaultValue;
AddParamBase(p);
}
bool CommandLine::ParseCommandLine(int _argc, wchar_t** _argv, int& _correctCount)
{
_correctCount = 0;
// apply defaults
for (size_t i = 0; i < mParams.size(); i++) {
ParamDef& p = mParams[i];
if (p.hasDefault) {
switch (p.type) {
case ParamType::BOOL: *p.outBool = p.defaultBool; break;
case ParamType::INT: *p.outInt = p.defaultInt; break;
case ParamType::STRING: *p.outString = p.defaultString; break;
case ParamType::ENUM: *p.outEnum = p.defaultEnum; break;
case ParamType::CHAR: *p.outChar = p.defaultChar; break;
case ParamType::COLOR: *p.outColor = p.defaultColor; break;
default: break;
}
}
}
for (int i = 1; i < _argc; i++) {
wstring arg = _argv[i];
if (arg.size() > 0) {
if (arg.rfind(L"--", 0) == 0) arg = arg.substr(2);
else if (arg[0] == L'-' || arg[0] == L'/') arg = arg.substr(1);
}
wstring value;
size_t eq = arg.find(L'=');
if (eq != wstring::npos) {
value = arg.substr(eq + 1);
arg = arg.substr(0, eq);
}
ParamDef* found = 0;
for (size_t p = 0; p < mParams.size(); p++) {
for (size_t n = 0; n < mParams[p].names.size(); n++) {
if (Conversion::ToLower(mParams[p].names[n]) == Conversion::ToLower(arg)) {
found = &mParams[p];
break;
}
}
if (found) break;
}
if (!found) {
wprintf(L"Unknown parameter: -%s\n", arg.c_str());
return false;
}
switch (found->type) {
case ParamType::BOOL: {
if (value.empty()) {
*found->outBool = true;
}
else {
if (value == L"1" || Conversion::ToLower(value) == L"true" || Conversion::ToLower(value) == L"on")
*found->outBool = true;
else if (value == L"0" || Conversion::ToLower(value) == L"false")
*found->outBool = false;
else {
wprintf(L"Invalid boolean value: %s\n", value.c_str());
return false;
}
}
found->seen = true;
_correctCount++;
break;
}
case ParamType::INT: {
if (!value.empty()) {
*found->outInt = _wtoi(value.c_str());
}
else {
if (i + 1 >= _argc) return false;
*found->outInt = _wtoi(_argv[++i]);
}
found->seen = true;
_correctCount++;
break;
}
case ParamType::STRING: {
if (!value.empty()) {
*found->outString = value;
}
else {
if (i + 1 >= _argc) return false;
*found->outString = _argv[++i];
}
*found->outString = Conversion::ParseEscapeString(*found->outString);
found->seen = true;
_correctCount++;
break;
}
case ParamType::ENUM: {
if (!value.empty()) {
wstring valLower = Conversion::ToLower(value);
bool matched = false;
for (auto& kv : found->enumMap) {
if (Conversion::ToLower(kv.first) == valLower) {
*found->outEnum = kv.second;
matched = true;
break;
}
}
if (!matched) {
wprintf(L"Invalid enum value: %s\n", value.c_str());
return false;
}
}
else {
if (i + 1 >= _argc) return false;
wstring val = _argv[++i];
wstring valLower = Conversion::ToLower(val);
bool matched = false;
for (auto& kv : found->enumMap) {
if (Conversion::ToLower(kv.first) == valLower) {
*found->outEnum = kv.second;
matched = true;
break;
}
}
if (!matched) {
wprintf(L"Invalid enum value: %s\n", val.c_str());
return false;
}
}
found->seen = true;
_correctCount++;
break;
}
case ParamType::CHAR: {
if (!value.empty()) {
value = Conversion::TrimWhiteChar(value);
if (value.empty()) {
wprintf(L"Invalid char value for -%s\n", found->names[0].c_str());
return false;
}
*found->outChar = value[0];
}
else {
if (i + 1 >= _argc) return false;
wstring val = Conversion::TrimWhiteChar(_argv[++i]);
if (val.empty()) {
wprintf(L"Invalid char value for -%s\n", found->names[0].c_str());
return false;
}
*found->outChar = val[0];
}
found->seen = true;
_correctCount++;
break;
}
case ParamType::COLOR: {
wstring valStr;
if (!value.empty()) {
valStr = value;
}
else {
if (i + 1 >= _argc) {
wprintf(L"Missing value for color parameter -%s\n", found->names[0].c_str());
return false;
}
valStr = _argv[++i];
}
valStr = Conversion::TrimString(valStr, L"#");
if (valStr.size() == 3) { // expand #abc → aabbcc
wstring expanded;
expanded.reserve(6);
for (wchar_t c : valStr)
expanded.append(2, c);
valStr = expanded;
}
if (valStr.size() < 6) {
wprintf(L"Invalid color format for -%s (expected #RRGGBB, RRGGBB, #RGB or RGB)\n", found->names[0].c_str());
return false;
}
try {
wstring hexPart = valStr.substr(0, 6);
for (wchar_t c : hexPart) {
if (!iswxdigit(c)) {
wprintf(L"Invalid character in color value for -%s: %lc\n", found->names[0].c_str(), c);
return false;
}
}
unsigned long r = wcstol(hexPart.substr(0, 2).c_str(), nullptr, 16);
unsigned long g = wcstol(hexPart.substr(2, 2).c_str(), nullptr, 16);
unsigned long b = wcstol(hexPart.substr(4, 2).c_str(), nullptr, 16);
if (r > 255 || g > 255 || b > 255) {
wprintf(L"Color values must be between 0 and 255\n");
return false;
}
*found->outColor = ColorRGB((unsigned char)r, (unsigned char)g, (unsigned char)b);
}
catch (...) {
wprintf(L"Error parsing color value for -%s\n", found->names[0].c_str());
return false;
}
found->seen = true;
_correctCount++;
break;
}
}
}
// check required
for (size_t i = 0; i < mParams.size(); i++) {
if (mParams[i].required == RequiredParam::Required) {
if (!mParams[i].seen) {
if (mHelp == false)
wprintf(L"Missing required parameter: -%s\n", mParams[i].names[0].c_str());
return false;
}
}
}
return true;
}
void CommandLine::Help()
{
wprintf(L"%hs %hs\n", VER_PRODUCTNAME_STR, VER_FILE_VERSION_STR);
wprintf(L"\t%hs\n\n", VER_FILE_DESCRIPTION_STR);
wprintf(L"Examples:\n");
wprintf(L"\t%hs -t Title -m Message\n", VER_ORIGINAL_FILENAME_STR);
wprintf(L"\n");
wprintf(L"Options:\n");
for (size_t ip = 0; ip < mParams.size(); ip++) {
const ParamDef& p = mParams[ip];
// name
wprintf(L" ");
for (size_t n = 0; n < p.names.size(); n++) {
wprintf(L"-%ls", p.names[n].c_str());
if (n + 1 < p.names.size()) wprintf(L", ");
}
// description
wprintf(L"\n %ls\n", p.description.c_str());
// enum
if (p.type == ParamType::ENUM && p.enumMap.size() != 0) {
map<int, vector<wstring> > grouped;
for (map<wstring, int>::const_iterator it = p.enumMap.begin(); it != p.enumMap.end(); ++it) {
grouped[it->second].push_back(it->first);
}
wprintf(L" Values:\n");
for (map<int, vector<wstring> >::iterator g = grouped.begin(); g != grouped.end(); ++g) {
const vector<wstring>& names = g->second;
if (names.empty())
continue;
wprintf(L" %ls", names[0].c_str());
// alias
if (names.size() > 1) {
wprintf(L" (");
for (size_t i = 1; i < names.size(); i++) {
wprintf(L"%ls", names[i].c_str());
if (i + 1 < names.size())
wprintf(L", ");
}
wprintf(L")");
}
wprintf(L"\n");
}
}
if (p.type == ParamType::COLOR) {
wprintf(L" Format: #RRGGBB or RRGGBB (Hex)\n");
if (p.hasDefault) {
wprintf(L" Default: #%02X%02X%02X\n", p.defaultColor.r, p.defaultColor.g, p.defaultColor.b);
}
}
wprintf(L"\n");
}
}
bool CommandLine::WasProvided(const wstring& _name) const
{
wstring key = Conversion::TrimWhiteChar(Conversion::ToLower(_name));
auto it = mAliasMap.find(key);
if (it == mAliasMap.end())
return false;
const ParamDef& p = mParams[it->second];
return p.seen;
}