-
Notifications
You must be signed in to change notification settings - Fork 3
/
cxxopts.hpp
2881 lines (2455 loc) · 57.3 KB
/
cxxopts.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) 2014-2022 Jarryd Beck
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.
*/
// vim: ts=2:sw=2:expandtab
#ifndef CXXOPTS_HPP_INCLUDED
#define CXXOPTS_HPP_INCLUDED
#include <cstring>
#include <exception>
#include <limits>
#include <initializer_list>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <algorithm>
#include <locale>
#ifdef CXXOPTS_NO_EXCEPTIONS
#include <iostream>
#endif
#if defined(__GNUC__) && !defined(__clang__)
# if (__GNUC__ * 10 + __GNUC_MINOR__) < 49
# define CXXOPTS_NO_REGEX true
# endif
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#define CXXOPTS_LINKONCE_CONST __declspec(selectany) extern
#define CXXOPTS_LINKONCE __declspec(selectany) extern
#else
#define CXXOPTS_LINKONCE_CONST
#define CXXOPTS_LINKONCE
#endif
#ifndef CXXOPTS_NO_REGEX
# include <regex>
#endif // CXXOPTS_NO_REGEX
// Nonstandard before C++17, which is coincidentally what we also need for <optional>
#ifdef __has_include
# if __has_include(<optional>)
# include <optional>
# ifdef __cpp_lib_optional
# define CXXOPTS_HAS_OPTIONAL
# endif
# endif
#endif
#define CXXOPTS_FALLTHROUGH
#ifdef __has_cpp_attribute
#if __has_cpp_attribute(fallthrough)
#undef CXXOPTS_FALLTHROUGH
#define CXXOPTS_FALLTHROUGH [[fallthrough]]
#endif
#endif
#if __cplusplus >= 201603L
#define CXXOPTS_NODISCARD [[nodiscard]]
#else
#define CXXOPTS_NODISCARD
#endif
#ifndef CXXOPTS_VECTOR_DELIMITER
#define CXXOPTS_VECTOR_DELIMITER ','
#endif
#define CXXOPTS__VERSION_MAJOR 3
#define CXXOPTS__VERSION_MINOR 1
#define CXXOPTS__VERSION_PATCH 1
#if (__GNUC__ < 10 || (__GNUC__ == 10 && __GNUC_MINOR__ < 1)) && __GNUC__ >= 6
#define CXXOPTS_NULL_DEREF_IGNORE
#endif
#if defined(__GNUC__)
#define DO_PRAGMA(x) _Pragma(#x)
#define CXXOPTS_DIAGNOSTIC_PUSH DO_PRAGMA(GCC diagnostic push)
#define CXXOPTS_DIAGNOSTIC_POP DO_PRAGMA(GCC diagnostic pop)
#define CXXOPTS_IGNORE_WARNING(x) DO_PRAGMA(GCC diagnostic ignored x)
#else
// define other compilers here if needed
#define CXXOPTS_DIAGNOSTIC_PUSH
#define CXXOPTS_DIAGNOSTIC_POP
#define CXXOPTS_IGNORE_WARNING(x)
#endif
#ifdef CXXOPTS_NO_RTTI
#define CXXOPTS_RTTI_CAST static_cast
#else
#define CXXOPTS_RTTI_CAST dynamic_cast
#endif
namespace cxxopts {
static constexpr struct {
uint8_t major, minor, patch;
} version = {
CXXOPTS__VERSION_MAJOR,
CXXOPTS__VERSION_MINOR,
CXXOPTS__VERSION_PATCH
};
} // namespace cxxopts
//when we ask cxxopts to use Unicode, help strings are processed using ICU,
//which results in the correct lengths being computed for strings when they
//are formatted for the help output
//it is necessary to make sure that <unicode/unistr.h> can be found by the
//compiler, and that icu-uc is linked in to the binary.
#ifdef CXXOPTS_USE_UNICODE
#include <unicode/unistr.h>
namespace cxxopts {
using String = icu::UnicodeString;
inline
String
toLocalString(std::string s)
{
return icu::UnicodeString::fromUTF8(std::move(s));
}
// GNU GCC with -Weffc++ will issue a warning regarding the upcoming class, we want to silence it:
// warning: base class 'class std::enable_shared_from_this<cxxopts::Value>' has accessible non-virtual destructor
CXXOPTS_DIAGNOSTIC_PUSH
CXXOPTS_IGNORE_WARNING("-Wnon-virtual-dtor")
// This will be ignored under other compilers like LLVM clang.
class UnicodeStringIterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = int32_t;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
UnicodeStringIterator(const icu::UnicodeString* string, int32_t pos)
: s(string)
, i(pos)
{
}
value_type
operator*() const
{
return s->char32At(i);
}
bool
operator==(const UnicodeStringIterator& rhs) const
{
return s == rhs.s && i == rhs.i;
}
bool
operator!=(const UnicodeStringIterator& rhs) const
{
return !(*this == rhs);
}
UnicodeStringIterator&
operator++()
{
++i;
return *this;
}
UnicodeStringIterator
operator+(int32_t v)
{
return UnicodeStringIterator(s, i + v);
}
private:
const icu::UnicodeString* s;
int32_t i;
};
CXXOPTS_DIAGNOSTIC_POP
inline
String&
stringAppend(String&s, String a)
{
return s.append(std::move(a));
}
inline
String&
stringAppend(String& s, std::size_t n, UChar32 c)
{
for (std::size_t i = 0; i != n; ++i)
{
s.append(c);
}
return s;
}
template <typename Iterator>
String&
stringAppend(String& s, Iterator begin, Iterator end)
{
while (begin != end)
{
s.append(*begin);
++begin;
}
return s;
}
inline
size_t
stringLength(const String& s)
{
return static_cast<size_t>(s.length());
}
inline
std::string
toUTF8String(const String& s)
{
std::string result;
s.toUTF8String(result);
return result;
}
inline
bool
empty(const String& s)
{
return s.isEmpty();
}
} // namespace cxxopts
namespace std {
inline
cxxopts::UnicodeStringIterator
begin(const icu::UnicodeString& s)
{
return cxxopts::UnicodeStringIterator(&s, 0);
}
inline
cxxopts::UnicodeStringIterator
end(const icu::UnicodeString& s)
{
return cxxopts::UnicodeStringIterator(&s, s.length());
}
} // namespace std
//ifdef CXXOPTS_USE_UNICODE
#else
namespace cxxopts {
using String = std::string;
template <typename T>
T
toLocalString(T&& t)
{
return std::forward<T>(t);
}
inline
std::size_t
stringLength(const String& s)
{
return s.length();
}
inline
String&
stringAppend(String&s, const String& a)
{
return s.append(a);
}
inline
String&
stringAppend(String& s, std::size_t n, char c)
{
return s.append(n, c);
}
template <typename Iterator>
String&
stringAppend(String& s, Iterator begin, Iterator end)
{
return s.append(begin, end);
}
template <typename T>
std::string
toUTF8String(T&& t)
{
return std::forward<T>(t);
}
inline
bool
empty(const std::string& s)
{
return s.empty();
}
} // namespace cxxopts
//ifdef CXXOPTS_USE_UNICODE
#endif
namespace cxxopts {
namespace {
CXXOPTS_LINKONCE_CONST std::string LQUOTE("\'");
CXXOPTS_LINKONCE_CONST std::string RQUOTE("\'");
} // namespace
// GNU GCC with -Weffc++ will issue a warning regarding the upcoming class, we
// want to silence it: warning: base class 'class
// std::enable_shared_from_this<cxxopts::Value>' has accessible non-virtual
// destructor This will be ignored under other compilers like LLVM clang.
CXXOPTS_DIAGNOSTIC_PUSH
CXXOPTS_IGNORE_WARNING("-Wnon-virtual-dtor")
// some older versions of GCC warn under this warning
CXXOPTS_IGNORE_WARNING("-Weffc++")
class Value : public std::enable_shared_from_this<Value>
{
public:
virtual ~Value() = default;
virtual
std::shared_ptr<Value>
clone() const = 0;
virtual void
add(const std::string& text) const = 0;
virtual void
parse(const std::string& text) const = 0;
virtual void
parse() const = 0;
virtual bool
has_default() const = 0;
virtual bool
is_container() const = 0;
virtual bool
has_implicit() const = 0;
virtual std::string
get_default_value() const = 0;
virtual std::string
get_implicit_value() const = 0;
virtual std::shared_ptr<Value>
default_value(const std::string& value) = 0;
virtual std::shared_ptr<Value>
implicit_value(const std::string& value) = 0;
virtual std::shared_ptr<Value>
no_implicit_value() = 0;
virtual bool
is_boolean() const = 0;
};
CXXOPTS_DIAGNOSTIC_POP
namespace exceptions {
class exception : public std::exception
{
public:
explicit exception(std::string message)
: m_message(std::move(message))
{
}
CXXOPTS_NODISCARD
const char*
what() const noexcept override
{
return m_message.c_str();
}
private:
std::string m_message;
};
class specification : public exception
{
public:
explicit specification(const std::string& message)
: exception(message)
{
}
};
class parsing : public exception
{
public:
explicit parsing(const std::string& message)
: exception(message)
{
}
};
class option_already_exists : public specification
{
public:
explicit option_already_exists(const std::string& option)
: specification("Option " + LQUOTE + option + RQUOTE + " already exists")
{
}
};
class invalid_option_format : public specification
{
public:
explicit invalid_option_format(const std::string& format)
: specification("Invalid option format " + LQUOTE + format + RQUOTE)
{
}
};
class invalid_option_syntax : public parsing {
public:
explicit invalid_option_syntax(const std::string& text)
: parsing("Argument " + LQUOTE + text + RQUOTE +
" starts with a - but has incorrect syntax")
{
}
};
class no_such_option : public parsing
{
public:
explicit no_such_option(const std::string& option)
: parsing("Option " + LQUOTE + option + RQUOTE + " does not exist")
{
}
};
class missing_argument : public parsing
{
public:
explicit missing_argument(const std::string& option)
: parsing(
"Option " + LQUOTE + option + RQUOTE + " is missing an argument"
)
{
}
};
class option_requires_argument : public parsing
{
public:
explicit option_requires_argument(const std::string& option)
: parsing(
"Option " + LQUOTE + option + RQUOTE + " requires an argument"
)
{
}
};
class gratuitous_argument_for_option : public parsing
{
public:
gratuitous_argument_for_option
(
const std::string& option,
const std::string& arg
)
: parsing(
"Option " + LQUOTE + option + RQUOTE +
" does not take an argument, but argument " +
LQUOTE + arg + RQUOTE + " given"
)
{
}
};
class requested_option_not_present : public parsing
{
public:
explicit requested_option_not_present(const std::string& option)
: parsing("Option " + LQUOTE + option + RQUOTE + " not present")
{
}
};
class option_has_no_value : public exception
{
public:
explicit option_has_no_value(const std::string& option)
: exception(
!option.empty() ?
("Option " + LQUOTE + option + RQUOTE + " has no value") :
"Option has no value")
{
}
};
class incorrect_argument_type : public parsing
{
public:
explicit incorrect_argument_type
(
const std::string& arg
)
: parsing(
"Argument " + LQUOTE + arg + RQUOTE + " failed to parse"
)
{
}
};
} // namespace exceptions
template <typename T>
void throw_or_mimic(const std::string& text)
{
static_assert(std::is_base_of<std::exception, T>::value,
"throw_or_mimic only works on std::exception and "
"deriving classes");
#ifndef CXXOPTS_NO_EXCEPTIONS
// If CXXOPTS_NO_EXCEPTIONS is not defined, just throw
throw T{text};
#else
// Otherwise manually instantiate the exception, print what() to stderr,
// and exit
T exception{text};
std::cerr << exception.what() << std::endl;
std::exit(EXIT_FAILURE);
#endif
}
using OptionNames = std::vector<std::string>;
namespace values {
namespace parser_tool {
struct IntegerDesc
{
std::string negative = "";
std::string base = "";
std::string value = "";
};
struct ArguDesc {
std::string arg_name = "";
bool grouping = false;
bool set_value = false;
std::string value = "";
};
#ifdef CXXOPTS_NO_REGEX
inline IntegerDesc SplitInteger(const std::string &text)
{
if (text.empty())
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
IntegerDesc desc;
const char *pdata = text.c_str();
if (*pdata == '-')
{
pdata += 1;
desc.negative = "-";
}
if (strncmp(pdata, "0x", 2) == 0)
{
pdata += 2;
desc.base = "0x";
}
if (*pdata != '\0')
{
desc.value = std::string(pdata);
}
else
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
return desc;
}
inline bool IsTrueText(const std::string &text)
{
const char *pdata = text.c_str();
if (*pdata == 't' || *pdata == 'T')
{
pdata += 1;
if (strncmp(pdata, "rue\0", 4) == 0)
{
return true;
}
}
else if (strncmp(pdata, "1\0", 2) == 0)
{
return true;
}
return false;
}
inline bool IsFalseText(const std::string &text)
{
const char *pdata = text.c_str();
if (*pdata == 'f' || *pdata == 'F')
{
pdata += 1;
if (strncmp(pdata, "alse\0", 5) == 0)
{
return true;
}
}
else if (strncmp(pdata, "0\0", 2) == 0)
{
return true;
}
return false;
}
inline OptionNames split_option_names(const std::string &text)
{
OptionNames split_names;
std::string::size_type token_start_pos = 0;
auto length = text.length();
if (length == 0)
{
throw_or_mimic<exceptions::invalid_option_format>(text);
}
while (token_start_pos < length) {
const auto &npos = std::string::npos;
auto next_non_space_pos = text.find_first_not_of(' ', token_start_pos);
if (next_non_space_pos == npos) {
throw_or_mimic<exceptions::invalid_option_format>(text);
}
token_start_pos = next_non_space_pos;
auto next_delimiter_pos = text.find(',', token_start_pos);
if (next_delimiter_pos == token_start_pos) {
throw_or_mimic<exceptions::invalid_option_format>(text);
}
if (next_delimiter_pos == npos) {
next_delimiter_pos = length;
}
auto token_length = next_delimiter_pos - token_start_pos;
// validate the token itself matches the regex /([:alnum:][-_[:alnum:]]*/
{
const char* option_name_valid_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"_-.?";
if (!std::isalnum(text[token_start_pos], std::locale::classic()) ||
text.find_first_not_of(option_name_valid_chars, token_start_pos) < next_delimiter_pos) {
throw_or_mimic<exceptions::invalid_option_format>(text);
}
}
split_names.emplace_back(text.substr(token_start_pos, token_length));
token_start_pos = next_delimiter_pos + 1;
}
return split_names;
}
inline ArguDesc ParseArgument(const char *arg, bool &matched)
{
ArguDesc argu_desc;
const char *pdata = arg;
matched = false;
if (strncmp(pdata, "--", 2) == 0)
{
pdata += 2;
if (isalnum(*pdata, std::locale::classic()))
{
argu_desc.arg_name.push_back(*pdata);
pdata += 1;
while (isalnum(*pdata, std::locale::classic()) || *pdata == '-' || *pdata == '_')
{
argu_desc.arg_name.push_back(*pdata);
pdata += 1;
}
if (argu_desc.arg_name.length() > 1)
{
if (*pdata == '=')
{
argu_desc.set_value = true;
pdata += 1;
if (*pdata != '\0')
{
argu_desc.value = std::string(pdata);
}
matched = true;
}
else if (*pdata == '\0')
{
matched = true;
}
}
}
}
else if (strncmp(pdata, "-", 1) == 0)
{
pdata += 1;
argu_desc.grouping = true;
while (isalnum(*pdata, std::locale::classic()))
{
argu_desc.arg_name.push_back(*pdata);
pdata += 1;
}
matched = !argu_desc.arg_name.empty() && *pdata == '\0';
}
return argu_desc;
}
#else // CXXOPTS_NO_REGEX
namespace {
CXXOPTS_LINKONCE
const char* const integer_pattern =
"(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)";
CXXOPTS_LINKONCE
const char* const truthy_pattern =
"(t|T)(rue)?|1";
CXXOPTS_LINKONCE
const char* const falsy_pattern =
"(f|F)(alse)?|0";
CXXOPTS_LINKONCE
const char* const option_pattern =
"--([[:alnum:]][-_[:alnum:]\\.]+)(=(.*))?|-([[:alnum:]].*)";
CXXOPTS_LINKONCE
const char* const option_specifier_pattern =
"([[:alnum:]][-_[:alnum:]\\.]*)(,[ ]*[[:alnum:]][-_[:alnum:]]*)*";
CXXOPTS_LINKONCE
const char* const option_specifier_separator_pattern = ", *";
} // namespace
inline IntegerDesc SplitInteger(const std::string &text)
{
static const std::basic_regex<char> integer_matcher(integer_pattern);
std::smatch match;
std::regex_match(text, match, integer_matcher);
if (match.length() == 0)
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
IntegerDesc desc;
desc.negative = match[1];
desc.base = match[2];
desc.value = match[3];
if (match.length(4) > 0)
{
desc.base = match[5];
desc.value = "0";
return desc;
}
return desc;
}
inline bool IsTrueText(const std::string &text)
{
static const std::basic_regex<char> truthy_matcher(truthy_pattern);
std::smatch result;
std::regex_match(text, result, truthy_matcher);
return !result.empty();
}
inline bool IsFalseText(const std::string &text)
{
static const std::basic_regex<char> falsy_matcher(falsy_pattern);
std::smatch result;
std::regex_match(text, result, falsy_matcher);
return !result.empty();
}
// Gets the option names specified via a single, comma-separated string,
// and returns the separate, space-discarded, non-empty names
// (without considering which or how many are single-character)
inline OptionNames split_option_names(const std::string &text)
{
static const std::basic_regex<char> option_specifier_matcher(option_specifier_pattern);
if (!std::regex_match(text.c_str(), option_specifier_matcher))
{
throw_or_mimic<exceptions::invalid_option_format>(text);
}
OptionNames split_names;
static const std::basic_regex<char> option_specifier_separator_matcher(option_specifier_separator_pattern);
constexpr int use_non_matches { -1 };
auto token_iterator = std::sregex_token_iterator(
text.begin(), text.end(), option_specifier_separator_matcher, use_non_matches);
std::copy(token_iterator, std::sregex_token_iterator(), std::back_inserter(split_names));
return split_names;
}
inline ArguDesc ParseArgument(const char *arg, bool &matched)
{
static const std::basic_regex<char> option_matcher(option_pattern);
std::match_results<const char*> result;
std::regex_match(arg, result, option_matcher);
matched = !result.empty();
ArguDesc argu_desc;
if (matched) {
argu_desc.arg_name = result[1].str();
argu_desc.set_value = result[2].length() > 0;
argu_desc.value = result[3].str();
if (result[4].length() > 0)
{
argu_desc.grouping = true;
argu_desc.arg_name = result[4].str();
}
}
return argu_desc;
}
#endif // CXXOPTS_NO_REGEX
#undef CXXOPTS_NO_REGEX
} // namespace parser_tool
namespace detail {
template <typename T, bool B>
struct SignedCheck;
template <typename T>
struct SignedCheck<T, true>
{
template <typename U>
void
operator()(bool negative, U u, const std::string& text)
{
if (negative)
{
if (u > static_cast<U>((std::numeric_limits<T>::min)()))
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
}
else
{
if (u > static_cast<U>((std::numeric_limits<T>::max)()))
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
}
}
};
template <typename T>
struct SignedCheck<T, false>
{
template <typename U>
void
operator()(bool, U, const std::string&) const {}
};
template <typename T, typename U>
void
check_signed_range(bool negative, U value, const std::string& text)
{
SignedCheck<T, std::numeric_limits<T>::is_signed>()(negative, value, text);
}
} // namespace detail
template <typename R, typename T>
void
checked_negate(R& r, T&& t, const std::string&, std::true_type)
{
// if we got to here, then `t` is a positive number that fits into
// `R`. So to avoid MSVC C4146, we first cast it to `R`.
// See https://github.com/jarro2783/cxxopts/issues/62 for more details.
r = static_cast<R>(-static_cast<R>(t-1)-1);
}
template <typename R, typename T>
void
checked_negate(R&, T&&, const std::string& text, std::false_type)
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
template <typename T>
void
integer_parser(const std::string& text, T& value)
{
parser_tool::IntegerDesc int_desc = parser_tool::SplitInteger(text);
using US = typename std::make_unsigned<T>::type;
constexpr bool is_signed = std::numeric_limits<T>::is_signed;
const bool negative = int_desc.negative.length() > 0;
const uint8_t base = int_desc.base.length() > 0 ? 16 : 10;
const std::string & value_match = int_desc.value;
US result = 0;
for (char ch : value_match)
{
US digit = 0;
if (ch >= '0' && ch <= '9')
{
digit = static_cast<US>(ch - '0');
}
else if (base == 16 && ch >= 'a' && ch <= 'f')
{
digit = static_cast<US>(ch - 'a' + 10);
}
else if (base == 16 && ch >= 'A' && ch <= 'F')
{
digit = static_cast<US>(ch - 'A' + 10);
}
else
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
const US next = static_cast<US>(result * base + digit);
if (result > next)
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
result = next;
}
detail::check_signed_range<T>(negative, result, text);
if (negative)
{
checked_negate<T>(value, result, text, std::integral_constant<bool, is_signed>());
}
else
{
value = static_cast<T>(result);
}
}
template <typename T>
void stringstream_parser(const std::string& text, T& value)
{
std::stringstream in(text);
in >> value;