forked from ibenes/freecell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argparse.h
1247 lines (1106 loc) · 37.4 KB
/
argparse.h
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
/*
__ _ _ __ __ _ _ __ __ _ _ __ ___ ___
/ _` | '__/ _` | '_ \ / _` | '__/ __|/ _ \ Argument Parser for Modern C++
| (_| | | | (_| | |_) | (_| | | \__ \ __/ http://github.com/p-ranav/argparse
\__,_|_| \__, | .__/ \__,_|_| |___/\___|
|___/|_|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2019-2022 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>
and other contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <algorithm>
#include <any>
#include <array>
#include <cerrno>
#include <charconv>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
namespace argparse {
namespace details { // namespace for helper methods
template <typename T, typename = void>
struct HasContainerTraits : std::false_type {};
template <> struct HasContainerTraits<std::string> : std::false_type {};
template <typename T>
struct HasContainerTraits<
T, std::void_t<typename T::value_type, decltype(std::declval<T>().begin()),
decltype(std::declval<T>().end()),
decltype(std::declval<T>().size())>> : std::true_type {};
template <typename T>
static constexpr bool IsContainer = HasContainerTraits<T>::value;
template <typename T, typename = void>
struct HasStreamableTraits : std::false_type {};
template <typename T>
struct HasStreamableTraits<
T,
std::void_t<decltype(std::declval<std::ostream &>() << std::declval<T>())>>
: std::true_type {};
template <typename T>
static constexpr bool IsStreamable = HasStreamableTraits<T>::value;
constexpr std::size_t repr_max_container_size = 5;
template <typename T> std::string repr(T const &val) {
if constexpr (std::is_same_v<T, bool>) {
return val ? "true" : "false";
} else if constexpr (std::is_convertible_v<T, std::string_view>) {
return '"' + std::string{std::string_view{val}} + '"';
} else if constexpr (IsContainer<T>) {
std::stringstream out;
out << "{";
const auto size = val.size();
if (size > 1) {
out << repr(*val.begin());
std::for_each(
std::next(val.begin()),
std::next(val.begin(),
std::min<std::size_t>(size, repr_max_container_size) - 1),
[&out](const auto &v) { out << " " << repr(v); });
if (size <= repr_max_container_size) {
out << " ";
} else {
out << "...";
}
}
if (size > 0) {
out << repr(*std::prev(val.end()));
}
out << "}";
return out.str();
} else if constexpr (IsStreamable<T>) {
std::stringstream out;
out << val;
return out.str();
} else {
return "<not representable>";
}
}
namespace {
template <typename T> constexpr bool standard_signed_integer = false;
template <> constexpr bool standard_signed_integer<signed char> = true;
template <> constexpr bool standard_signed_integer<short int> = true;
template <> constexpr bool standard_signed_integer<int> = true;
template <> constexpr bool standard_signed_integer<long int> = true;
template <> constexpr bool standard_signed_integer<long long int> = true;
template <typename T> constexpr bool standard_unsigned_integer = false;
template <> constexpr bool standard_unsigned_integer<unsigned char> = true;
template <> constexpr bool standard_unsigned_integer<unsigned short int> = true;
template <> constexpr bool standard_unsigned_integer<unsigned int> = true;
template <> constexpr bool standard_unsigned_integer<unsigned long int> = true;
template <>
constexpr bool standard_unsigned_integer<unsigned long long int> = true;
} // namespace
constexpr int radix_8 = 8;
constexpr int radix_10 = 10;
constexpr int radix_16 = 16;
template <typename T>
constexpr bool standard_integer =
standard_signed_integer<T> || standard_unsigned_integer<T>;
template <class F, class Tuple, class Extra, std::size_t... I>
constexpr decltype(auto) apply_plus_one_impl(F &&f, Tuple &&t, Extra &&x,
std::index_sequence<I...> /*unused*/) {
return std::invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...,
std::forward<Extra>(x));
}
template <class F, class Tuple, class Extra>
constexpr decltype(auto) apply_plus_one(F &&f, Tuple &&t, Extra &&x) {
return details::apply_plus_one_impl(
std::forward<F>(f), std::forward<Tuple>(t), std::forward<Extra>(x),
std::make_index_sequence<
std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
}
constexpr auto pointer_range(std::string_view s) noexcept {
return std::tuple(s.data(), s.data() + s.size());
}
template <class CharT, class Traits>
constexpr bool starts_with(std::basic_string_view<CharT, Traits> prefix,
std::basic_string_view<CharT, Traits> s) noexcept {
return s.substr(0, prefix.size()) == prefix;
}
enum class chars_format {
scientific = 0x1,
fixed = 0x2,
hex = 0x4,
general = fixed | scientific
};
struct ConsumeHexPrefixResult {
bool is_hexadecimal;
std::string_view rest;
};
using namespace std::literals;
constexpr auto consume_hex_prefix(std::string_view s)
-> ConsumeHexPrefixResult {
if (starts_with("0x"sv, s) || starts_with("0X"sv, s)) {
s.remove_prefix(2);
return {true, s};
}
return {false, s};
}
template <class T, auto Param>
inline auto do_from_chars(std::string_view s) -> T {
T x;
auto [first, last] = pointer_range(s);
auto [ptr, ec] = std::from_chars(first, last, x, Param);
if (ec == std::errc()) {
if (ptr == last) {
return x;
}
throw std::invalid_argument{"pattern does not match to the end"};
}
if (ec == std::errc::invalid_argument) {
throw std::invalid_argument{"pattern not found"};
}
if (ec == std::errc::result_out_of_range) {
throw std::range_error{"not representable"};
}
return x; // unreachable
}
template <class T, auto Param = 0> struct parse_number {
auto operator()(std::string_view s) -> T {
return do_from_chars<T, Param>(s);
}
};
template <class T> struct parse_number<T, radix_16> {
auto operator()(std::string_view s) -> T {
if (auto [ok, rest] = consume_hex_prefix(s); ok) {
return do_from_chars<T, radix_16>(rest);
}
throw std::invalid_argument{"pattern not found"};
}
};
template <class T> struct parse_number<T> {
auto operator()(std::string_view s) -> T {
auto [ok, rest] = consume_hex_prefix(s);
if (ok) {
return do_from_chars<T, radix_16>(rest);
}
if (starts_with("0"sv, s)) {
return do_from_chars<T, radix_8>(rest);
}
return do_from_chars<T, radix_10>(rest);
}
};
namespace {
template <class T> inline const auto generic_strtod = nullptr;
template <> inline const auto generic_strtod<float> = strtof;
template <> inline const auto generic_strtod<double> = strtod;
template <> inline const auto generic_strtod<long double> = strtold;
} // namespace
template <class T> inline auto do_strtod(std::string const &s) -> T {
if (isspace(static_cast<unsigned char>(s[0])) || s[0] == '+') {
throw std::invalid_argument{"pattern not found"};
}
auto [first, last] = pointer_range(s);
char *ptr;
errno = 0;
auto x = generic_strtod<T>(first, &ptr);
if (errno == 0) {
if (ptr == last) {
return x;
}
throw std::invalid_argument{"pattern does not match to the end"};
}
if (errno == ERANGE) {
throw std::range_error{"not representable"};
}
return x; // unreachable
}
template <class T> struct parse_number<T, chars_format::general> {
auto operator()(std::string const &s) -> T {
if (auto r = consume_hex_prefix(s); r.is_hexadecimal) {
throw std::invalid_argument{
"chars_format::general does not parse hexfloat"};
}
return do_strtod<T>(s);
}
};
template <class T> struct parse_number<T, chars_format::hex> {
auto operator()(std::string const &s) -> T {
if (auto r = consume_hex_prefix(s); !r.is_hexadecimal) {
throw std::invalid_argument{"chars_format::hex parses hexfloat"};
}
return do_strtod<T>(s);
}
};
template <class T> struct parse_number<T, chars_format::scientific> {
auto operator()(std::string const &s) -> T {
if (auto r = consume_hex_prefix(s); r.is_hexadecimal) {
throw std::invalid_argument{
"chars_format::scientific does not parse hexfloat"};
}
if (s.find_first_of("eE") == std::string::npos) {
throw std::invalid_argument{
"chars_format::scientific requires exponent part"};
}
return do_strtod<T>(s);
}
};
template <class T> struct parse_number<T, chars_format::fixed> {
auto operator()(std::string const &s) -> T {
if (auto r = consume_hex_prefix(s); r.is_hexadecimal) {
throw std::invalid_argument{
"chars_format::fixed does not parse hexfloat"};
}
if (s.find_first_of("eE") != std::string::npos) {
throw std::invalid_argument{
"chars_format::fixed does not parse exponent part"};
}
return do_strtod<T>(s);
}
};
} // namespace details
enum class nargs_pattern {
optional,
any,
at_least_one
};
enum class default_arguments : unsigned int {
none = 0,
help = 1,
version = 2,
all = help | version,
};
inline default_arguments operator&(const default_arguments &a,
const default_arguments &b) {
return static_cast<default_arguments>(
static_cast<std::underlying_type<default_arguments>::type>(a) &
static_cast<std::underlying_type<default_arguments>::type>(b));
}
class ArgumentParser;
class Argument {
friend class ArgumentParser;
friend auto operator<<(std::ostream &stream, const ArgumentParser &parser)
-> std::ostream &;
template <std::size_t N, std::size_t... I>
explicit Argument(std::array<std::string_view, N> &&a,
std::index_sequence<I...> /*unused*/)
: m_is_optional((is_optional(a[I]) || ...)), m_is_required(false),
m_is_repeatable(false), m_is_used(false) {
((void)m_names.emplace_back(a[I]), ...);
std::sort(
m_names.begin(), m_names.end(), [](const auto &lhs, const auto &rhs) {
return lhs.size() == rhs.size() ? lhs < rhs : lhs.size() < rhs.size();
});
}
public:
template <std::size_t N>
explicit Argument(std::array<std::string_view, N> &&a)
: Argument(std::move(a), std::make_index_sequence<N>{}) {}
Argument &help(std::string help_text) {
m_help = std::move(help_text);
return *this;
}
template <typename T> Argument &default_value(T &&value) {
m_default_value_repr = details::repr(value);
m_default_value = std::forward<T>(value);
return *this;
}
Argument &required() {
m_is_required = true;
return *this;
}
Argument &implicit_value(std::any value) {
m_implicit_value = std::move(value);
m_num_args_range = NArgsRange{0, 0};
return *this;
}
template <class F, class... Args>
auto action(F &&callable, Args &&...bound_args)
-> std::enable_if_t<std::is_invocable_v<F, Args..., std::string const>,
Argument &> {
using action_type = std::conditional_t<
std::is_void_v<std::invoke_result_t<F, Args..., std::string const>>,
void_action, valued_action>;
if constexpr (sizeof...(Args) == 0) {
m_action.emplace<action_type>(std::forward<F>(callable));
} else {
m_action.emplace<action_type>(
[f = std::forward<F>(callable),
tup = std::make_tuple(std::forward<Args>(bound_args)...)](
std::string const &opt) mutable {
return details::apply_plus_one(f, tup, opt);
});
}
return *this;
}
auto &append() {
m_is_repeatable = true;
return *this;
}
template <char Shape, typename T>
auto scan() -> std::enable_if_t<std::is_arithmetic_v<T>, Argument &> {
static_assert(!(std::is_const_v<T> || std::is_volatile_v<T>),
"T should not be cv-qualified");
auto is_one_of = [](char c, auto... x) constexpr {
return ((c == x) || ...);
};
if constexpr (is_one_of(Shape, 'd') && details::standard_integer<T>) {
action(details::parse_number<T, details::radix_10>());
} else if constexpr (is_one_of(Shape, 'i') && details::standard_integer<T>) {
action(details::parse_number<T>());
} else if constexpr (is_one_of(Shape, 'u') &&
details::standard_unsigned_integer<T>) {
action(details::parse_number<T, details::radix_10>());
} else if constexpr (is_one_of(Shape, 'o') &&
details::standard_unsigned_integer<T>) {
action(details::parse_number<T, details::radix_8>());
} else if constexpr (is_one_of(Shape, 'x', 'X') &&
details::standard_unsigned_integer<T>) {
action(details::parse_number<T, details::radix_16>());
} else if constexpr (is_one_of(Shape, 'a', 'A') &&
std::is_floating_point_v<T>) {
action(details::parse_number<T, details::chars_format::hex>());
} else if constexpr (is_one_of(Shape, 'e', 'E') &&
std::is_floating_point_v<T>) {
action(details::parse_number<T, details::chars_format::scientific>());
} else if constexpr (is_one_of(Shape, 'f', 'F') &&
std::is_floating_point_v<T>) {
action(details::parse_number<T, details::chars_format::fixed>());
} else if constexpr (is_one_of(Shape, 'g', 'G') &&
std::is_floating_point_v<T>) {
action(details::parse_number<T, details::chars_format::general>());
} else {
static_assert(alignof(T) == 0, "No scan specification for T");
}
return *this;
}
Argument &nargs(std::size_t num_args) {
m_num_args_range = NArgsRange{num_args, num_args};
return *this;
}
Argument &nargs(std::size_t num_args_min, std::size_t num_args_max) {
m_num_args_range = NArgsRange{num_args_min, num_args_max};
return *this;
}
Argument &nargs(nargs_pattern pattern) {
switch (pattern) {
case nargs_pattern::optional:
m_num_args_range = NArgsRange{0, 1};
break;
case nargs_pattern::any:
m_num_args_range = NArgsRange{0, std::numeric_limits<std::size_t>::max()};
break;
case nargs_pattern::at_least_one:
m_num_args_range = NArgsRange{1, std::numeric_limits<std::size_t>::max()};
break;
}
return *this;
}
Argument &remaining() {
m_accepts_optional_like_value = true;
return nargs(nargs_pattern::any);
}
template <typename Iterator>
Iterator consume(Iterator start, Iterator end,
std::string_view used_name = {}) {
if (!m_is_repeatable && m_is_used) {
throw std::runtime_error("Duplicate argument");
}
m_is_used = true;
m_used_name = used_name;
const auto num_args_max = m_num_args_range.get_max();
const auto num_args_min = m_num_args_range.get_min();
std::size_t dist = 0;
if (num_args_max == 0) {
m_values.emplace_back(m_implicit_value);
std::visit([](const auto &f) { f({}); }, m_action);
return start;
}
if ((dist = static_cast<std::size_t>(std::distance(start, end))) >= num_args_min) {
if (num_args_max < dist) {
end = std::next(start, num_args_max);
}
if (!m_accepts_optional_like_value) {
end = std::find_if(start, end, Argument::is_optional);
dist = static_cast<std::size_t>(std::distance(start, end));
if (dist < num_args_min) {
throw std::runtime_error("Too few arguments");
}
}
struct ActionApply {
void operator()(valued_action &f) {
std::transform(first, last, std::back_inserter(self.m_values), f);
}
void operator()(void_action &f) {
std::for_each(first, last, f);
if (!self.m_default_value.has_value()) {
if (!self.m_accepts_optional_like_value) {
self.m_values.resize(std::distance(first, last));
}
}
}
Iterator first, last;
Argument &self;
};
std::visit(ActionApply{start, end, *this}, m_action);
return end;
}
if (m_default_value.has_value()) {
return start;
}
throw std::runtime_error("Too few arguments for '" +
std::string(m_used_name) + "'.");
}
/*
* @throws std::runtime_error if argument values are not valid
*/
void validate() const {
if (m_is_optional) {
// TODO: check if an implicit value was programmed for this argument
if (!m_is_used && !m_default_value.has_value() && m_is_required) {
throw_required_arg_not_used_error();
}
if (m_is_used && m_is_required && m_values.empty()) {
throw_required_arg_no_value_provided_error();
}
} else {
if (!m_num_args_range.contains(m_values.size()) && !m_default_value.has_value()) {
throw_nargs_range_validation_error();
}
}
}
std::size_t get_arguments_length() const {
return std::accumulate(std::begin(m_names), std::end(m_names),
std::size_t(0), [](const auto &sum, const auto &s) {
return sum + s.size() +
1; // +1 for space between names
});
}
friend std::ostream &operator<<(std::ostream &stream,
const Argument &argument) {
std::stringstream name_stream;
std::copy(std::begin(argument.m_names), std::end(argument.m_names),
std::ostream_iterator<std::string>(name_stream, " "));
stream << name_stream.str() << "\t" << argument.m_help;
if (argument.m_default_value.has_value()) {
if (!argument.m_help.empty()) {
stream << " ";
}
stream << "[default: " << argument.m_default_value_repr << "]";
} else if (argument.m_is_required) {
if (!argument.m_help.empty()) {
stream << " ";
}
stream << "[required]";
}
stream << "\n";
return stream;
}
template <typename T> bool operator!=(const T &rhs) const {
return !(*this == rhs);
}
/*
* Compare to an argument value of known type
* @throws std::logic_error in case of incompatible types
*/
template <typename T> bool operator==(const T &rhs) const {
if constexpr (!details::IsContainer<T>) {
return get<T>() == rhs;
} else {
auto lhs = get<T>();
return std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs),
std::end(rhs), [](const auto &lhs, const auto &rhs) {
return lhs == rhs;
});
}
}
private:
class NArgsRange {
std::size_t m_min;
std::size_t m_max;
public:
NArgsRange(std::size_t minimum, std::size_t maximum)
: m_min(minimum), m_max(maximum) {
if (minimum > maximum) {
throw std::logic_error("Range of number of arguments is invalid");
}
}
bool contains(std::size_t value) const {
return value >= m_min && value <= m_max;
}
bool is_exact() const {
return m_min == m_max;
}
bool is_right_bounded() const {
return m_max < std::numeric_limits<std::size_t>::max();
}
std::size_t get_min() const {
return m_min;
}
std::size_t get_max() const {
return m_max;
}
};
void throw_nargs_range_validation_error() const {
std::stringstream stream;
if (!m_used_name.empty()) {
stream << m_used_name << ": ";
}
if (m_num_args_range.is_exact()) {
stream << m_num_args_range.get_min();
} else if (m_num_args_range.is_right_bounded()) {
stream << m_num_args_range.get_min() << " to " << m_num_args_range.get_max();
} else {
stream << m_num_args_range.get_min() << " or more";
}
stream << " argument(s) expected. " << m_values.size() << " provided.";
throw std::runtime_error(stream.str());
}
void throw_required_arg_not_used_error() const {
std::stringstream stream;
stream << m_names[0] << ": required.";
throw std::runtime_error(stream.str());
}
void throw_required_arg_no_value_provided_error() const {
std::stringstream stream;
stream << m_used_name << ": no value provided.";
throw std::runtime_error(stream.str());
}
static constexpr int eof = std::char_traits<char>::eof();
static auto lookahead(std::string_view s) -> int {
if (s.empty()) {
return eof;
}
return static_cast<int>(static_cast<unsigned char>(s[0]));
}
/*
* decimal-literal:
* '0'
* nonzero-digit digit-sequence_opt
* integer-part fractional-part
* fractional-part
* integer-part '.' exponent-part_opt
* integer-part exponent-part
*
* integer-part:
* digit-sequence
*
* fractional-part:
* '.' post-decimal-point
*
* post-decimal-point:
* digit-sequence exponent-part_opt
*
* exponent-part:
* 'e' post-e
* 'E' post-e
*
* post-e:
* sign_opt digit-sequence
*
* sign: one of
* '+' '-'
*/
static bool is_decimal_literal(std::string_view s) {
auto is_digit = [](auto c) constexpr {
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return true;
default:
return false;
}
};
// precondition: we have consumed or will consume at least one digit
auto consume_digits = [=](std::string_view s) {
// NOLINTNEXTLINE(readability-qualified-auto)
auto it = std::find_if_not(std::begin(s), std::end(s), is_digit);
return s.substr(it - std::begin(s));
};
switch (lookahead(s)) {
case '0': {
s.remove_prefix(1);
if (s.empty()) {
return true;
}
goto integer_part;
}
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
s = consume_digits(s);
if (s.empty()) {
return true;
}
goto integer_part_consumed;
}
case '.': {
s.remove_prefix(1);
goto post_decimal_point;
}
default:
return false;
}
integer_part:
s = consume_digits(s);
integer_part_consumed:
switch (lookahead(s)) {
case '.': {
s.remove_prefix(1);
if (is_digit(lookahead(s))) {
goto post_decimal_point;
} else {
goto exponent_part_opt;
}
}
case 'e':
case 'E': {
s.remove_prefix(1);
goto post_e;
}
default:
return false;
}
post_decimal_point:
if (is_digit(lookahead(s))) {
s = consume_digits(s);
goto exponent_part_opt;
}
return false;
exponent_part_opt:
switch (lookahead(s)) {
case eof:
return true;
case 'e':
case 'E': {
s.remove_prefix(1);
goto post_e;
}
default:
return false;
}
post_e:
switch (lookahead(s)) {
case '-':
case '+':
s.remove_prefix(1);
}
if (is_digit(lookahead(s))) {
s = consume_digits(s);
return s.empty();
}
return false;
}
static bool is_optional(std::string_view name) {
return !is_positional(name);
}
/*
* positional:
* _empty_
* '-'
* '-' decimal-literal
* !'-' anything
*/
static bool is_positional(std::string_view name) {
switch (lookahead(name)) {
case eof:
return true;
case '-': {
name.remove_prefix(1);
if (name.empty()) {
return true;
}
return is_decimal_literal(name);
}
default:
return true;
}
}
/*
* Get argument value given a type
* @throws std::logic_error in case of incompatible types
*/
template <typename T> auto get() const -> std::conditional_t<details::IsContainer<T>, T, const T&> {
if (!m_values.empty()) {
if constexpr (details::IsContainer<T>) {
return any_cast_container<T>(m_values);
} else {
return *std::any_cast<T>(&m_values.front());
}
}
if (m_default_value.has_value()) {
return *std::any_cast<T>(&m_default_value);
}
if constexpr (details::IsContainer<T>) {
if (!m_accepts_optional_like_value) {
return any_cast_container<T>(m_values);
}
}
throw std::logic_error("No value provided for '" + m_names.back() + "'.");
}
/*
* Get argument value given a type.
* @pre The object has no default value.
* @returns The stored value if any, std::nullopt otherwise.
*/
template <typename T> auto present() const -> std::optional<T> {
if (m_default_value.has_value()) {
throw std::logic_error("Argument with default value always presents");
}
if (m_values.empty()) {
return std::nullopt;
}
if constexpr (details::IsContainer<T>) {
return any_cast_container<T>(m_values);
}
return std::any_cast<T>(m_values.front());
}
template <typename T>
static auto any_cast_container(const std::vector<std::any> &operand) -> T {
using ValueType = typename T::value_type;
T result;
std::transform(
std::begin(operand), std::end(operand), std::back_inserter(result),
[](const auto &value) { return *std::any_cast<ValueType>(&value); });
return result;
}
std::vector<std::string> m_names;
std::string_view m_used_name;
std::string m_help;
std::any m_default_value;
std::string m_default_value_repr;
std::any m_implicit_value;
using valued_action = std::function<std::any(const std::string &)>;
using void_action = std::function<void(const std::string &)>;
std::variant<valued_action, void_action> m_action{
std::in_place_type<valued_action>,
[](const std::string &value) { return value; }};
std::vector<std::any> m_values;
NArgsRange m_num_args_range {1, 1};
bool m_accepts_optional_like_value = false;
bool m_is_optional : true;
bool m_is_required : true;
bool m_is_repeatable : true;
bool m_is_used : true; // True if the optional argument is used by user
};
class ArgumentParser {
public:
explicit ArgumentParser(std::string program_name = {},
std::string version = "1.0",
default_arguments add_args = default_arguments::all)
: m_program_name(std::move(program_name)), m_version(std::move(version)) {
if ((add_args & default_arguments::help) == default_arguments::help) {
add_argument("-h", "--help")
.action([&](const auto &/*unused*/) {
std::cout << help().str();
std::exit(0);
})
.default_value(false)
.help("shows help message and exits")
.implicit_value(true)
.nargs(0);
}
if ((add_args & default_arguments::version) == default_arguments::version) {
add_argument("-v", "--version")
.action([&](const auto &/*unused*/) {
std::cout << m_version << std::endl;
std::exit(0);
})
.default_value(false)
.help("prints version information and exits")
.implicit_value(true)
.nargs(0);
}
}
ArgumentParser(ArgumentParser &&) noexcept = default;
ArgumentParser &operator=(ArgumentParser &&) = default;
ArgumentParser(const ArgumentParser &other)
: m_program_name(other.m_program_name),
m_version(other.m_version),
m_description(other.m_description),
m_epilog(other.m_epilog),
m_is_parsed(other.m_is_parsed),
m_positional_arguments(other.m_positional_arguments),
m_optional_arguments(other.m_optional_arguments) {
for (auto it = std::begin(m_positional_arguments);
it != std::end(m_positional_arguments); ++it) {
index_argument(it);
}
for (auto it = std::begin(m_optional_arguments);
it != std::end(m_optional_arguments); ++it) {
index_argument(it);
}
}
~ArgumentParser() = default;
ArgumentParser &operator=(const ArgumentParser &other) {
auto tmp = other;
std::swap(*this, tmp);
return *this;
}
// Parameter packing
// Call add_argument with variadic number of string arguments
template <typename... Targs> Argument &add_argument(Targs... f_args) {
using array_of_sv = std::array<std::string_view, sizeof...(Targs)>;
auto argument = m_optional_arguments.emplace(
std::cend(m_optional_arguments), array_of_sv{f_args...});
if (!argument->m_is_optional) {
m_positional_arguments.splice(std::cend(m_positional_arguments),
m_optional_arguments, argument);
}
index_argument(argument);
return *argument;
}