This repository has been archived by the owner on Sep 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonsugar.hpp
1189 lines (1052 loc) · 40 KB
/
nonsugar.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
// nonsugar
//
// Copyright iorate 2016-2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef NONSUGAR_HPP
#define NONSUGAR_HPP
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4100)
#pragma warning(disable: 4127)
#endif
#ifdef _MSVC_LANG
#if _MSVC_LANG >= 201703L
#define NONSUGAR_CXX17
#endif
#elif __cplusplus >= 201703L
#define NONSUGAR_CXX17
#endif
#include <algorithm>
#include <cstddef>
#include <exception>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <regex>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#ifdef NONSUGAR_CXX17
#include <optional>
#endif
namespace boost {
template <class T>
class optional;
} // namespace boost
namespace nonsugar {
namespace detail {
template <class ...>
using void_t = void;
} // namespace detail
template <class T, class = void>
struct optional_traits {};
template <class T>
struct optional_traits<std::shared_ptr<T>>
{
using value_type = T;
static std::shared_ptr<T> construct(T const &v) { return std::make_shared<T>(v); }
};
template <class T>
struct optional_traits<boost::optional<T>>
{
using value_type = T;
static boost::optional<T> construct(T const &v) { return v; }
};
#ifdef NONSUGAR_CXX17
template <class T>
struct optional_traits<std::optional<T>>
{
using value_type = T;
static std::optional<T> construct(T const &v) { return v; }
};
#endif
template <class T, class = void>
struct container_traits {};
template <class T>
struct container_traits<T, detail::void_t<
typename T::value_type, typename T::iterator, typename T::size_type, typename T::reference>>
{
using value_type = typename T::value_type;
static void push_back(T &t, value_type const &v) { t.insert(t.end(), v); }
};
namespace detail {
template <class T, class = void>
struct is_optional : std::false_type {};
template <class T>
struct is_optional<T, void_t<typename optional_traits<T>::value_type>> : std::true_type {};
template <class T, class = void>
struct is_container : std::false_type {};
template <class T>
struct is_container<T, void_t<typename container_traits<T>::value_type>> : std::true_type {};
template <class String, class T>
struct is_container_and_not_string :
std::integral_constant<bool, !std::is_same<String, T>::value && is_container<T>::value>
{};
template <class String, class T, class = void>
struct value_of { using type = T; };
template <class String>
struct value_of<String, void> { using type = int; };
template <class String, class T>
struct value_of<String, T, std::enable_if_t<is_optional<T>::value>>
{
using type = typename optional_traits<T>::value_type;
};
template <class String, class T>
struct value_of<String, T, std::enable_if_t<is_container_and_not_string<String, T>::value>>
{
using type = typename container_traits<T>::value_type;
};
template <class String, class OptionType, OptionType Option, class Value>
struct flag
{
using string_type = String;
using value_type = Value;
static constexpr OptionType option() { return Option; }
std::vector<typename String::value_type> short_names;
std::vector<String> long_names;
String placeholder;
String help;
std::shared_ptr<Value> default_value;
std::function<std::shared_ptr<typename value_of<String, Value>::type> (String const &)> read;
bool exclusive;
};
template <class String, class OptionType, OptionType Option, class Command>
struct subcommand
{
using string_type = String;
using command_type = Command;
static constexpr OptionType option() { return Option; }
String name;
String help;
Command command;
};
template <class String, class OptionType, OptionType Option, class Value>
struct argument
{
using string_type = String;
using value_type = Value;
static constexpr OptionType option() { return Option; }
String placeholder;
std::function<std::shared_ptr<typename value_of<String, Value>::type> (String const &)> read;
};
template <class String>
using sstream = std::basic_stringstream<
typename String::value_type, typename String::traits_type, typename String::allocator_type>;
template <class String>
inline String widen(char const *s)
{
sstream<String> ss;
ss << s;
return ss.str();
}
template <>
inline std::string widen<>(char const *s)
{
return s;
}
template <class T>
inline std::shared_ptr<T> copy_shared(T const &t)
{
return std::make_shared<T>(t);
}
} // namespace detail
template <class String>
class basic_error : public std::exception
{
public:
using string_type = String;
explicit basic_error(String const &message) : m_message(message) {}
char const *what() const noexcept override { return "nonsugar::basic_error"; }
String message() const { return m_message; }
private:
String m_message;
};
using error = basic_error<std::string>;
using werror = basic_error<std::wstring>;
using u16error = basic_error<std::u16string>;
using u32error = basic_error<std::u32string>;
template <class String, class T, class = void>
struct value_read
{
std::shared_ptr<T> operator()(String const &s) const
{
using sstream_t = detail::sstream<String>;
sstream_t ss(s);
T t;
ss >> t;
return ss && ss.peek() == sstream_t::traits_type::eof() ?
detail::copy_shared(t) : nullptr;
}
};
template <class String>
struct value_read<String, String>
{
std::shared_ptr<String> operator()(String const &s) const
{
return detail::copy_shared(s);
}
};
template <
class String,
class OptionType,
class Flags = std::tuple<>,
class Subcommands = std::tuple<>,
class Arguments = std::tuple<>>
class basic_command;
namespace detail {
template <class String, class OptionType, class Flags, class Subcommands, class Arguments>
inline basic_command<String, OptionType, Flags, Subcommands, Arguments> make_command(
String const &header, String const &footer, Flags const &flags, Subcommands const &subcommands,
Arguments const &arguments)
{
return { header, footer, flags, subcommands, arguments };
}
template <class Tuple, class T, std::size_t ...Indices>
inline auto tuple_append_impl(Tuple const &tuple, T &&t, std::index_sequence<Indices...>)
{
return std::make_tuple(std::get<Indices>(tuple)..., std::forward<T>(t));
}
template <class Tuple, class T>
inline auto tuple_append(Tuple const &tuple, T &&t)
{
return tuple_append_impl(
tuple, std::forward<T>(t), std::make_index_sequence<std::tuple_size<Tuple>::value>());
}
} // namespace detail
enum class flag_type
{
normal,
exclusive
};
template <class String, class OptionType, class Flags, class Subcommands, class Arguments>
class basic_command
{
public:
using char_type = typename String::value_type;
using string_type = String;
using option_type = OptionType;
using flag_tuple_type = Flags;
using subcommand_tuple_type = Subcommands;
using argument_tuple_type = Arguments;
explicit basic_command(String const &header, String const &footer = String()) :
m_header(header), m_footer(footer)
{}
basic_command(
String const &header, String const &footer, Flags const &flags,
Subcommands const &subcommands, Arguments const &arguments) :
m_header(header), m_footer(footer), m_flags(flags), m_subcommands(subcommands),
m_arguments(arguments)
{}
template <
OptionType Option, class Bool = bool,
std::enable_if_t<std::is_same<Bool, bool>::value> * = nullptr>
[[deprecated("use `flag<Option>(short_names, long_names, placeholder, help[, type])` instead.")]]
auto flag(
std::initializer_list<char_type> short_names, std::initializer_list<String> long_names,
String const &help, Bool is_help_or_version = false) const
{
detail::flag<String, OptionType, Option, void> f {
short_names, long_names, {}, help, {}, {}, is_help_or_version };
return detail::make_command<String, OptionType>(
m_header, m_footer, detail::tuple_append(m_flags, std::move(f)), m_subcommands,
m_arguments);
}
template <OptionType Option, class Value = void>
auto flag(
std::initializer_list<char_type> short_names, std::initializer_list<String> long_names,
String const &placeholder, String const &help, flag_type type = flag_type::normal) const
{
detail::flag<String, OptionType, Option, Value> f {
short_names, long_names, placeholder, help, {},
value_read<String, typename detail::value_of<String, Value>::type>(),
type == flag_type::exclusive };
return detail::make_command<String, OptionType>(
m_header, m_footer, detail::tuple_append(m_flags, std::move(f)), m_subcommands,
m_arguments);
}
template <OptionType Option, class Value>
auto flag(
std::initializer_list<char_type> short_names, std::initializer_list<String> long_names,
String const &placeholder, String const &help, Value const &default_value,
flag_type type = flag_type::normal) const
{
detail::flag<String, OptionType, Option, Value> f {
short_names, long_names, placeholder, help, std::make_shared<Value>(default_value),
value_read<String, typename detail::value_of<String, Value>::type>(),
type == flag_type::exclusive };
return detail::make_command<String, OptionType>(
m_header, m_footer, detail::tuple_append(m_flags, std::move(f)), m_subcommands,
m_arguments);
}
template <
OptionType Option, class Value, class Read,
std::enable_if_t<!std::is_convertible<Read, Value>::value> * = nullptr>
[[deprecated("process values after parsing or specialize `nonsugar::value_read` instead.")]]
auto flag(
std::initializer_list<char_type> short_names, std::initializer_list<String> long_names,
String const &placeholder, String const &help, Read &&read) const
{
std::function<std::shared_ptr<Value> (String const &)> r = std::forward<Read>(read);
detail::flag<String, OptionType, Option, Value> f {
short_names, long_names, placeholder, help, {}, std::move(r), false };
return detail::make_command<String, OptionType>(
m_header, m_footer, detail::tuple_append(m_flags, std::move(f)), m_subcommands,
m_arguments);
}
template <OptionType Option, class Value, class Read>
[[deprecated("process values after parsing or specialize `nonsugar::value_read` instead.")]]
auto flag(
std::initializer_list<char_type> short_names, std::initializer_list<String> long_names,
String const &placeholder, String const &help, Value const &default_value,
Read &&read) const
{
std::function<std::shared_ptr<Value> (String const &)> r = std::forward<Read>(read);
detail::flag<String, OptionType, Option, Value> f {
short_names, long_names, placeholder, help, std::make_shared<Value>(default_value),
std::move(r), false };
return detail::make_command<String, OptionType>(
m_header, m_footer, detail::tuple_append(m_flags, std::move(f)), m_subcommands,
m_arguments);
}
template <OptionType Option, class Command>
auto subcommand(String const &name, String const &help, Command const &command) const
{
detail::subcommand<String, OptionType, Option, Command> s { name, help, command };
return detail::make_command<String, OptionType>(
m_header, m_footer, m_flags, detail::tuple_append(m_subcommands, std::move(s)),
m_arguments);
}
template <OptionType Option, class Value>
auto argument(String const &placeholder) const
{
detail::argument<String, OptionType, Option, Value> a {
placeholder, value_read<String, typename detail::value_of<String, Value>::type>() };
return detail::make_command<String, OptionType>(
m_header, m_footer, m_flags, m_subcommands,
detail::tuple_append(m_arguments, std::move(a)));
}
template <OptionType Option, class Value, class Read>
[[deprecated("process values after parsing or specialize `nonsugar::value_read` instead.")]]
auto argument(String const &placeholder, Read &&read) const
{
detail::argument<String, OptionType, Option, Value> a {
placeholder, std::forward<Read>(read) };
return detail::make_command<String, OptionType>(
m_header, m_footer, m_flags, m_subcommands,
detail::tuple_append(m_arguments, std::move(a)));
}
String m_header;
String m_footer;
Flags m_flags;
Subcommands m_subcommands;
Arguments m_arguments;
};
template <class OptionType> using command = basic_command<std::string, OptionType>;
template <class OptionType> using wcommand = basic_command<std::wstring, OptionType>;
template <class OptionType> using u16command = basic_command<std::u16string, OptionType>;
template <class OptionType> using u32command = basic_command<std::u32string, OptionType>;
namespace detail {
template <class OptionType, OptionType Option, class Value>
struct option_pair
{
std::shared_ptr<Value> value;
};
template <class Command>
struct to_option_map;
} // namespace detail
enum class argument_order
{
strict,
flexible
};
template <class Iterator, class Command>
inline typename detail::to_option_map<Command>::type
parse(Iterator, Iterator, Command const &, argument_order = argument_order::strict);
template <class OptionType, class ...Pairs>
class option_map : private Pairs...
{
private:
template <class Iterator, class Command>
friend typename detail::to_option_map<Command>::type
parse(Iterator, Iterator, Command const &, argument_order);
template <OptionType Option, class Value>
static auto &priv_value_impl(detail::option_pair<OptionType, Option, Value> &p)
{
return p.value;
}
template <OptionType Option, class Value>
static auto const &priv_value_impl(detail::option_pair<OptionType, Option, Value> const &p)
{
return p.value;
}
template <OptionType Option>
auto &priv_value() { return priv_value_impl<Option>(*this); }
template <OptionType Option>
auto const &priv_value() const { return priv_value_impl<Option>(*this); }
public:
using option_type = OptionType;
template <OptionType Option>
using type = typename std::remove_reference_t<
decltype(priv_value_impl<Option>(std::declval<option_map &>()))>::element_type;
template <OptionType Option>
bool has() const { return static_cast<bool>(priv_value<Option>()); }
template <OptionType Option>
auto get() const { return *priv_value<Option>(); }
template <OptionType Option>
auto get_shared() const
{
auto const &v = priv_value<Option>();
return v ? detail::copy_shared(*v) : nullptr;
}
#ifdef NONSUGAR_CXX17
template <OptionType Option>
auto get_optional() const
{
auto const &v = priv_value<Option>();
return v ? std::make_optional(*v) : std::nullopt;
}
#endif
};
namespace detail {
template <class>
struct to_option_pair;
template <class String, class OptionType, OptionType Option, class Value>
struct to_option_pair<flag<String, OptionType, Option, Value>>
{
using type = option_pair<OptionType, Option, Value>;
};
template <class String, class OptionType, OptionType Option, class Command>
struct to_option_pair<subcommand<String, OptionType, Option, Command>>
{
using type = option_pair<OptionType, Option, typename to_option_map<Command>::type>;
};
template <class String, class OptionType, OptionType Option, class Value>
struct to_option_pair<argument<String, OptionType, Option, Value>>
{
using type = option_pair<OptionType, Option, Value>;
};
template <class String, class OptionType, class ...Flags, class ...Subcommands, class ...Arguments>
struct to_option_map<basic_command<
String, OptionType,
std::tuple<Flags...>, std::tuple<Subcommands...>, std::tuple<Arguments...>>>
{
using type = option_map<
OptionType, typename to_option_pair<Flags>::type...,
typename to_option_pair<Subcommands>::type...,
typename to_option_pair<Arguments>::type...>;
};
struct eat
{
template <class ...Args>
constexpr eat(Args &&...) {}
};
template <class T, class F, std::size_t ...I>
inline void tuple_for_each_impl(T &&t, F &&f, std::index_sequence<I...>)
{
eat { (f(std::get<I>(t)), void(), 0)... };
}
template <class T, class F>
inline void tuple_for_each(T &&t, F &&f)
{
tuple_for_each_impl(
std::forward<T>(t), std::forward<F>(f),
std::make_index_sequence<std::tuple_size<std::remove_reference_t<T>>::value>());
}
template <class String, class Value, class = void>
struct parse_flag_long
{
template <class Iterator, class Flag>
void operator()(
std::match_results<typename String::const_iterator> const &match,
Iterator &arg_it, Iterator arg_last,
std::shared_ptr<Value> &value, Flag const &flg, String &err) const
{
String s;
if (!match[2].matched) {
++arg_it;
if (arg_it == arg_last) {
err = widen<String>("argument required: ") + match.str(0);
return;
}
s = *arg_it;
} else {
s = match.str(2);
}
auto const v = flg.read(s);
if (!v) {
err = widen<String>("invalid argument: --") + match.str(1) + widen<String>("=") + s;
return;
}
value = v;
}
};
template <class String>
struct parse_flag_long<String, void>
{
template <class Iterator, class Flag>
void operator()(
std::match_results<typename String::const_iterator> const &match, Iterator &, Iterator,
std::shared_ptr<void> &value, Flag const &, String &err) const
{
if (match[2].matched) {
err = widen<String>("argument not allowed: ") + match.str(0);
return;
}
value = std::make_shared<int>();
}
};
template <class String, class Value>
struct parse_flag_long<String, Value, std::enable_if_t<is_optional<Value>::value>>
{
template <class Iterator, class Flag>
void operator()(
std::match_results<typename String::const_iterator> const &match, Iterator &, Iterator,
std::shared_ptr<Value> &value, Flag const &flg, String &err) const
{
if (!match[2].matched) {
value = std::make_shared<Value>();
} else {
auto const v = flg.read(match.str(2));
if (!v) {
err = widen<String>("invalid argument: ") + match.str(0);
return;
}
value = copy_shared(optional_traits<Value>::construct(*v));
}
}
};
template <class String, class Value>
struct parse_flag_long<
String, Value, std::enable_if_t<is_container_and_not_string<String, Value>::value>>
{
template <class Iterator, class Flag>
void operator()(
std::match_results<typename String::const_iterator> const &match,
Iterator &arg_it, Iterator arg_last,
std::shared_ptr<Value> &value, Flag const &flg, String &err) const
{
std::shared_ptr<typename container_traits<Value>::value_type> v;
parse_flag_long<String, typename container_traits<Value>::value_type>()(
match, arg_it, arg_last, v, flg, err);
if (!err.empty()) return;
if (!value) value = std::make_shared<Value>();
container_traits<Value>::push_back(*value, *v);
}
};
template <class String, class Value, class = void>
struct parse_flag_short
{
template <class NameIterator, class Iterator, class Flag>
void operator()(
NameIterator &it, NameIterator last, Iterator &arg_it, Iterator arg_last,
std::shared_ptr<Value> &value, Flag const &flg, String &err) const
{
auto const name = *it;
String s;
if (std::next(it) == last) {
++arg_it;
if (arg_it == arg_last) {
err = widen<String>("argument required: -") + name;
return;
}
s = *arg_it;
} else {
s = String(std::next(it), last);
it = std::prev(last);
}
auto const v = flg.read(s);
if (!v) {
err = widen<String>("invalid argument: -") + name + widen<String>(" ") + s;
return;
}
value = v;
}
};
template <class String>
struct parse_flag_short<String, void>
{
template <class NameIterator, class Iterator, class Flag>
void operator()(
NameIterator &, NameIterator, Iterator &, Iterator,
std::shared_ptr<void> &value, Flag const &, String &) const
{
value = std::make_shared<int>();
}
};
template <class String, class Value>
struct parse_flag_short<String, Value, std::enable_if_t<is_optional<Value>::value>>
{
template <class NameIterator, class Iterator, class Flag>
void operator()(
NameIterator &it, NameIterator last, Iterator &, Iterator,
std::shared_ptr<Value> &value, Flag const &flg, String &err) const
{
auto const name = *it;
if (std::next(it) == last) {
value = std::make_shared<Value>();
} else {
auto const s = String(std::next(it), last);
it = std::prev(last);
auto const v = flg.read(s);
if (!v) {
err = widen<String>("invalid argument: -") + name + widen<String>(" ") + s;
return;
}
value = copy_shared(optional_traits<Value>::construct(*v));
}
}
};
template <class String, class Value>
struct parse_flag_short<
String, Value, std::enable_if_t<is_container_and_not_string<String, Value>::value>>
{
template <class NameIterator, class Iterator, class Flag>
void operator()(
NameIterator &it, NameIterator last, Iterator &arg_it, Iterator arg_last,
std::shared_ptr<Value> &value, Flag const &flg, String &err) const
{
std::shared_ptr<typename container_traits<Value>::value_type> v;
parse_flag_short<String, typename container_traits<Value>::value_type>()(
it, last, arg_it, arg_last, v, flg, err);
if (!err.empty()) return;
if (!value) value = std::make_shared<Value>();
container_traits<Value>::push_back(*value, *v);
}
};
template <class String, class Value, class = void>
struct parse_argument
{
template <class Iterator, class Argument>
void operator()(
Iterator &it, Iterator last, std::shared_ptr<Value> &value, Argument const &arg,
String &err) const
{
if (it == last) {
err = widen<String>("argument required: ") + arg.placeholder;
return;
}
auto const v = arg.read(*it);
if (!v) {
err = widen<String>("invalid argument: ") + arg.placeholder + widen<String>("=") + *it;
return;
}
++it;
value = v;
}
};
template <class String, class Value>
struct parse_argument<String, Value, std::enable_if_t<is_optional<Value>::value>>
{
template <class Iterator, class Argument>
void operator()(
Iterator &it, Iterator last, std::shared_ptr<Value> &value, Argument const &arg,
String &err) const
{
if (it == last) {
value = std::make_shared<Value>();
} else {
auto const v = arg.read(*it);
if (!v) {
err = widen<String>("invalid argument: ") + arg.placeholder +
widen<String>("=") + *it;
return;
}
++it;
value = copy_shared(optional_traits<Value>::construct(*v));
}
}
};
template <class String, class Value>
struct parse_argument<
String, Value, std::enable_if_t<is_container_and_not_string<String, Value>::value>>
{
template <class Iterator, class Argument>
void operator()(
Iterator &it, Iterator last, std::shared_ptr<Value> &value, Argument const &arg,
String &err) const
{
value = std::make_shared<Value>();
while (it != last) {
std::shared_ptr<typename container_traits<Value>::value_type> v;
parse_argument<String, typename container_traits<Value>::value_type>()(
it, last, v, arg, err);
if (!err.empty()) return;
container_traits<Value>::push_back(*value, *v);
}
}
};
template <class T>
using remove_cvr_t = std::remove_cv_t<std::remove_reference_t<T>>;
template <class String>
inline bool starts_with(String const &s1, String const &s2)
{
return s1.size() >= s2.size() && std::equal(s2.begin(), s2.end(), s1.begin());
}
template <class Container>
inline bool contains(Container const &c, typename Container::value_type v)
{
return std::find(c.begin(), c.end(), v) != c.end();
}
} // namespace detail
template <class Iterator, class Command>
inline typename detail::to_option_map<Command>::type parse(
Iterator begin, Iterator end, Command const &command, argument_order order)
{
using string_type = typename Command::string_type;
using error_type = basic_error<string_type>;
using regex_type = std::basic_regex<typename string_type::value_type>;
auto const &_ = detail::widen<string_type>;
typename detail::to_option_map<Command>::type opts;
detail::tuple_for_each(command.m_flags, [&](auto const &flg)
{
constexpr auto option = detail::remove_cvr_t<decltype(flg)>::option();
opts.template priv_value<option>() = flg.default_value;
});
std::vector<string_type> const args(begin, end);
auto arg_it = args.begin();
auto const arg_last = args.end();
bool exclusive = false;
constexpr auto subcommand_required =
std::tuple_size<typename Command::subcommand_tuple_type>::value != 0;
std::shared_ptr<string_type> subcommand_arg;
std::vector<string_type> argument_args;
for (; arg_it != arg_last; ++arg_it) {
if (*arg_it == _("--")) {
argument_args.insert(argument_args.end(), std::next(arg_it), arg_last);
break;
}
std::match_results<typename string_type::const_iterator> match;
if (std::regex_match(*arg_it, match, regex_type(_("--([^=]+)(?:=(.*))?")))) {
// long flag
auto const name = match.str(1);
bool exact = false;
std::vector<string_type> partial_matches;
detail::tuple_for_each(command.m_flags, [&](auto const &flg)
{
for (auto const &long_name : flg.long_names) {
if (name == long_name) {
if (std::exchange(exact, true)) {
throw error_type(
command.m_header + _(": ambiguous option: --") + name);
}
} else if (detail::starts_with(long_name, name)) {
partial_matches.push_back(long_name);
}
}
});
if (!exact) {
if (partial_matches.empty()) {
throw error_type(command.m_header + _(": unrecognized option: --") + name);
} else if (partial_matches.size() >= 2) {
detail::sstream<string_type> ss;
ss << command.m_header << ": ambiguous option: --" << name << " [";
bool first = true;
for (auto const &long_name : partial_matches) {
if (!std::exchange(first, false)) ss << ", ";
ss << "--" << long_name;
}
ss << "]";
throw error_type(ss.str());
}
}
detail::tuple_for_each(command.m_flags, [&](auto const &flg)
{
using flag_type = detail::remove_cvr_t<decltype(flg)>;
using value_type = typename flag_type::value_type;
constexpr auto option = flag_type::option();
for (auto const &long_name : flg.long_names) {
if (exact ? name == long_name : detail::starts_with(long_name, name)) {
string_type err;
detail::parse_flag_long<string_type, value_type>()(
match, arg_it, arg_last,
opts.template priv_value<option>(), flg, err);
if (!err.empty()) {
throw error_type(command.m_header + _(": ") + err);
}
if (flg.exclusive) exclusive = true;
}
}
});
} else if (std::regex_match(*arg_it, match, regex_type(_("-(.+)")))) {
// short flag
for (auto it = match[1].first; it != match[1].second; ++it) {
auto const name = *it;
bool exact = false;
detail::tuple_for_each(command.m_flags, [&](auto const &flg)
{
if (detail::contains(flg.short_names, name)) {
if (std::exchange(exact, true)) {
throw error_type(
command.m_header + _(": ambiguous option: -") + name);
}
}
});
if (!exact) {
throw error_type(command.m_header + _(": unrecognized option: -") + name);
}
detail::tuple_for_each(command.m_flags, [&](auto const &flg)
{
using flag_type = detail::remove_cvr_t<decltype(flg)>;
using value_type = typename flag_type::value_type;
constexpr auto option = flag_type::option();
if (detail::contains(flg.short_names, name)) {
string_type err;
detail::parse_flag_short<string_type, value_type>()(
it, match[1].second, arg_it, arg_last,
opts.template priv_value<option>(), flg, err);
if (!err.empty()) {
throw error_type(command.m_header + _(": ") + err);
}
if (flg.exclusive) exclusive = true;
}
});
}
} else if (subcommand_required && !subcommand_arg) {
// subcommand and arguments
subcommand_arg = detail::copy_shared(*arg_it);
argument_args.insert(argument_args.end(), std::next(arg_it), arg_last);
break;
} else if (order == argument_order::flexible) {
// argument
argument_args.push_back(*arg_it);
} else {
// arguments
argument_args.insert(argument_args.end(), arg_it, arg_last);
break;
}
}
if (exclusive) return opts;
auto argument_it = argument_args.begin();
auto const argument_last = argument_args.end();
if (subcommand_required) {
if (!subcommand_arg) {
throw error_type(command.m_header + _(": command required"));
}
bool exact = false;
detail::tuple_for_each(command.m_subcommands, [&](auto const &subcmd)
{
if (subcmd.name == *subcommand_arg) {
if (std::exchange(exact, true)) {
throw error_type(
command.m_header + _(": ambiguous command: ") + *subcommand_arg);
}
}
});
if (!exact) {
throw error_type(command.m_header + _(": unrecognized command: ") + *subcommand_arg);
}
detail::tuple_for_each(command.m_subcommands, [&](auto const &subcmd)
{
if (subcmd.name == *subcommand_arg) {
using subcommand_type = std::remove_cv_t<
std::remove_reference_t<decltype(subcmd)>>;
constexpr auto option = subcommand_type::option();
opts.template priv_value<option>() = detail::copy_shared(
parse(argument_it, argument_last, subcmd.command, order));
}
});
return opts;
}
detail::tuple_for_each(command.m_arguments, [&](auto const &arg)
{
using argument_type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
using value_type = typename argument_type::value_type;
constexpr auto option = argument_type::option();
string_type err;
detail::parse_argument<string_type, value_type>()(
argument_it, argument_last, opts.template priv_value<option>(), arg, err);
if (!err.empty()) {
throw error_type(command.m_header + _(": ") + err);
}
});
if (argument_it != argument_last) {
throw error_type(command.m_header + _(": unrecognized argument: ") + *argument_it);
}
return opts;
}
template <class Char, class Command>
inline auto parse(
int argc, Char * const *argv, Command const &command,
argument_order order = argument_order::strict)
{
return parse(argv + (argc > 0 ? 1 : 0), argv + argc, command, order);
}