-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathParseCmdLineArg.cpp
More file actions
582 lines (486 loc) Β· 20.3 KB
/
ParseCmdLineArg.cpp
File metadata and controls
582 lines (486 loc) Β· 20.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// MLB Utility Library Module File
// ////////////////////////////////////////////////////////////////////////////
/*
File Name : %M%
File Version : %I%
Last Extracted : %D% %T%
Last Updated : %E% %U%
File Description : Implementation of the argument parsing and validation
class ParseCmdLineArg.
Revision History : 1993-10-02 --- Creation
Michael L. Brock
Copyright Michael L. Brock 1993 - 2018.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// Required include files...
// ////////////////////////////////////////////////////////////////////////////
#include <Utility/C_StringSupport.hpp>
#include <Utility/ParseCmdLineArg.hpp>
#include <Utility/InlineContainer.hpp>
#include <Utility/Sleep.hpp>
#include <Utility/PathName.hpp>
#include <Utility/RegexParamNameAdaptor.hpp>
// ////////////////////////////////////////////////////////////////////////////
namespace MLB {
namespace Utility {
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::HasCmdLineHelp(int argc, char **argv, int first_index)
{
return(HasCmdLineArg("-HELP", argc, argv, first_index) ||
HasCmdLineArg("-H", argc, argv, first_index) ||
HasCmdLineArg("-?", argc, argv, first_index));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::HasCmdLineArg(const char *arg_name, int argc, char **argv,
int first_index)
{
return(FindCmdLineArg(arg_name, argc, argv, first_index) > -1);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
int ParseCmdLineArg::FindCmdLineArg(const char *arg_name, int argc, char **argv,
int first_index)
{
CheckCmdLineArgList(argc, argv);
if ((first_index >= 0) && (first_index < argc)) {
while (first_index < argc) {
if (!Utility_stricmp(argv[first_index], arg_name))
return(first_index);
++first_index;
}
}
return(-1);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::IsCmdLineArg(const char *param_name,
unsigned int current_index, int argc, char **argv)
{
CheckCmdLineSource(current_index, argc, argv);
return(Utility_stricmp(argv[current_index], param_name) == 0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseDirectory(const std::string ¶m_name,
unsigned int ¤t_index, int argc, char **argv, std::string &out_datum,
const std::string &base_path, bool require_existence)
{
if (!ParseCmdLineFollowing(param_name, current_index, argc, argv,
out_datum))
return(false);
ResolveDirectoryPath(out_datum, base_path, require_existence);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseWorkingDir(unsigned int ¤t_index, int argc,
char **argv, std::string &out_datum, const std::string &base_path,
bool require_existence)
{
if (!ParseCmdLineFollowingSpec(MLB::Utility::MakeInlineVector<std::string>
("-WORKING_DIRECTORY")
("-WORKINGDIRECTORY")
("-WORKING_DIR")
("-WORKINGDIR")
("-WORK_DIRECTORY")
("-WORKDIRECTORY")
("-WORK_DIR")
("-WORKDIR"),
current_index, argc, argv, out_datum))
return(false);
ResolveDirectoryPath(out_datum, base_path, require_existence);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseLoggingDir(unsigned int ¤t_index, int argc,
char **argv, std::string &out_datum, const std::string &base_path,
bool require_existence)
{
if (!ParseCmdLineFollowingSpec(MLB::Utility::MakeInlineVector<std::string>
("-LOGGING_DIRECTORY")
("-LOGGINGDIRECTORY")
("-LOGGING_DIR")
("-LOGGINGDIR")
("-LOG_DIRECTORY")
("-LOGDIRECTORY")
("-LOG_DIR")
("-LOGDIR"),
current_index, argc, argv, out_datum))
return(false);
ResolveDirectoryPath(out_datum, base_path, require_existence);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseLoggingFlag(unsigned int ¤t_index, int argc,
char **argv, bool &out_datum)
{
std::string tmp_string;
if (!ParseCmdLineFollowingSpec(MLB::Utility::MakeInlineVector<std::string>
("-LOGGING_FLAG")
("-LOGGINGFLAG")
("-LOGGING")
("-LOG_FLAG")
("-LOGFLAG")
("-LOG"),
current_index, argc, argv, tmp_string))
return(false);
ParseCmdLineDatum(tmp_string, out_datum);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
bool &out_datum)
{
if ((!Utility_stricmp(param_value.c_str(), "true")) || (param_value == "1") ||
(!Utility_stricmp(param_value.c_str(), "on")) ||
(!Utility_stricmp(param_value.c_str(), "yes")))
out_datum = true;
else if ((!Utility_stricmp(param_value.c_str(), "false")) ||
(param_value == "0") || (!Utility_stricmp(param_value.c_str(), "off")) ||
(!Utility_stricmp(param_value.c_str(), "no")))
out_datum = false;
else
ThrowInvalidArgument(std::string("Unable to parse expected boolean "
"value ('") + param_value + "').");
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
signed char &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
unsigned char &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
signed short &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
unsigned short &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
signed int &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
unsigned int &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
signed long &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
unsigned long &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
signed long long &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
unsigned long long &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
float &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
double &out_datum)
{
return(ParseCmdLineNumeric(param_value, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
std::string &out_datum)
{
out_datum = param_value;
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
UniqueId &out_datum)
{
out_datum = UniqueId::FromString(param_value);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
TimeT &out_datum)
{
out_datum = TimeT::FromString(param_value);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
TimeVal &out_datum)
{
out_datum = TimeVal::FromString(param_value);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineDatum(const std::string ¶m_value,
TimeSpec &out_datum)
{
out_datum = TimeSpec::FromString(param_value);
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineFollowing(const std::string ¶m_name,
unsigned int ¤t_index, int argc, char **argv, std::string &out_datum)
{
return(ParseCmdLineFollowing(param_name.c_str(), current_index, argc,
argv, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineFollowing(const char *param_name,
unsigned int ¤t_index, int argc, char **argv, std::string &out_datum)
{
CheckCmdLineSource(current_index, argc, argv);
if (Utility_stricmp(argv[current_index], param_name))
return(false);
RequireFollowingParameter(current_index, argc, argv);
out_datum = argv[++current_index];
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineFollowingNotEmpty(
const std::string ¶m_name, unsigned int ¤t_index, int argc,
char **argv, std::string &out_datum)
{
return(ParseCmdLineFollowingNotEmpty(param_name.c_str(), current_index,
argc, argv, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseCmdLineFollowingNotEmpty(const char *param_name,
unsigned int ¤t_index, int argc, char **argv, std::string &out_datum)
{
CheckCmdLineSource(current_index, argc, argv);
if (Utility_stricmp(argv[current_index], param_name))
return(false);
RequireFollowingParameterNotEmpty(current_index, argc, argv);
out_datum = argv[++current_index];
return(true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::CheckCmdLineSource(unsigned int current_index,
int argc, char **argv)
{
CheckCmdLineArgList(argc, argv);
if (current_index >= static_cast<unsigned int>(argc)) {
std::ostringstream error_text;
error_text << "Invalid argument list index (" << current_index <<
"), argument count is " << argc << ".";
ThrowInvalidArgument(error_text.str().c_str());
}
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::CheckCmdLineArgList(int argc, char **argv)
{
if (argc < 0) {
std::ostringstream error_text;
error_text << "Invalid argument count (" << argc << ").";
ThrowInvalidArgument(error_text);
}
if (argv == NULL)
ThrowInvalidArgument("Invalid argument list (NULL).");
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::RequireFollowingParameter(unsigned int current_index,
int argc, char **argv)
{
CheckCmdLineSource(current_index, argc, argv);
if ((current_index + 1) == static_cast<unsigned int>(argc)) {
std::ostringstream error_text;
error_text << "Expected a command-line qualifier to follow the '" <<
argv[current_index] << "' parameter.";
ThrowInvalidArgument(error_text.str().c_str());
}
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::RequireFollowingParameterNotEmpty(
unsigned int current_index, int argc, char **argv)
{
RequireFollowingParameter(current_index, argc, argv);
if (!(*argv[current_index])) {
std::ostringstream error_text;
error_text << "The command-line qualifier following the '" <<
argv[current_index] << "' parameter is an empty string.";
ThrowInvalidArgument(error_text.str().c_str());
}
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::InvalidArgument(const char *parameter)
{
std::ostringstream error_text;
error_text << "Invalid parameter '" << parameter <<
"' was encountered on the command line.";
ThrowInvalidArgument(error_text);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::CheckDirectoryPath(const std::string ¶m_name,
std::string &in_path, const std::string &base_path, bool require_existence)
{
CheckFilePathGeneral(param_name, in_path, base_path, require_existence,
true, false);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::CheckFilePath(const std::string ¶m_name,
std::string &in_path, const std::string &base_path, bool require_existence)
{
CheckFilePathGeneral(param_name, in_path, base_path, require_existence,
false, true);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void ParseCmdLineArg::CheckFilePathGeneral(const std::string ¶m_name,
std::string &in_path, const std::string &base_path, bool require_existence,
bool is_directory, bool is_file)
{
try {
ResolveFilePathGeneral(in_path, in_path, base_path, require_existence,
is_directory, is_file);
}
catch (const std::exception &except) {
MLB::Utility::ThrowException("The specified " + param_name +
" parameter could not be resolved: " + except.what());
}
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string ParseCmdLineArg::GetMainWaitMSecsHelpShort()
{
return("[ -main_function_sleep_milliseconds <milliseconds-to-sleep> ]");
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string ParseCmdLineArg::GetMainWaitMSecsHelpLong()
{
return("\
-main_function_sleep_milliseconds <milliseconds-to-sleep>\n\
Specifies the number of milliseconds the program is to sleep in main()\n\
before beginning operation.\n\n\
The intent of this parameter is to impose a delay to assist in program\n\
debugging and problem resolution.");
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseMainWaitMSecs(unsigned int ¤t_index, int argc,
char **argv, unsigned int &out_datum)
{
return(ParseCmdLineDatum(RegexParamNameAdaptor(
// "\\-MAIN(_*FUNC(TION)*)*_*SLEEP(_*((M(ILLI)*SEC(OND)*)S*))*(_*TIMES*)*$",
"^--?MAIN((-|_)?FUNC(TION)?)?(-|_)?SLEEP((-|_)?((M(ILLI)?SEC(OND)?)S?))?"
"((-|_)?TIMES?)?$",
current_index, argc, argv), current_index, argc, argv, out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool ParseCmdLineArg::ParseMainWaitMSecs(unsigned int ¤t_index, int argc,
char **argv)
{
unsigned int out_datum;
return(ParseMainWaitMSecs(current_index, argc, argv,
out_datum));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
unsigned int ParseCmdLineArg::GetMainWaitMSecs(int argc, char **argv)
{
unsigned int sleep_msecs;
unsigned int count_1;
for (count_1 = 1; count_1 < static_cast<unsigned int>(argc); ++count_1) {
try {
if (ParseMainWaitMSecs(count_1, argc, argv, sleep_msecs))
return(sleep_msecs);
}
catch (const std::exception &except) {
MLB::Utility::Rethrow(except, "Unable to parse the main() sleep "
"milliseconds value from the command line: " +
std::string(except.what()));
}
}
return(0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
unsigned int ParseCmdLineArg::PerformMainWaitMSecs(int argc, char **argv,
std::ostream &o_str)
{
unsigned int sleep_msecs = GetMainWaitMSecs(argc, argv);
if (sleep_msecs) {
o_str << "Sleeping within main() for " << sleep_msecs <<
" milliseconds..." << std::endl;
SleepMilliSecs(sleep_msecs);
o_str << "Sleep within main() is complete." << std::endl;
}
return(sleep_msecs);
}
// ////////////////////////////////////////////////////////////////////////////
} // namespace Utility
} // namespace MLB