-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProgramArgs.hpp
1578 lines (1378 loc) · 46.7 KB
/
ProgramArgs.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 (c) 2016, Hobu Inc., [email protected]
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
****************************************************************************/
#pragma once
#include <map>
#include <memory>
#include <vector>
#include "Utils.hpp"
namespace pdal
{
class arg_error
{
public:
arg_error(const std::string& error) : m_error(error)
{}
std::string what() const
{ return m_error; }
std::string m_error;
};
// Specifically, an error in the argument's value.
class arg_val_error : public arg_error
{
public:
arg_val_error(const std::string& error) : arg_error(error)
{}
};
namespace
{
class ArgValList
{
struct ArgVal
{
std::string m_val;
bool m_consumed;
ArgVal(const std::string& s) :
m_val(s), m_consumed(false)
{}
};
public:
ArgValList(const std::vector<std::string>& slist) : m_unconsumedStart(0)
{
for (const std::string& s : slist)
add(s);
}
void add(const std::string& s)
{
if (s.empty())
return;
// Turn a short arg list into a set of short args: -afv -> -a -f -v
// so that each argval represents a single arg.
if (s.size() > 1 && s[0] == '-' && s[1] != '-')
for (size_t i = 1; i < s.size(); i++)
m_vals.push_back({std::string("-") + s[i]});
else
m_vals.push_back({s});
}
void consume(size_t i)
{
m_vals[i].m_consumed = true;
if (i == m_unconsumedStart)
while (i < m_vals.size() - 1 && consumed(++i))
m_unconsumedStart++;
}
std::vector<std::string> unconsumedArgs() const
{
std::vector<std::string> remainingVals;
for (size_t i = firstUnconsumed(); i < size(); ++i)
if (!consumed(i))
remainingVals.push_back(m_vals[i].m_val);
return remainingVals;
}
size_t size() const
{ return m_vals.size(); }
const std::string& operator[](size_t i) const
{ return m_vals[i].m_val; }
bool consumed(size_t i) const
{ return m_vals[i].m_consumed; }
size_t firstUnconsumed() const
{ return m_unconsumedStart; }
private:
std::vector<ArgVal> m_vals;
size_t m_unconsumedStart;
};
} // unnamed namespace
/**
Description of an argument that can be parsed by \class ProgramArgs.
Stores information about each argument including the required "longname",
an optional single-character shortname, a description, and an indicator
of the positional-type of the argument.
*/
class Arg
{
public:
/**
Positional type. Either None, Optional or Required.
*/
enum class PosType
{
None, ///< Not positional
Required, ///< Required positional
Optional ///< Optional positional
};
protected:
/**
Constructor.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
*/
Arg(const std::string& longname, const std::string& shortname,
const std::string& description) : m_longname(longname),
m_shortname(shortname), m_description(description), m_set(false),
m_hidden(false), m_positional(PosType::None)
{}
public:
virtual ~Arg()
{}
/**
Indicate that the argument shouldn't be shown in help text.
\param hidden Whether the argument should be hidden or not
[default: true].
\return A reference to this \class Arg, to allow the function
call to be chained.
*/
Arg& setHidden(bool hidden = true)
{
m_hidden = true;
return *this;
}
/**
Indicate that the argument is positional.
Positional arguments may be specified on the command line without
any argument name. Such arguments are required to be specified
either with the argument name as a normal option or positionally.
Missing positional arguments will raise an exception when the
command line is parsed
*/
virtual Arg& setPositional()
{
m_positional = PosType::Required;
return *this;
}
/**
Indicate that the argument is positional and optional.
Positional arguments may be specified on the command line without
any argument name. Optional positional arguments must be added to
\class ProgramArgs after any non-optional arguments. If optional
positional arguments are not found, no exception is raised when
the command line is parsed.
*/
virtual Arg& setOptionalPositional()
{
m_positional = PosType::Optional;
return *this;
}
/**
Provide error text for the argument to override the default.
\param error Error text.
*/
virtual Arg& setErrorText(const std::string& error)
{
m_error = error;
return *this;
}
/**
Return whether the argument was set during command-line parsing.
*/
bool set() const
{ return m_set; }
/**
Return whether a default value was provided for the argument.
\return Whether a default was provided.
*/
virtual bool defaultProvided() const
{ return false; }
/**
Return a string representation of an Arg's default value, or an
empty string if none exists.
\return Default value as a string.
*/
virtual std::string defaultVal() const
{ return std::string(); }
public:
/**
Return whether an option needs a value to be valid. Generally true
for all options not bound to boolean values.
\note Not intended to be called from user code.
*/
virtual bool needsValue() const
{ return true; }
/**
Set a an argument's value from a string.
Throws an arg_error exception if \a s can't be converted to
the argument's type.
\note Not intended to be called from user code.
\param s Value to set.
*/
virtual void setValue(const std::string& s) = 0;
/**
Reset the argument's state.
Set the internal state of the argument and it's referenced variable
as if no command-line parsing had occurred.
\note For testing. Not intended to be called from user code.
*/
virtual void reset() = 0;
/**
Set the argument's value from the list of command-line args.
\note Not intended to be called from user code.
\param vals The list of command-line argument values.
*/
virtual void assignPositional(ArgValList& vals)
{}
/**
Returns the positional type of the argument.
\note Not intended to be called from user code.
*/
PosType positional() const
{ return m_positional; }
/**
Returns whether the argument is hidden or not.
\note Not intended to be called from user code.
*/
bool hidden() const
{ return m_hidden; }
/**
Returns the description of the argument.
\note Not intended to be called from user code.
\return Argument description.
*/
std::string description() const
{ return m_description; }
/**
Returns the longname of the argument.
\note Not intended to be called from user code.
\return Argument long name.
*/
std::string longname() const
{ return m_longname; }
/**
Returns text indicating the longname and shortname of the option
suitable for displaying in help information.
\note Not intended to be called from user code.
*/
std::string nameDescrip() const
{
std::string s("--");
s += m_longname;
if (m_shortname.size())
s += ", -" + m_shortname;
return s;
}
/**
Returns text indicating the name of the option suitable for displaying
in "usage" text.
\note Not intended to be called from user code.
*/
std::string commandLine() const
{
std::string s;
if (m_positional == PosType::Required)
s = m_longname;
else if (m_positional == PosType::Optional)
s += '[' + m_longname + ']';
return s;
}
protected:
std::string m_longname;
std::string m_shortname;
std::string m_description;
std::string m_rawVal;
bool m_set;
bool m_hidden;
PosType m_positional;
std::string m_error;
};
/**
Description of an argument. Boolean arguments and vector (list-based)
arguments are handled separately.
*/
template <typename T>
class TArg : public Arg
{
public:
/**
Constructor that takes a default argument.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable Variable to which the value of the argument should
be bound.
\param def Default value to be assigned to the bound variable.
*/
TArg(const std::string& longname, const std::string& shortname,
const std::string& description, T& variable, T def) :
Arg(longname, shortname, description), m_var(variable),
m_defaultVal(def), m_defaultProvided(true)
{ m_var = m_defaultVal; }
/**
Constructor.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable Variable to which the value of the argument should
be bound.
*/
TArg(const std::string& longname, const std::string& shortname,
const std::string& description, T& variable) :
Arg(longname, shortname, description), m_var(variable),
m_defaultVal(T()), m_defaultProvided(false)
{ m_var = m_defaultVal; }
/**
Set a an argument's value from a string.
Throws an arg_error exception if \a s can't be converted to
the argument's type. Values must be provided for with the
option name.
\note Not intended to be called from user code.
\param s Value to set.
*/
virtual void setValue(const std::string& s)
{
if (m_set)
{
throw arg_val_error("Attempted to set value twice for argument '" +
m_longname + "'.");
}
if (s.empty())
{
throw arg_val_error("Argument '" + m_longname +
"' needs a value and none was provided.");
}
m_rawVal = s;
auto status = Utils::fromString(s, m_var);
if (!status)
{
std::string error(m_error);
if (error.empty())
{
if (status.what().size())
error = "Invalid value for argument '" + m_longname +
"': " + status.what();
else
error = "Invalid value '" + s + "' for argument '" +
m_longname + "'.";
}
throw arg_val_error(error);
}
m_set = true;
}
/**
Reset the argument's state.
Set the interval state of the argument and it's referenced variable
as if no command-line parsing had occurred.
\note For testing. Not intended to be called from user code.
*/
virtual void reset()
{
m_var = m_defaultVal;
m_set = false;
m_hidden = false;
}
/**
Set the argument's value from the command-line args.
If no value is provided for a required positional option, an arg_error
exception is thrown.
\note Not intended to be called from user code.
\param vals The list of command-line args.
*/
virtual void assignPositional(ArgValList& vals)
{
if (m_positional == PosType::None || m_set)
return;
for (size_t i = vals.firstUnconsumed(); i < vals.size(); ++i)
{
const std::string& val = vals[i];
if ((val.size() && val[0] == '-') || vals.consumed(i))
continue;
setValue(val);
vals.consume(i);
return;
}
if (m_positional == PosType::Required)
throw arg_error("Missing value for positional argument '" +
m_longname + "'.");
}
/**
Return whether a default value was provided for the argument.
\return Whether a default was provided.
*/
virtual bool defaultProvided() const
{ return m_defaultProvided; }
/**
Return a string representation of an Arg's default value.
\return Default value as a string.
*/
virtual std::string defaultVal() const
{ return Utils::toString(m_defaultVal); }
private:
T& m_var;
T m_defaultVal;
bool m_defaultProvided;
};
/**
Description of a boolean argument. Boolean arguments only take the values "true" or "false".
Setting a boolean argument without a value inverts its default value. Boolean arguments
are normally 'false' by default.
*/
template <>
class TArg<bool> : public Arg
{
public:
/**
Constructor for boolean arguments with default value.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable bool variable to which the value of the argument should
be bound.
\param def Default value to be assigned to the bound variable.
*/
TArg(const std::string& longname, const std::string& shortname,
const std::string& description, bool& variable, bool def) :
Arg(longname, shortname, description), m_val(variable),
m_defaultVal(def), m_defaultProvided(true)
{ m_val = m_defaultVal; }
/**
Constructor for boolean arguments without default value.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable bool variable to which the value of the argument should
be bound.
*/
TArg(const std::string& longname, const std::string& shortname,
const std::string& description, bool& variable) :
Arg(longname, shortname, description), m_val(variable),
m_defaultVal(false), m_defaultProvided(false)
{ m_val = m_defaultVal; }
/**
Return whether an option needs a value to be valid.
\return false Boolean values don't need a value.
\note Not intended to be called from user code.
*/
virtual bool needsValue() const
{ return false; }
/**
Set a an argument's value from a string.
\note The argumet is either 'true' or 'false'. True means that we're
setting the option, which sets the negative of the default value.
False sets the option to the default value (essentially a no-op).
\note Not intended to be called from user code.
\param s Value to set [ignored].
*/
virtual void setValue(const std::string& s)
{
if (s.size() && s[0] == '-')
{
throw arg_val_error("Argument '" + m_longname +
"' needs a value and none was provided.");
}
if (s == "invert")
m_val = !m_defaultVal;
else if (s == "true")
m_val = true;
else
m_val = false;
m_set = true;
}
/**
Reset the argument's state.
Set the internal state of the argument and it's referenced variable
as if no command-line parsing had occurred.
\note For testing. Not intended to be called from user code.
*/
virtual void reset()
{
m_val = m_defaultVal;
m_set = false;
m_hidden = false;
}
/**
Indicate that the argument is positional.
Throws an exception to indicate that boolean arguments can't
positional.
*/
virtual Arg& setPositional()
{
throw arg_error("Boolean argument '" + m_longname +
"' can't be positional.");
return *this;
}
/**
Indicate that the argument is positional and optional.
Throws an exception to indicate that boolean arguments can't
positional.
*/
virtual Arg& setOptionalPositional()
{
throw arg_error("Boolean argument '" + m_longname +
"' can't be positional.");
return *this;
}
/**
Return whether a default value was provided for the argument.
\return Whether a default was provided.
*/
virtual bool defaultProvided() const
{ return m_defaultProvided; }
/**
Return a string representation of an Arg's default value.
\return Default value as a string.
*/
virtual std::string defaultVal() const
{ return Utils::toString(m_defaultVal); }
private:
bool& m_val;
bool m_defaultVal;
bool m_defaultProvided;
};
/**
Description of a list-based (vector) argument. List-based arguments can
be specified multiple times, taking multiple values. List-based
arguments are necessarily bound to variables that are vectors.
\note Doesn't properly support list-based boolean values.
*/
class BaseVArg : public Arg
{
public:
/**
Constructor.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
*/
BaseVArg(const std::string& longname, const std::string& shortname,
const std::string& description) : Arg(longname, shortname, description),
m_defaultProvided(false)
{}
/**
Set the argument's value from the command-line args.
List-based arguments consume ALL positional arguments until
one is found that can't be converted to the type of the bound variable.
\note Not intended to be called from user code.
\param vals The list of command-line args.
*/
virtual void assignPositional(ArgValList& vals)
{
if (m_positional == PosType::None || m_set)
return;
int cnt = 0;
for (size_t i = vals.firstUnconsumed(); i < vals.size(); ++i)
{
const std::string& val = vals[i];
if ((val.size() && val[0] == '-') || vals.consumed(i))
continue;
try
{
setValue(val);
vals.consume(i);
cnt++;
}
catch (arg_error&)
{
break;
}
}
if (cnt == 0 && m_positional == PosType::Required)
{
throw arg_error("Missing value for positional argument '" +
m_longname + "'.");
}
}
/**
Return whether a default value was provided for the argument.
\return Whether a default was provided.
*/
virtual bool defaultProvided() const
{ return m_defaultProvided; }
protected:
bool m_defaultProvided;
};
/**
Description of a generic list-based (vector) argument.
\note Doesn't properly support list-based boolean values.
*/
template <typename T>
class VArg : public BaseVArg
{
public:
/**
Constructor for arguments with default value.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable Variable to which the argument value(s) should be bound.
\param def Default value.
*/
VArg(const std::string& longname, const std::string& shortname,
const std::string& description, std::vector<T>& variable,
std::vector<T> def) :
BaseVArg(longname, shortname, description), m_var(variable),
m_defaultVal(def)
{
m_var = def;
m_defaultProvided = true;
}
/**
Constructor for arguments without default value.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable Variable to which the argument value(s) should be bound.
*/
VArg(const std::string& longname, const std::string& shortname,
const std::string& description, std::vector<T>& variable) :
BaseVArg(longname, shortname, description), m_var(variable)
{
// Clearing the vector resets to "default" value.
m_var.clear();
}
/**
Set a an argument's value from a string.
Throws an arg_error exception if \a s can't be converted to
the argument's type.
\note Not intended to be called from user code.
\param s Value to set.
*/
virtual void setValue(const std::string& s)
{
T var;
m_rawVal = s;
auto status = Utils::fromString(s, var);
if (!status)
{
std::string error(m_error);
if (error.empty())
{
if (status.what().size())
error = "Invalid value for argument '" + m_longname +
"': " + status.what();
else
error = "Invalid value '" + s + "' for argument '" +
m_longname + "'.";
}
throw arg_val_error(error);
}
if (!m_set)
m_var.clear();
m_var.push_back(std::move(var));
m_set = true;
}
/**
Reset the argument's state.
Set the internal state of the argument and it's referenced variable
as if no command-line parsing had occurred.
\note For testing. Not intended to be called from user code.
*/
virtual void reset()
{
m_var = m_defaultVal;
m_set = false;
m_hidden = false;
}
/**
Return a string representation of an Arg's default value, or an
empty string if none exists.
\return Default value as a string.
*/
virtual std::string defaultVal() const
{
std::string s;
for (size_t i = 0; i < m_defaultVal.size(); ++i)
{
if (i > 0)
s += ", ";
s += Utils::toString(m_defaultVal[i]);
}
return s;
}
private:
std::vector<T>& m_var;
std::vector<T> m_defaultVal;
};
/**
Description of an argument tied to a string vector.
*/
template <>
class VArg<std::string> : public BaseVArg
{
public:
/**
Constructor for arguments wit default value.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable Variable to which the argument value(s) should be bound.
\param def Default value.
*/
VArg(const std::string& longname, const std::string& shortname,
const std::string& description, std::vector<std::string>& variable,
std::vector<std::string> def) :
BaseVArg(longname, shortname, description), m_var(variable),
m_defaultVal(def)
{
m_var = def;
m_defaultProvided = true;
}
/**
Constructor for arguments without default value.
\param longname Name of argument specified on command line with "--"
prefix.
\param shortname Optional name of argument specified on command
line with "-" prefix.
\param description Argument description.
\param variable Variable to which the argument value(s) should be bound.
*/
VArg(const std::string& longname, const std::string& shortname,
const std::string& description, std::vector<std::string>& variable) :
BaseVArg(longname, shortname, description), m_var(variable)
{}
/**
Set a an argument's value from a string.
Throws an arg_error exception if \a s can't be converted to
the argument's type.
\note Not intended to be called from user code.
\param s Value to set.
*/
virtual void setValue(const std::string& s)
{
std::vector<std::string> slist = Utils::split2(s, ',');
for (auto& ts : slist)
Utils::trim(ts);
if (slist.empty())
throw arg_val_error("Missing value for argument '" + m_longname +
"'.");
m_rawVal = s;
if (!m_set)
m_var.clear();
m_var.reserve(m_var.size() + slist.size());
m_var.insert(m_var.end(), slist.begin(), slist.end());
m_set = true;
}
/**
Reset the argument's state.
Set the internal state of the argument and it's referenced variable
as if no command-line parsing had occurred.
\note For testing. Not intended to be called from user code.
*/
virtual void reset()
{
m_var = m_defaultVal;
m_set = false;
m_hidden = false;
}
/**
Return a string representation of an Arg's default value, or an
empty string if none exists.
\return Default value as a string.
*/
virtual std::string defaultVal() const
{
std::string s;
for (size_t i = 0; i < m_defaultVal.size(); ++i)
{
if (i > 0)
s += ", ";
s += m_defaultVal[i];
}
return s;
}
private:
std::vector<std::string>& m_var;
std::vector<std::string> m_defaultVal;
};
/**
Parses command lines, provides validation and stores found values in
bound variables. Add arguments with \ref add. When all arguments
have been added, use \ref parse to validate command line and assign
values to variables bound with \ref add.
*/
class ProgramArgs
{
public:
/**
Add a string argument to the list of arguments.
\param name Name of argument. Argument names are specified as
"longname[,shortname]", where shortname is an optional one-character
abbreviation.
\param description Description of the argument.
\param var Reference to variable to bind to argument.
\param def Default value of argument.
\return Reference to the new argument.
*/
Arg& add(const std::string& name, const std::string description,
std::string& var, std::string def)
{
return add<std::string>(name, description, var, def);
}
/**
Add a list-based (vector) string argument
\param name Name of argument. Argument names are specified as
"longname[,shortname]", where shortname is an optional one-character
abbreviation.
\param description Description of the argument.
\param var Reference to variable to bind to argument.
\return Reference to the new argument.
*/
Arg& add(const std::string& name, const std::string& description,
std::vector<std::string>& var)
{
return add<std::string>(name, description, var);
}
/**
Return whether the argument (as specified by it's longname) had
its value set during parsing.
*/
bool set(const std::string& name) const
{
Arg *arg = findLongArg(name);
if (arg)
return arg->set();
return false;
}
/**
Add a list-based (vector) argument.
\param name Name of argument. Argument names are specified as
"longname[,shortname]", where shortname is an optional one-character
abbreviation.
\param description Description of the argument.
\param var Reference to variable to bind to argument.
\return Reference to the new argument.
*/
template<typename T>
Arg& add(const std::string& name, const std::string& description,
std::vector<T>& var)
{