-
Notifications
You must be signed in to change notification settings - Fork 11
/
popl.hpp
1257 lines (967 loc) · 30.6 KB
/
popl.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
/***
____ __ ____ __
( _ \ / \( _ \( )
) __/( O )) __// (_/\
(__) \__/(__) \____/
version 1.2.0
https://github.com/badaix/popl
This file is part of popl (program options parser lib)
Copyright (C) 2015-2018 Johannes Pohl
This software may be modified and distributed under the terms
of the MIT license. See the LICENSE file for details.
***/
/// checked with clang-tidy:
/// run-clang-tidy-3.8.py -header-filter='.*' -checks='*,-misc-definitions-in-headers,-google-readability-braces-around-statements,-readability-braces-around-statements,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-build-using-namespace,-google-build-using-namespace'
#ifndef POPL_HPP
#define POPL_HPP
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <vector>
namespace popl
{
#define POPL_VERSION "1.2.0"
/// Option's argument type
/**
* Switch has "no" argument
* Value has "required" argument
* Implicit has "optional" argument
*/
enum class Argument
{
no = 0, // option never takes an argument
required, // option always requires an argument
optional // option may take an argument
};
/// Option's attribute
/**
* inactive: Option is not set and will not be parsed
* hidden: Option is active, but will not show up in the help message
* required: Option must be set on the command line. Otherwise an exception will be thrown
* optional: Option must not be set. Default attribute.
* advanced: Option is advanced and will only show up in the advanced help message
* expoert: Option is expert and will only show up in the expert help message
*/
enum class Attribute
{
inactive = 0,
hidden = 1,
required = 2,
optional = 3,
advanced = 4,
expert = 5
};
/// Option name type. Used in invalid_option exception.
/**
* unspecified: not specified
* short_name: The option's short name
* long_name: The option's long name
*/
enum class OptionName
{
unspecified,
short_name,
long_name
};
/// Abstract Base class for Options
/**
* Base class for Options
* holds just configuration data, no runtime data.
* Option is not bound to a special type "T"
*/
class Option
{
friend class OptionParser;
public:
/// Construct an Option
/// @param short_name the options's short name. Must be empty or one character.
/// @param long_name the option's long name. Can be empty.
/// @param description the Option's description that will be shown in the help message
Option(const std::string& short_name, const std::string& long_name, std::string description);
/// Destructor
virtual ~Option() = default;
/// default copy constructor
Option(const Option&) = default;
/// default move constructor
Option(Option&&) = default;
/// default assignement operator
Option& operator=(const Option&) = default;
/// default move assignement operator
Option& operator=(Option&&) = default;
/// Get the Option's short name
/// @return character of the options's short name or 0 if no short name is defined
char short_name() const;
/// Get the Option's long name
/// @return the long name of the Option. Empty string if no long name is defined
std::string long_name() const;
/// Get the Option's long or short name
/// @param what_name the option's name to return
/// @param what_hyphen preced the returned name with (double-)hypen
/// @return the requested name of the Option. Empty string if not defined.
std::string name(OptionName what_name, bool with_hypen = false) const;
/// Get the Option's description
/// @return the description
std::string description() const;
/// Get the Option's default value
/// @param out stream to write the default value to
/// @return true if a default value is available, false if not
virtual bool get_default(std::ostream& out) const = 0;
/// Set the Option's attribute
/// @param attribute
void set_attribute(const Attribute& attribute);
/// Get the Option's attribute
/// @return the Options's attribute
Attribute attribute() const;
/// Get the Option's argument type
/// @return argument type (no, required, optional)
virtual Argument argument_type() const = 0;
/// Check how often the Option is set on command line
/// @return the Option's count on command line
virtual size_t count() const = 0;
/// Check if the Option is set
/// @return true if set at least once
virtual bool is_set() const = 0;
protected:
/// Parse the command line option and fill the internal data structure
/// @param what_name short or long option name
/// @param value the value as given on command line
virtual void parse(OptionName what_name, const char* value) = 0;
/// Clear the internal data structure
virtual void clear() = 0;
std::string short_name_;
std::string long_name_;
std::string description_;
Attribute attribute_;
};
/// Value option with optional default value
/**
* Value option with optional default value
* If set, it requires an argument
*/
template<class T>
class Value : public Option
{
public:
/// Construct an Value Option
/// @param short_name the option's short name. Must be empty or one character.
/// @param long_name the option's long name. Can be empty.
/// @param description the Option's description that will be shown in the help message
Value(const std::string& short_name, const std::string& long_name, const std::string& description);
/// Construct an Value Option
/// @param short_name the option's short name. Must be empty or one character.
/// @param long_name the option's long name. Can be empty.
/// @param description the Option's description that will be shown in the help message
/// @param default_val the Option's default value
/// @param assign_to pointer to a variable to assign the parsed command line value to
Value(const std::string& short_name, const std::string& long_name, const std::string& description, const T& default_val, T* assign_to = nullptr);
size_t count() const override;
bool is_set() const override;
/// Assign the last parsed command line value to "var"
/// @param var pointer to the variable where is value is written to
void assign_to(T* var);
/// Manually set the Option's value. Deletes current value(s)
/// @param value the new value of the option
void set_value(const T& value);
/// Get the Option's value. Will throw if option at index idx is not available
/// @param idx the zero based index of the value (if set multiple times)
/// @return the Option's value at index "idx"
T value(size_t idx = 0) const;
/// Set the Option's default value
/// @param value the default value if not specified on command line
void set_default(const T& value);
/// Check if the Option has a default value
/// @return true if the Option has a default value
bool has_default() const;
/// Get the Option's default value. Will throw if no default is set.
/// @return the Option's default value
T get_default() const;
bool get_default(std::ostream& out) const override;
Argument argument_type() const override;
protected:
void parse(OptionName what_name, const char* value) override;
std::unique_ptr<T> default_;
virtual void update_reference();
virtual void add_value(const T& value);
void clear() override;
T* assign_to_;
std::vector<T> values_;
};
/// Value option with implicit default value
/**
* Value option with implicit default value
* If set, an argument is optional
* -without argument it carries the implicit default value
* -with argument it carries the explicit value
*/
template<class T>
class Implicit : public Value<T>
{
public:
Implicit(const std::string& short_name, const std::string& long_name, const std::string& description, const T& implicit_val, T* assign_to = nullptr);
Argument argument_type() const override;
protected:
void parse(OptionName what_name, const char* value) override;
};
/// Value option without value
/**
* Value option without value
* Does not require an argument
* Can be either set or not set
*/
class Switch : public Value<bool>
{
public:
Switch(const std::string& short_name, const std::string& long_name, const std::string& description, bool* assign_to = nullptr);
void set_default(const bool& value) = delete;
Argument argument_type() const override;
protected:
void parse(OptionName what_name, const char* value) override;
};
using Option_ptr = std::shared_ptr<Option>;
/// OptionParser manages all Options
/**
* OptionParser manages all Options
* Add Options (Option_Type = Value<T>, Implicit<T> or Switch) with "add<Option_Type>(option params)"
* Call "parse(argc, argv)" to trigger parsing of the options and to
* fill "non_option_args" and "unknown_options"
*/
class OptionParser
{
public:
/// Construct the OptionParser
/// @param description used for the help message
explicit OptionParser(std::string description = "");
/// Destructor
virtual ~OptionParser() = default;
/// Add an Option e.g. 'add<Value<int>>("i", "int", "description for the -i option")'
/// @param T the option type (Value, Switch, Implicit)
/// @param attribute the Option's attribute (inactive, hidden, required, optional, ...)
/// @param Ts the Option's parameter
template<typename T, Attribute attribute, typename... Ts>
std::shared_ptr<T> add(Ts&&... params);
/// Add an Option e.g. 'add<Value<int>>("i", "int", "description for the -i option")'
/// @param T the option type (Value, Switch, Implicit)
/// @param Ts the Option's parameter
template<typename T, typename... Ts>
std::shared_ptr<T> add(Ts&&... params);
/// Parse the command line into the added Options
/// @param argc command line argument count
/// @param argv command line arguments
void parse(int argc, const char * const argv[]);
/// Produce a help message
/// @param max_attribute show options up to this level (optional, advanced, expert)
/// @return the help message
std::string help(const Attribute& max_attribute = Attribute::optional) const;
/// Get the OptionParser's description
/// @return the description as given during construction
std::string description() const;
/// Get all options that where added with "add"
/// @return a vector of the contained Options
const std::vector<Option_ptr>& options() const;
/// Get command line arguments without option
/// e.g. "-i 5 hello" => hello
/// e.g. "-i 5 -- from here non option args" => "from", "here", "non", "option", "args"
/// @return vector to "stand-alone" command line arguments
const std::vector<std::string>& non_option_args() const;
/// Get unknown command options
/// e.g. '--some_unknown_option="hello"'
/// @return vector to "stand-alone" command line arguments
const std::vector<std::string>& unknown_options() const;
/// Get an Option by it's long name
/// @param the Option's long name
/// @return a pointer of type "Value, Switch, Implicit" to the Option or nullptr
template<typename T>
std::shared_ptr<T> get_option(const std::string& long_name) const;
/// Get an Option by it's short name
/// @param the Option's short name
/// @return a pointer of type "Value, Switch, Implicit" to the Option or nullptr
template<typename T>
std::shared_ptr<T> get_option(char short_name) const;
protected:
std::vector<Option_ptr> options_;
std::string description_;
std::vector<std::string> non_option_args_;
std::vector<std::string> unknown_options_;
Option_ptr find_option(const std::string& long_name) const;
Option_ptr find_option(char short_name) const;
};
class invalid_option : public std::invalid_argument
{
public:
enum class Error
{
missing_argument,
invalid_argument,
too_many_arguments,
missing_option
};
invalid_option(const Option* option, invalid_option::Error error, OptionName what_name, std::string value, const std::string& text) :
std::invalid_argument(text.c_str()), option_(option), error_(error), what_name_(what_name), value_(std::move(value))
{
}
invalid_option(const Option* option, invalid_option::Error error, const std::string& text) :
invalid_option(option, error, OptionName::unspecified, "", text)
{
}
const Option* option() const
{
return option_;
}
Error error() const
{
return error_;
}
OptionName what_name() const
{
return what_name_;
}
std::string value() const
{
return value_;
}
private:
const Option* option_;
Error error_;
OptionName what_name_;
std::string value_;
};
/// Base class for an OptionPrinter
/**
* OptionPrinter creates a help message for a given OptionParser
*/
class OptionPrinter
{
public:
/// Constructor
/// @param option_parser the OptionParser to create the help message from
explicit OptionPrinter(const OptionParser* option_parser) : option_parser_(option_parser)
{
}
/// Destructor
virtual ~OptionPrinter() = default;
/// Create a help message
/// @param max_attribute show options up to this level (optional, advanced, expert)
/// @return the help message
virtual std::string print(const Attribute& max_attribute = Attribute::optional) const = 0;
protected:
const OptionParser* option_parser_;
};
/// Option printer for the console
/**
* Standard console option printer
* Creates a human readable help message
*/
class ConsoleOptionPrinter : public OptionPrinter
{
public:
explicit ConsoleOptionPrinter(const OptionParser* option_parser);
~ConsoleOptionPrinter() override = default;
std::string print(const Attribute& max_attribute = Attribute::optional) const override;
private:
std::string to_string(Option_ptr option) const;
};
/// Option printer for man pages
/**
* Creates help messages in groff format that can be used in man pages
*/
class GroffOptionPrinter : public OptionPrinter
{
public:
explicit GroffOptionPrinter(const OptionParser* option_parser);
~GroffOptionPrinter() override = default;
std::string print(const Attribute& max_attribute = Attribute::optional) const override;
private:
std::string to_string(Option_ptr option) const;
};
/// Option printer for bash completion
/**
* Creates a script with all options (short and long) that can be used for bash completion
*/
class BashCompletionOptionPrinter : public OptionPrinter
{
public:
BashCompletionOptionPrinter(const OptionParser* option_parser, std::string program_name);
~BashCompletionOptionPrinter() override = default;
std::string print(const Attribute& max_attribute = Attribute::optional) const override;
private:
std::string program_name_;
};
/// Option implementation /////////////////////////////////
inline Option::Option(const std::string& short_name, const std::string& long_name, std::string description) :
short_name_(short_name),
long_name_(long_name),
description_(std::move(description)),
attribute_(Attribute::optional)
{
if (short_name.size() > 1)
throw std::invalid_argument("length of short name must be <= 1: '" + short_name + "'");
if (short_name.empty() && long_name.empty())
throw std::invalid_argument("short and long name are empty");
}
inline char Option::short_name() const
{
if (!short_name_.empty())
return short_name_[0];
return 0;
}
inline std::string Option::long_name() const
{
return long_name_;
}
inline std::string Option::name(OptionName what_name, bool with_hypen) const
{
if (what_name == OptionName::short_name)
return short_name_.empty()?"":((with_hypen?"-":"") + short_name_);
if (what_name == OptionName::long_name)
return long_name_.empty()?"":((with_hypen?"--":"") + long_name_);
return "";
}
inline std::string Option::description() const
{
return description_;
}
inline void Option::set_attribute(const Attribute& attribute)
{
attribute_ = attribute;
}
inline Attribute Option::attribute() const
{
return attribute_;
}
/// Value implementation /////////////////////////////////
template<class T>
inline Value<T>::Value(const std::string& short_name, const std::string& long_name, const std::string& description) :
Option(short_name, long_name, description),
assign_to_(nullptr)
{
}
template<class T>
inline Value<T>::Value(const std::string& short_name, const std::string& long_name, const std::string& description, const T& default_val, T* assign_to) :
Value<T>(short_name, long_name, description)
{
assign_to_ = assign_to;
set_default(default_val);
}
template<class T>
inline size_t Value<T>::count() const
{
return values_.size();
}
template<class T>
inline bool Value<T>::is_set() const
{
return !values_.empty();
}
template<class T>
inline void Value<T>::assign_to(T* var)
{
assign_to_ = var;
update_reference();
}
template<class T>
inline void Value<T>::set_value(const T& value)
{
clear();
add_value(value);
}
template<class T>
inline T Value<T>::value(size_t idx) const
{
if (!this->is_set() && default_)
return *default_;
if (!is_set() || (idx >= count()))
{
std::stringstream optionStr;
if (!is_set())
optionStr << "option not set: \"";
else
optionStr << "index out of range (" << idx << ") for \"";
if (short_name() != 0)
optionStr << "-" << short_name();
else
optionStr << "--" << long_name();
optionStr << "\"";
throw std::out_of_range(optionStr.str());
}
return values_[idx];
}
template<class T>
inline void Value<T>::set_default(const T& value)
{
this->default_.reset(new T);
*this->default_ = value;
update_reference();
}
template<class T>
inline bool Value<T>::has_default() const
{
return (this->default_ != nullptr);
}
template<class T>
inline T Value<T>::get_default() const
{
if (!has_default())
throw std::runtime_error("no default value set");
return *this->default_;
}
template<class T>
inline bool Value<T>::get_default(std::ostream& out) const
{
if (!has_default())
return false;
out << *this->default_;
return true;
}
template<class T>
inline Argument Value<T>::argument_type() const
{
return Argument::required;
}
template<>
inline void Value<std::string>::parse(OptionName what_name, const char* value)
{
if (strlen(value) == 0)
throw invalid_option(this, invalid_option::Error::missing_argument, what_name, value, "missing argument for " + name(what_name, true));
add_value(value);
}
template<class T>
inline void Value<T>::parse(OptionName what_name, const char* value)
{
T parsed_value;
std::string strValue;
if (value != nullptr)
strValue = value;
std::istringstream is(strValue);
int valuesRead = 0;
while (is.good())
{
if (is.peek() != EOF)
is >> parsed_value;
else
break;
valuesRead++;
}
if (is.fail())
throw invalid_option(this, invalid_option::Error::invalid_argument, what_name, value, "invalid argument for " + name(what_name, true) + ": '" + strValue + "'");
if (valuesRead > 1)
throw invalid_option(this, invalid_option::Error::too_many_arguments, what_name, value, "too many arguments for " + name(what_name, true) + ": '" + strValue + "'");
if (strValue.empty())
throw invalid_option(this, invalid_option::Error::missing_argument, what_name, "", "missing argument for " + name(what_name, true));
this->add_value(parsed_value);
}
template<class T>
inline void Value<T>::update_reference()
{
if (this->assign_to_)
{
if (this->is_set() || default_)
*this->assign_to_ = value();
}
}
template<class T>
inline void Value<T>::add_value(const T& value)
{
values_.push_back(value);
update_reference();
}
template<class T>
inline void Value<T>::clear()
{
values_.clear();
update_reference();
}
/// Implicit implementation /////////////////////////////////
template<class T>
inline Implicit<T>::Implicit(const std::string& short_name, const std::string& long_name, const std::string& description, const T& implicit_val, T* assign_to) :
Value<T>(short_name, long_name, description, implicit_val, assign_to)
{
}
template<class T>
inline Argument Implicit<T>::argument_type() const
{
return Argument::optional;
}
template<class T>
inline void Implicit<T>::parse(OptionName what_name, const char* value)
{
if ((value != nullptr) && (strlen(value) > 0))
Value<T>::parse(what_name, value);
else
this->add_value(*this->default_);
}
/// Switch implementation /////////////////////////////////
inline Switch::Switch(const std::string& short_name, const std::string& long_name, const std::string& description, bool* assign_to) :
Value<bool>(short_name, long_name, description, false, assign_to)
{
}
inline void Switch::parse(OptionName /*what_name*/, const char* /*value*/)
{
add_value(true);
}
inline Argument Switch::argument_type() const
{
return Argument::no;
}
/// OptionParser implementation /////////////////////////////////
inline OptionParser::OptionParser(std::string description) : description_(std::move(description))
{
}
template<typename T, typename... Ts>
inline std::shared_ptr<T> OptionParser::add(Ts&&... params)
{
return add<T, Attribute::optional>(std::forward<Ts>(params)...);
}
template<typename T, Attribute attribute, typename... Ts>
inline std::shared_ptr<T> OptionParser::add(Ts&&... params)
{
static_assert(
std::is_base_of<Option, typename std::decay<T>::type>::value,
"type T must be Switch, Value or Implicit"
);
std::shared_ptr<T> option = std::make_shared<T>(std::forward<Ts>(params)...);
for (const auto& o: options_)
{
if ((option->short_name() != 0) && (option->short_name() == o->short_name()))
throw std::invalid_argument("duplicate short option name '-" + std::string(1, option->short_name()) + "'");
if (!option->long_name().empty() && (option->long_name() == (o->long_name())))
throw std::invalid_argument("duplicate long option name '--" + option->long_name() + "'");
}
option->set_attribute(attribute);
options_.push_back(option);
return option;
}
inline std::string OptionParser::description() const
{
return description_;
}
inline const std::vector<Option_ptr>& OptionParser::options() const
{
return options_;
}
inline const std::vector<std::string>& OptionParser::non_option_args() const
{
return non_option_args_;
}
inline const std::vector<std::string>& OptionParser::unknown_options() const
{
return unknown_options_;
}
inline Option_ptr OptionParser::find_option(const std::string& long_name) const
{
for (const auto& option: options_)
if (option->long_name() == long_name)
return option;
return nullptr;
}
inline Option_ptr OptionParser::find_option(char short_name) const
{
for (const auto& option: options_)
if (option->short_name() == short_name)
return option;
return nullptr;
}
template<typename T>
inline std::shared_ptr<T> OptionParser::get_option(const std::string& long_name) const
{
Option_ptr option = find_option(long_name);
if (!option)
throw std::invalid_argument("option not found: " + long_name);
auto result = std::dynamic_pointer_cast<T>(option);
if (!result)
throw std::invalid_argument("cannot cast option to T: " + long_name);
return result;
}
template<typename T>
inline std::shared_ptr<T> OptionParser::get_option(char short_name) const
{
Option_ptr option = find_option(short_name);
if (!option)
throw std::invalid_argument("option not found: " + std::string(1, short_name));
auto result = std::dynamic_pointer_cast<T>(option);
if (!result)
throw std::invalid_argument("cannot cast option to T: " + std::string(1, short_name));
return result;
}
inline void OptionParser::parse(int argc, const char * const argv[])
{
unknown_options_.clear();
non_option_args_.clear();
for (auto& opt : options_)
opt->clear();
for (int n=1; n<argc; ++n)
{
const std::string arg(argv[n]);
if (arg == "--")
{
///from here on only non opt args
for (int m=n+1; m<argc; ++m)
non_option_args_.emplace_back(argv[m]);
}
else if (arg.find("--") == 0)
{
/// long option arg
std::string opt = arg.substr(2);
std::string optarg;
size_t equalIdx = opt.find('=');
if (equalIdx != std::string::npos)
{
optarg = opt.substr(equalIdx + 1);
opt.resize(equalIdx);
}
Option_ptr option = find_option(opt);
if (option && (option->attribute() == Attribute::inactive))
option = nullptr;
if (option)
{
if (option->argument_type() == Argument::no)
{
if (!optarg.empty())
option = nullptr;
}
else if (option->argument_type() == Argument::required)
{
if (optarg.empty() && n < argc-1)
optarg = argv[++n];
}
}
if (option)
option->parse(OptionName::long_name, optarg.c_str());
else
unknown_options_.push_back(arg);
}
else if (arg.find('-') == 0)
{
/// short option arg
std::string opt = arg.substr(1);
bool unknown = false;
for (size_t m=0; m<opt.size(); ++m)
{
char c = opt[m];
std::string optarg;
Option_ptr option = find_option(c);
if (option && (option->attribute() == Attribute::inactive))
option = nullptr;
if (option)
{
if (option->argument_type() == Argument::required)
{
/// use the rest of the current argument as optarg
optarg = opt.substr(m + 1);
/// or the next arg
if (optarg.empty() && n < argc-1)
optarg = argv[++n];
m = opt.size();
}
else if (option->argument_type() == Argument::optional)
{
/// use the rest of the current argument as optarg
optarg = opt.substr(m + 1);
m = opt.size();
}
}