-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommandLine.hpp
1610 lines (1426 loc) · 67.8 KB
/
CommandLine.hpp
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020 Tamas Bolner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <map>
#include <unordered_map>
namespace CommandLine {
namespace Helper {
/**
* @brief Argument type
*/
enum class ArgumentType : int {
String = 1,
Int = 2,
Double = 3,
Boolean = 4
};
/**
* @brief Classes of the arguments
*/
enum class ArgumentClass : int {
Argument = 1,
Option = 2,
Operand = 3
};
/**
* @brief Properties of an argument
*/
class CommandLineArg {
private:
std::string name;
std::string valueName;
char letter;
ArgumentClass argClass;
ArgumentType argType;
bool optional;
std::string stringDefault;
int intDefault;
double doubleDefault;
std::string description;
public:
/**
* @brief Create empty ebject
*/
CommandLineArg()
: name(), valueName(), letter(0), argClass(ArgumentClass::Argument), argType(ArgumentType::String),
optional(false), stringDefault(), intDefault(0), doubleDefault(0), description()
{ }
/**
* @brief Create a new Operand
*
* @param name Name of the operand.
* @param description The description of the operand.
*/
CommandLineArg(std::string name, std::string description)
: name(name), valueName(), letter(0), argClass(ArgumentClass::Operand), argType(ArgumentType::String),
optional(false), stringDefault(), intDefault(0), doubleDefault(0), description(description)
{ }
/**
* @brief Create a new Option
*
* @param name Name of the option.
* @param letter The single-character name of the option.
* @param description Description of the option.
*/
CommandLineArg(std::string name, char letter, std::string description)
: name(name), valueName(), letter(letter), argClass(ArgumentClass::Option), argType(ArgumentType::Boolean),
optional(false), stringDefault(), intDefault(0), doubleDefault(0), description(description)
{ }
/**
* @brief Create a mandatory argument without a default.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param argType String / Int / Double / Boolean
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
CommandLineArg(std::string name, char letter, ArgumentType argType, std::string valueName,
std::string description)
: name(name), valueName(valueName), letter(letter), argClass(ArgumentClass::Argument), argType(argType),
optional(false), stringDefault(), intDefault(0), doubleDefault(0), description(description)
{ }
/**
* @brief Create an optional integer argument with a default.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param intDefault Default value for the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
CommandLineArg(std::string name, char letter, int intDefault, std::string valueName, std::string description)
: name(name), valueName(valueName), letter(letter), argClass(ArgumentClass::Argument),
argType(ArgumentType::Int), optional(true), stringDefault(), intDefault(intDefault),
doubleDefault(0), description(description)
{ }
/**
* @brief Create an optional string argument with a default.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param stringDefault Default value for the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
CommandLineArg(std::string name, char letter, std::string stringDefault, std::string valueName,
std::string description)
: name(name), valueName(valueName), letter(letter), argClass(ArgumentClass::Argument),
argType(ArgumentType::String), optional(true), stringDefault(stringDefault),
intDefault(0), doubleDefault(0), description(description)
{ }
/**
* @brief Create an optional double argument with a default.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param doubleDefault Default value for the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
CommandLineArg(std::string name, char letter, double doubleDefault, std::string valueName,
std::string description)
: name(name), valueName(valueName), letter(letter), argClass(ArgumentClass::Argument),
argType(ArgumentType::Double), optional(true), stringDefault(),
intDefault(0), doubleDefault(doubleDefault), description(description)
{ }
/**
* @brief Get the name of the argument.
*
* @return std::string
*/
std::string GetName() const {
return this->name;
}
/**
* @brief Get the value name of the argument. For the auto-generated
* doc only. This is displayed after the options of the argument.
* It's a short name that describes the accepted values.
*
* @return std::string
*/
std::string GetValueName() const {
return this->valueName;
}
/**
* @brief Get the single-character name of the argument.
*
* @return char
*/
char GetLetter() const {
return this->letter;
}
/**
* @brief Get the class of the argument. (argument, option, operand.)
*
* @return ArgumentClass
*/
ArgumentClass GetArgClass() const {
return this->argClass;
}
/**
* @brief Get the type of the argument
*
* @return ArgumentType
*/
ArgumentType GetArgType() const {
return this->argType;
}
/**
* @brief Is this parameter optional.
*
* @return bool
*/
bool IsOptional() const {
return this->optional;
}
/**
* @brief Get the string default value.
*
* @return std::string
*/
std::string GetStringDefault() const {
return this->stringDefault;
}
/**
* @brief Get the int default value.
*
* @return int
*/
int GetIntDefault() const {
return this->intDefault;
}
/**
* @brief Get the double default value.
*
* @return double
*/
int GetDoubleDefault() const {
return this->doubleDefault;
}
/**
* @brief Get the description of the argument.
*
* @return std::string
*/
std::string GetDescription() const {
return this->description;
}
};
/**
* @brief For accumulating the definitions and values of
* all arguments.
*/
class ArgumentAccumulator {
public:
bool empty;
bool restrictOperands;
std::string executableName;
std::map<std::string, CommandLineArg> argsByName;
std::map<char, CommandLineArg> argsByLetter;
std::vector<CommandLineArg> operands;
std::unordered_map<std::string, std::string> stringValues;
std::unordered_map<std::string, int> intValues;
std::unordered_map<std::string, double> doubleValues;
std::set<std::string> optionNames;
std::vector<std::string> operandValues;
std::unordered_map<std::string, std::vector<std::string>> stringValuesList;
std::unordered_map<std::string, std::vector<int>> intValuesList;
std::unordered_map<std::string, std::vector<double>> doubleValuesList;
/**
* @brief Construct a new Argument Accumulator object
*/
ArgumentAccumulator() : empty(true), restrictOperands(false), executableName(), argsByName(), argsByLetter(),
operands(), stringValues(), intValues(), doubleValues(), optionNames(), operandValues(),
stringValuesList(), intValuesList(), doubleValuesList() { }
/**
* @brief Specify a new argument
*
* @param arg
*/
void AddArg(CommandLineArg arg) {
if (argsByName.find(arg.GetName()) != argsByName.end()) {
throw std::runtime_error("Two different arguments have the same name: '" + arg.GetName() + "'.");
}
argsByName.insert({ arg.GetName(), arg });
if (arg.GetArgClass() == ArgumentClass::Operand) {
operands.push_back(arg);
}
if (arg.GetLetter() != 0) {
if (argsByLetter.find(arg.GetLetter()) != argsByLetter.end()) {
throw std::runtime_error("Two different arguments have the same one-letter name: '"
+ std::string(1, arg.GetLetter()) + "'.");
}
argsByLetter.insert({ arg.GetLetter(), arg});
}
/*
Store default
*/
if (arg.IsOptional()) {
if (arg.GetArgType() == ArgumentType::Double) {
this->doubleValues.insert({arg.GetName(), arg.GetDoubleDefault()});
}
else if (arg.GetArgType() == ArgumentType::Int) {
this->intValues.insert({arg.GetName(), arg.GetIntDefault()});
}
else {
this->stringValues.insert({arg.GetName(), arg.GetStringDefault()});
}
}
}
/**
* @brief Converts the value to the proper type
* and stores it.
*
* @param arg
* @param value
*/
void SetValue(CommandLineArg arg, std::string value) {
if (arg.GetArgType() == ArgumentType::Int) {
char *endPtr;
/*
Convert string to int
*/
int result = strtol(value.c_str(), &endPtr, 10);
if (errno == ERANGE) {
throw std::runtime_error("Integer value out of range: " + value);
}
else if (*endPtr != '\0' || errno != 0) {
throw std::runtime_error("Invalid integer value: " + value);
}
/*
Save or update single value
*/
auto item = this->intValues.find(arg.GetName());
if (item == this->intValues.end()) {
this->intValues.insert({ arg.GetName(), result });
} else {
item->second = result;
}
/*
Add to value list
*/
auto listItem = this->intValuesList.find(arg.GetName());
if (listItem == this->intValuesList.end()) {
this->intValuesList.insert({ arg.GetName(), std::vector<int> { result } });
} else {
listItem->second.push_back(result);
}
}
else if (arg.GetArgType() == ArgumentType::Double) {
char *endPtr;
/*
Convert string to double
*/
double result = strtod(value.c_str(), &endPtr);
if (errno == ERANGE) {
throw std::runtime_error("Double value out of range: " + value);
}
else if (*endPtr != '\0' || errno != 0) {
throw std::runtime_error("Invalid Double value: " + value);
}
/*
Save or update single value
*/
auto item = this->doubleValues.find(arg.GetName());
if (item == this->doubleValues.end()) {
this->doubleValues.insert({ arg.GetName(), result });
} else {
item->second = result;
}
/*
Add to value list
*/
auto listItem = this->doubleValuesList.find(arg.GetName());
if (listItem == this->doubleValuesList.end()) {
this->doubleValuesList.insert({ arg.GetName(), std::vector<double> { result } });
} else {
listItem->second.push_back(result);
}
}
else {
/*
Save or update single value
*/
auto item = this->stringValues.find(arg.GetName());
if (item == this->stringValues.end()) {
this->stringValues.insert({arg.GetName(), value});
} else {
item->second = value;
}
/*
Add to value list
*/
auto listItem = this->stringValuesList.find(arg.GetName());
if (listItem == this->stringValuesList.end()) {
this->stringValuesList.insert({ arg.GetName(), std::vector<std::string> { value } });
} else {
listItem->second.push_back(value);
}
}
}
};
/**
* @brief Provides a user-friendly interface
* for specifying arguments of different types.
*/
class ArgumentSpecifier {
private:
ArgumentAccumulator &accu;
public:
/**
* @brief Construct a new ArgumentSpecifier object
*
* @param accu
*/
ArgumentSpecifier(ArgumentAccumulator &accu) : accu(accu) { }
/**
* @brief Specify an optional argument with integer value and
* a default.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param defaultValue Default value for the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
void Int(const char *name, char letter, int defaultValue, const char *valueName, const char *description) {
accu.AddArg(CommandLineArg(name, letter, defaultValue, valueName, description));
}
/**
* @brief Specify a mandatory argument with integer value.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
void Int(const char *name, char letter, const char *valueName, const char *description) {
accu.AddArg(CommandLineArg(name, letter, ArgumentType::Int, valueName, description));
}
/**
* @brief Specify an optional argument with string type and
* default value.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param defaultValue Default value for the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
void String(const char *name, char letter, const char *defaultValue, const char *valueName,
const char *description) {
accu.AddArg(CommandLineArg(name, letter, defaultValue, valueName, description));
}
/**
* @brief Specify a mandatory argument with string type.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
void String(const char *name, char letter, const char *valueName, const char *description) {
accu.AddArg(CommandLineArg(name, letter, ArgumentType::String, valueName, description));
}
/**
* @brief Specify an optional argument with double type and
* default value.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param defaultValue Default value for the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
void Double(const char *name, char letter, double defaultValue, const char *valueName,
const char *description) {
accu.AddArg(CommandLineArg(name, letter, defaultValue, valueName, description));
}
/**
* @brief Specify a mandatory argument with double type.
*
* @param name Name of the argument.
* @param letter The single-character name of the argument.
* @param valueName For the auto-generated doc only. This is displayed after the
* options of the argument. It should be a short name that describes the accepted values.
* @param description Description of the argument.
*/
void Double(const char *name, char letter, const char *valueName, const char *description) {
accu.AddArg(CommandLineArg(name, letter, ArgumentType::Double, valueName, description));
}
};
}
/**
* @brief Provides a user-friendly interface
* to query argument values and related info.
*/
class Arguments {
private:
/**
* @brief Encapsulates the actual data.
*/
Helper::ArgumentAccumulator &accu;
public:
/**
* @brief Construct a new Arguments object
*
* @param accu
*/
Arguments(Helper::ArgumentAccumulator &accu) : accu(accu) { }
/**
* @brief Returns true if an argument with this name
* has been specified by the developer.
* It can be of any type (argument, option or operand).
*
* @param name
* @return bool
*/
bool IsArgumentExist(std::string name) {
return this->accu.argsByName.find(name) != this->accu.argsByName.end();
}
/**
* @brief Returns true if no arguments were passed
* on the command line, false otherwise.
*
* @return bool
*/
bool IsEmpty() {
return this->accu.empty;
}
/**
* @brief Returns true if an option is set,
* false otherwise.
*
* @param name
* @return bool
* @throw runtime_error If no option was specified with
* the given name.
*/
bool IsOptionSet(std::string name) {
auto item = this->accu.argsByName.find(name);
if (item == this->accu.argsByName.end()) {
throw std::runtime_error("Arguments::IsOptionSet() option with name '"
+ name + "' doesn't exist.");
}
if (item->second.GetArgClass() != Helper::ArgumentClass::Option) {
throw std::runtime_error("Arguments::IsOptionSet() argument with name '"
+ name + "' is not an option.");
}
return this->accu.optionNames.find(name) != this->accu.optionNames.end();
}
/**
* @brief Get the value of the given argument. If the argument
* has a default, then it can return that if no values were
* passed on the command line. If the argument is set multiple
* times, then return the value of the last one.
*
* @param argumentName
* @return std::string
* @throw runtime_error If no argument was specified with
* the given name and type.
*/
std::string GetStringValue(std::string argumentName) {
auto item = this->accu.argsByName.find(argumentName);
if (item == this->accu.argsByName.end()) {
throw std::runtime_error("Arguments::GetStringValue(): argument with name '"
+ argumentName + "' doesn't exist.");
}
if (item->second.GetArgType() != Helper::ArgumentType::String) {
throw std::runtime_error("Arguments::GetStringValue(): argument with name '"
+ argumentName + "' is not of type string.");
}
auto valueItem = this->accu.stringValues.find(argumentName);
if (valueItem == this->accu.stringValues.end()) {
return item->second.GetStringDefault();
}
return valueItem->second;
}
/**
* @brief If the argument was set multiple times, then
* return the values in the same order of occurance as
* on the command line. If the argument was not set,
* then returns an empty vector.
*
* @param argumentName
* @return std::vector<std::string>
* @throw runtime_error If no argument was specified with
* the given name and type.
*/
std::vector<std::string> GetStringValueList(std::string argumentName) {
auto item = this->accu.argsByName.find(argumentName);
if (item == this->accu.argsByName.end()) {
throw std::runtime_error("Arguments::GetStringValueList(): argument with name '"
+ argumentName + "' doesn't exist.");
}
if (item->second.GetArgType() != Helper::ArgumentType::String) {
throw std::runtime_error("Arguments::GetStringValueList(): argument with name '"
+ argumentName + "' is not of type string.");
}
auto valueItem = this->accu.stringValuesList.find(argumentName);
if (valueItem == this->accu.stringValuesList.end()) {
return std::vector<std::string>();
}
return valueItem->second;
}
/**
* @brief Get the value of the given argument. If the argument
* has a default, then it can return that if no values were
* passed on the command line. If the argument is set multiple
* times, then return the value of the last one.
*
* @param argumentName
* @return int
* @throw runtime_error If no argument was specified with
* the given name and type.
*/
int GetIntValue(std::string argumentName) {
auto item = this->accu.argsByName.find(argumentName);
if (item == this->accu.argsByName.end()) {
throw std::runtime_error("Arguments::GetIntValue(): argument with name '"
+ argumentName + "' doesn't exist.");
}
if (item->second.GetArgType() != Helper::ArgumentType::Int) {
throw std::runtime_error("Arguments::GetIntValue(): argument with name '"
+ argumentName + "' is not of type integer.");
}
auto valueItem = this->accu.intValues.find(argumentName);
if (valueItem == this->accu.intValues.end()) {
return item->second.GetIntDefault();
}
return valueItem->second;
}
/**
* @brief If the argument was set multiple times, then
* return the values in the same order of occurance as
* on the command line. If the argument was not set,
* then returns an empty vector.
*
* @param argumentName
* @return std::vector<int>
* @throw runtime_error If no argument was specified with
* the given name and type.
*/
std::vector<int> GetIntValueList(std::string argumentName) {
auto item = this->accu.argsByName.find(argumentName);
if (item == this->accu.argsByName.end()) {
throw std::runtime_error("Arguments::GetIntValueList(): argument with name '"
+ argumentName + "' doesn't exist.");
}
if (item->second.GetArgType() != Helper::ArgumentType::Int) {
throw std::runtime_error("Arguments::GetIntValueList(): argument with name '"
+ argumentName + "' is not of type integer.");
}
auto valueItem = this->accu.intValuesList.find(argumentName);
if (valueItem == this->accu.intValuesList.end()) {
return std::vector<int>();
}
return valueItem->second;
}
/**
* @brief Get the value of the given argument. If the argument
* has a default, then it can return that if no values were
* passed on the command line. If the argument is set multiple
* times, then return the value of the last one.
*
* @param argumentName
* @return double
* @throw runtime_error If no argument was specified with
* the given name and type.
*/
double GetDoubleValue(std::string argumentName) {
auto item = this->accu.argsByName.find(argumentName);
if (item == this->accu.argsByName.end()) {
throw std::runtime_error("Arguments::GetDoubleValue(): argument with name '"
+ argumentName + "' doesn't exist.");
}
if (item->second.GetArgType() != Helper::ArgumentType::Double) {
throw std::runtime_error("Arguments::GetDoubleValue(): argument with name '"
+ argumentName + "' is not of type double.");
}
auto valueItem = this->accu.doubleValues.find(argumentName);
if (valueItem == this->accu.doubleValues.end()) {
return item->second.GetDoubleDefault();
}
return valueItem->second;
}
/**
* @brief If the argument was set multiple times, then
* return the values in the same order of occurance as
* on the command line. If the argument was not set,
* then returns an empty vector.
*
* @param argumentName
* @return std::vector<double>
* @throw runtime_error If no argument was specified with
* the given name and type.
*/
std::vector<double> GetDoubleValueList(std::string argumentName) {
auto item = this->accu.argsByName.find(argumentName);
if (item == this->accu.argsByName.end()) {
throw std::runtime_error("Arguments::GetDoubleValueList(): argument with name '"
+ argumentName + "' doesn't exist.");
}
if (item->second.GetArgType() != Helper::ArgumentType::Double) {
throw std::runtime_error("Arguments::GetDoubleValueList(): argument with name '"
+ argumentName + "' is not of type double.");
}
auto valueItem = this->accu.doubleValuesList.find(argumentName);
if (valueItem == this->accu.doubleValuesList.end()) {
return std::vector<double>();
}
return valueItem->second;
}
/**
* @brief Returns the first part of the command line string,
* which is the name (and path) of the executable.
*
* @return std::string
*/
std::string GetExecutableName() {
return this->accu.executableName;
}
/**
* @brief Returns all operand values preserving
* the order they were passed on the command line.
*
* @return std::vector<std::string>
*/
std::vector<std::string> GetOperands() {
return this->accu.operandValues;
}
};
/**
* @brief Parse command line arguments.
*/
class Parser {
private:
int argc;
char **argv;
Helper::ArgumentAccumulator accu;
int screenWidth;
/**
* @brief Creates a detailed error string, including
* the reconstructed command line, and an arrow
* pointing to the problem.
*
* @param message
* @param line
* @param position
*/
void ThrowError(std::string message, std::string line, int position) {
std::stringstream buff;
int windowSize = this->screenWidth;
const float ratio = 0.666;
int maxHead = (int)(ratio * (float)windowSize);
int maxTail = windowSize - maxHead;
int start, head, length;
/*
The 4 cases are explained here:
- protocol_doc/monet-explorer/docs/error_display_cases.png
(length = head + tail)
*/
if (position < maxHead || (int)line.size() < windowSize) {
// Cases 2 and 4
start = 0;
head = position;
length = std::min(windowSize, (int)line.size());
}
else if ((int)line.size() - position < maxTail) {
// Case 3
start = (int)line.size() - windowSize;
head = position - start;
length = windowSize;
}
else {
// Case 1
start = position - maxHead;
head = maxHead;
length = windowSize;
}
buff << "\033[33m" << std::string(windowSize, '-') << "\033[0m" << '\n';
buff << "\033[31m" << message << "\033[0m" << "\n\n";
buff << line.substr(start, length) << '\n';
buff << std::string(head, ' ') << "\033[1m\033[37m" << "^\n";
buff << "\033[33m" << std::string(head, '-') << "\033[1m\033[37m" << '|'
<< "\033[33m" << std::string(windowSize - head - 1, '-') << "\033[0m";
throw std::runtime_error(buff.str());
}
/**
* @brief Add a new operand. Throw error if there are
* too many operands.
*
* @param value
*/
void AddOperand(char *value) {
if (this->accu.operandValues.size() > this->accu.operands.size()
&& this->accu.restrictOperands) {
throw std::runtime_error("The maximal number of operands is restricted to "
+ std::to_string(this->accu.operands.size()) + ".");
}
this->accu.operandValues.push_back(std::string(value));
int size = this->accu.operands.size();
if (size < 1) {
return;
}
int index = this->accu.operandValues.size();
if (size <= index) {
index = size - 1;
}
this->accu.SetValue(this->accu.operands[size - 1], value);
}
/**
* @brief Find the next chunk of text (limited by breaker characters),
* to be outputted on a line. Convert soft hyphens to hyphens when
* necessary. Fill the remainder with spaces
*
* @param text The full text for a column. It will not be modified.
* @param cursor The current byte position in the text. At the beginning it
* must point to the first byte of multy-byte characters (if any).
* This parameter will be modified and set to the next character to be
* processed. It is set to the length of the string if no more characters
* are available.
* @param limit The maximal number of UTF-8 characters in a line.
* @param softHypen Optional soft hyphen character to make the
* text more fluid. Set it to 0 to disable this feature.
* Can only be a single-byte character.
* @param textAttribute For maintaining the text attribute states
* per columns between calls. Examples: bold, underline.
* @param out The resulting characters will be appended to this
* string stream.
* @param breakAll Break text after every character.
* For languages like Japanese and Chinese.
*/
inline void FormatLine(const std::string &text, int &cursor, int limit, char softHypen,
int &textAttribute, std::stringstream &out, bool breakAll) {
int length = text.length();
int charCount = 0;
int mb_remain = 0; // Bytes remaining from a multi-byte character
std::stringstream lastWord;
int lastWordCharCount = 0;
char c; // Current byte
bool foundSoftHyphen = false;
int lastWordPosition = 0; // starting byte position of the last word (inc. breaker)
/*
This is required in case a word containing a non-breaking
space was wrapped to the next line, and the word contains
2 or more text attribute changes. In this case store only
the first attribute change in the textAttribute register.
*/
bool textAttributeWasSetInLastWord = false;
/*
The word that came before the last word ended in a hyphen.
This is important for the case when a soft hypen is
used after a dash, like in "sugar-|free". Normally hypens
don't break, but only if a soft hyphen is added after them.
This variable prevents adding a second hyphen.
*/
bool beforeLastWordEndedInHyphen = false;
/*
Restore the text attribute
*/
out << "\033[" << textAttribute << 'm';
/*
Left-trim
*/
while(cursor < length && (!isprint(text[cursor]) || text[cursor] == ' ')
&& text[cursor] != '\033') {
cursor++;
}
/*
Parse line
*/
for (; cursor < length; cursor++) {
c = text[cursor];
/*
When inside a multi-byte character:
- ignore breakers
- don't increment char count
*/
if (mb_remain > 0) {
// Check for UTF-8 errors
if ((c & 0xC0) != 0x80) {
// Non-expected byte header
// => Treat it as a new character
mb_remain = 0;
goto char_limit_check;
}
mb_remain--;
lastWord << c;
continue;
}
/*
Check for VT100 escape sequences.
Allow only text attributes: ESC[0m, ESC[1m, etc.
Output them, but don't include them
in the char count.
*/
if (c == '\033') {
if (cursor + 3 < length) {
if (text[cursor + 1] == '[' && text[cursor + 3] == 'm'
&& text[cursor + 2] >= 48 && text[cursor + 2] <= 56
&& text[cursor + 2] != 51 && text[cursor + 2] != 54) {