-
Notifications
You must be signed in to change notification settings - Fork 0
/
argparser.hpp
1276 lines (1216 loc) · 37.2 KB
/
argparser.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
#ifndef ARG_PARSER_ALL_H
#define ARG_PARSER_ALL_H
#include <string>
#ifndef ARG_PARSER_H
#define ARG_PARSER_H
#include <cctype>
#include <iostream>
#include <list>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <unordered_map>
#include <vector>
#ifndef ARG_PARSER_COMMON_H
#define ARG_PARSER_COMMON_H
#include <cctype>
#include <string>
namespace argparser
{
namespace flag
{
inline static bool is_full_flag(const std::string &name)
{
return name.size() >= 3 && name[0] == '-' && name[1] == '-' &&
isalpha(name[2]);
}
inline static bool is_short_flag(const std::string &name)
{
return name.size() >= 2 && name[0] == '-' && isalpha(name[1]);
}
inline static bool is_flag(const std::string &str)
{
return is_full_flag(str) || is_short_flag(str);
}
std::vector<std::string> split(std::string str,
const std::string& delimiter)
{
std::vector<std::string> ret;
size_t pos = 0;
std::string token;
while ((pos = str.find(delimiter)) != std::string::npos)
{
token = str.substr(0, pos);
ret.emplace_back(std::move(token));
str.erase(0, pos + delimiter.length());
}
ret.push_back(std::move(str));
return ret;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
os << "[";
for (size_t i = 0; i < vec.size(); ++i)
{
os << vec[i];
if (i != vec.size() - 1)
{
os << ", ";
}
}
os << "]";
return os;
}
std::ostream& operator<<(std::ostream& os, const std::vector<std::string>& vec)
{
os << "[";
for (size_t i = 0; i < vec.size(); ++i)
{
os << "\"" << vec[i] << "\"";
if (i != vec.size() - 1)
{
os << ", ";
}
}
os << "]";
return os;
}
} // namespace flag
} // namespace argparser
#endif
#ifndef DEBUG_H
#define DEBUG_H
#include <ostream>
#include <list>
#include <string>
std::ostream& operator<<(std::ostream& os, std::list<std::pair<std::string, std::string>> pairs)
{
if (!pairs.empty())
{
os << "[";
for (const auto&[first, second]: pairs)
{
os << "{" << first << ", " << second << "}, ";
}
os << "]" << std::endl;
}
else
{
os << "[]" << std::endl;
}
return os;
}
#endif
#ifndef ARG_PARSER_MANAGER_H
#define ARG_PARSER_MANAGER_H
#include <iostream>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#ifndef ARG_PARSER_FLAG_H
#define ARG_PARSER_FLAG_H
#include <iostream>
#include <istream>
#include <string>
#ifndef CONVERT_H
#define CONVERT_H
#include <iostream>
#include <optional>
namespace argparse
{
namespace convert
{
template <typename T>
bool apply_to(T *target, const std::string &value);
template <typename T>
std::optional<T> try_to(const std::string &input)
{
T tmp;
if (!apply_to<T>(&tmp, input))
{
return {};
}
return tmp;
}
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec)
{
std::string token;
vec.clear();
if (is.peek() == EOF)
{
// parse empty string "" to an array
// success and the result is an empty array
return is;
}
while (!is.eof())
{
std::getline(is, token, ',');
T tmp;
bool succ = apply_to<T>(&tmp, token);
if (!succ)
{
// manually set fail bit
is.setstate(std::ios_base::failbit);
return is;
}
vec.emplace_back(std::move(tmp));
}
return is;
}
template <typename T>
bool apply_to(T *target, const std::string &value)
{
// A space is never allowed except that T is std::string.
if (value.find(' ') != std::string::npos)
{
return false;
}
std::istringstream iss(value);
iss >> *target;
return iss.eof() && !iss.fail();
}
template <>
bool apply_to<bool>(bool *target, const std::string &value)
{
if (value.find(' ') != std::string::npos)
{
return false;
}
if (value.empty() || value == "1" || value == "true")
{
*target = true;
return true;
}
else if (value == "0" || value == "false")
{
*target = false;
return true;
}
return false;
}
template <>
bool apply_to<std::string>(std::string *target, const std::string &value)
{
*target = value;
return true;
}
} // namespace convert
} // namespace argparse
#endif
namespace argparser
{
namespace flag
{
class FlagStore;
class Flag
{
public:
using Pointer = std::shared_ptr<Flag>;
Flag(const std::string &full_name,
const std::string &short_name,
const std::string &desc)
: full_name_(full_name), short_name_(short_name), desc_(desc)
{
}
virtual ~Flag() = default;
virtual bool apply(const std::string &value) = 0;
virtual std::string short_name() const
{
return short_name_;
}
virtual std::string full_name() const
{
return full_name_;
}
virtual std::string desc() const
{
return desc_;
}
virtual bool match(const std::string &key) const
{
// never match an empty string.
if (key.empty())
{
return false;
}
return key == full_name_ || key == short_name_;
}
virtual std::string to_string() const
{
return "{Flag " + full_name_ + ", " + short_name_ + "}";
}
private:
std::string full_name_;
std::string short_name_;
std::string desc_;
};
template <typename T>
class ConcreteFlag : public Flag
{
public:
ConcreteFlag(T *flag,
const std::string &full_name,
const std::string &short_name,
const std::string &desc)
: Flag(full_name, short_name, desc), flag_(flag)
{
}
static std::shared_ptr<ConcreteFlag<T>> make_flag(
T *flag,
const std::string &full_name,
const std::string &short_name,
const std::string &desc)
{
return std::make_shared<ConcreteFlag<T>>(
flag, full_name, short_name, desc);
}
bool apply(const std::string &value) override
{
std::optional<T> maybe = argparse::convert::try_to<T>(value);
if (maybe.has_value())
{
*flag_ = std::move(maybe.value());
return true;
}
return false;
}
~ConcreteFlag() = default;
protected:
T *flag_;
};
class AllocatedFlag : public Flag
{
public:
AllocatedFlag(const std::string &full_name,
const std::string &short_name,
const std::string &desc)
: Flag(full_name, short_name, desc)
{
}
static std::shared_ptr<AllocatedFlag> make_flag(
const std::string &full_name,
const std::string &short_name,
const std::string &desc)
{
return std::make_shared<AllocatedFlag>(full_name, short_name, desc);
}
template <typename T>
T to() const
{
std::optional<T> maybe = argparse::convert::try_to<T>(inner_);
if (maybe.has_value())
{
return maybe.value();
}
std::cerr << __FILE__ << ":" << __LINE__ << " Failed to convert \""
<< inner_ << "\" to type " << typeid(T).name() << std::endl;
std::terminate();
}
template <typename T>
bool convertable_to() const
{
std::optional<T> maybe = argparse::convert::try_to<T>(inner_);
return maybe.has_value();
}
const std::string &inner() const
{
return inner_;
}
private:
friend FlagStore;
bool apply(const std::string &value) override
{
inner_ = value;
return true;
}
std::string inner_;
};
template <>
std::string AllocatedFlag::to<std::string>() const
{
return inner_;
}
template <>
bool AllocatedFlag::convertable_to<std::string>() const
{
return !inner_.empty();
}
} // namespace flag
} // namespace argparser
#endif
namespace argparser
{
namespace flag
{
class FlagStore
{
using Required = bool;
using Applied = bool;
using Meta = std::pair<Required, Applied>;
public:
using Pointer = std::shared_ptr<FlagStore>;
static Pointer new_instance()
{
return std::make_unique<FlagStore>();
}
template <typename T>
bool add_flag(T *slot,
const std::string &full_name,
const std::string &short_name,
const std::string &desc,
const std::optional<std::string> &default_val,
bool required)
{
auto pair = Meta(required, false);
flags_.emplace_back(
flag::ConcreteFlag<T>::make_flag(slot, full_name, short_name, desc),
std::move(pair));
if (default_val.has_value())
{
auto &flag = flags_.back().first;
if (!flag->apply(default_val.value()))
{
std::cerr << "Failed to register flag " << full_name << ": "
<< "default value \"" << default_val.value()
<< "\" not parsable" << std::endl;
// pop the error one
flags_.pop_back();
return false;
}
}
max_full_name_len_ = std::max(max_full_name_len_, full_name.size());
max_short_name_len_ = std::max(max_short_name_len_, short_name.size());
return true;
}
bool add_flag(const std::string &full_name,
const std::string &short_name,
const std::string &desc,
const std::optional<std::string> &default_val,
bool required)
{
auto meta = Meta(required, false);
allocated_flags_.emplace_back(
flag::AllocatedFlag(full_name, short_name, desc), std::move(meta));
if (default_val.has_value())
{
auto &allocated_flag = allocated_flags_.back().first;
if (!allocated_flag.apply(default_val.value()))
{
std::cerr << "Failed to register flag " << full_name << ": "
<< "default value \"" << default_val.value()
<< "\" not parsable" << std::endl;
// pop the error one
allocated_flags_.pop_back();
return false;
}
}
max_full_name_len_ = std::max(max_full_name_len_, full_name.size());
max_short_name_len_ = std::max(max_short_name_len_, short_name.size());
return true;
}
bool empty() const
{
return flags_.empty() && allocated_flags_.empty();
}
bool apply(const std::string &key, const std::string &value)
{
for (auto &[flag, meta] : flags_)
{
bool &applied = meta.second;
if (flag->match(key))
{
if (applied)
{
std::cerr << "Failed to apply " << key << "=\"" << value
<< "\": "
<< "Flag " << key
<< " already set and is provided more than once."
<< std::endl;
return false;
}
if (!flag->apply(value))
{
std::cerr << "Failed to apply " << key << "=\"" << value
<< "\": \"" << value << "\" not parsable"
<< std::endl;
return false;
}
applied = true;
return true;
}
}
for (auto &[flag, meta] : allocated_flags_)
{
bool &applied = meta.second;
if (flag.match(key))
{
if (applied)
{
std::cerr << "Failed to apply " << key << "=\"" << value
<< "\": "
<< "Flag " << key
<< " already set and is provided more than once."
<< std::endl;
return false;
}
if (!flag.apply(value))
{
std::cerr << "Failed to apply " << key << "=\"" << value
<< "\": \"" << value << "\" not parsable"
<< std::endl;
return false;
}
applied = true;
return true;
}
}
return false;
}
bool contain(const std::string name) const
{
for (const auto &flagline : flags_)
{
if (flagline.first->match(name))
{
return true;
}
}
for (const auto &flagline : allocated_flags_)
{
if (flagline.first.match(name))
{
return true;
}
}
return false;
}
const flag::AllocatedFlag &get(const std::string &name) const
{
for (const auto &[flag, meta] : allocated_flags_)
{
std::ignore = meta;
if (flag.match(name))
{
return flag;
}
}
std::cerr << "Failed to get " << name << ": not found." << std::endl;
std::terminate();
}
bool has(const std::string &name) const
{
for (const auto &[flag, meta] : allocated_flags_)
{
std::ignore = meta;
if (flag.match(name))
{
return true;
}
}
return false;
}
size_t size() const
{
return flags_.size() + allocated_flags_.size();
}
using FlagId = std::tuple<std::string, std::string>;
std::vector<FlagId> missing_keys() const
{
std::vector<FlagId> ret;
for (const auto &[flag, meta] : flags_)
{
const bool &required = meta.first;
const bool &applied = meta.second;
if (required && !applied)
{
ret.emplace_back(flag->full_name(), flag->short_name());
}
}
for (const auto &[flag, meta] : allocated_flags_)
{
const bool &required = meta.first;
const bool &applied = meta.second;
if (required && !applied)
{
ret.emplace_back(flag.full_name(), flag.short_name());
}
}
return ret;
}
// TODO: make it formator
void print_flags(const std::string &title = "Flags") const
{
if (empty())
{
return;
}
std::cout << title << ":" << std::endl;
for (const auto &[flag, meta] : flags_)
{
std::ignore = meta;
auto short_name = flag->short_name();
auto full_name = flag->full_name();
std::cout << " ";
std::cout << std::string(max_short_name_len_ - short_name.size(),
' ');
std::cout << short_name << (short_name.empty() ? " " : ", ");
std::cout << full_name;
std::cout << std::endl;
size_t padding_len =
2 + max_short_name_len_ + 2 + max_full_name_len_ + 2;
auto padding_str = std::string(padding_len, ' ');
std::cout << padding_str;
auto desc = flag->desc();
size_t pos = 0;
std::string token;
size_t current_column = 0;
while ((pos = desc.find(" ")) != std::string::npos)
{
token = desc.substr(0, pos);
current_column += token.size();
if (current_column >= 80)
{
current_column = 0;
std::cout << std::endl << padding_str;
}
std::cout << token << " ";
desc.erase(0, pos + 1);
}
std::cout << desc;
std::cout << std::endl << std::endl;
}
for (const auto &[flag, meta] : allocated_flags_)
{
std::ignore = meta;
auto short_name = flag.short_name();
auto full_name = flag.full_name();
std::cout << " ";
std::cout << std::string(max_short_name_len_ - short_name.size(),
' ');
std::cout << short_name << (short_name.empty() ? " " : ", ");
std::cout << full_name;
std::cout << std::endl;
size_t padding_len =
2 + max_short_name_len_ + 2 + max_full_name_len_ + 2;
auto padding_str = std::string(padding_len, ' ');
std::cout << padding_str;
auto desc = flag.desc();
size_t pos = 0;
std::string token;
size_t current_column = 0;
while ((pos = desc.find(" ")) != std::string::npos)
{
token = desc.substr(0, pos);
current_column += token.size();
if (current_column >= 80)
{
current_column = 0;
std::cout << std::endl << padding_str;
}
std::cout << token << " ";
desc.erase(0, pos + 1);
}
std::cout << desc;
std::cout << std::endl;
}
std::cout << std::endl;
}
FlagStore() = default;
~FlagStore() = default;
private:
// FlagLine = {pointer, {bool, bool}}
using FlagLine = std::pair<flag::Flag::Pointer, Meta>;
using AllocatedFlagLine = std::pair<flag::AllocatedFlag, Meta>;
std::vector<FlagLine> flags_;
std::vector<AllocatedFlagLine> allocated_flags_;
std::vector<bool> required_;
std::vector<bool> applied_;
size_t max_full_name_len_{0};
size_t max_short_name_len_{0};
};
} // namespace flag
} // namespace argparser
#endif
#ifndef FLAG_VALIDATOR_H
#define FLAG_VALIDATOR_H
#include <iostream>
#include <set>
#include <string>
namespace argparser
{
namespace flag
{
class Validator
{
public:
Validator(flag::FlagStore::Pointer gf_store) : gf_store_(gf_store)
{
}
bool validate(const std::string &full_name, const std::string &short_name)
{
if (!flag::is_full_flag(full_name) && !flag::is_short_flag(short_name))
{
std::cerr << "Failed to register flag " << full_name << ", "
<< short_name << ": Both flag formats are not allowed"
<< std::endl;
return false;
}
if (!full_name.empty() && !flag::is_full_flag(full_name))
{
std::cerr << "Failed to register flag " << full_name
<< ": identity not allowed" << std::endl;
return false;
}
if (!short_name.empty() && !flag::is_short_flag(short_name))
{
std::cerr << "Failed to register flag " << short_name << "("
<< full_name << ")"
<< ": identity not allowed" << std::endl;
return false;
}
if (!unique_full_flag(full_name))
{
std::cerr << "Failed to register flag " << full_name
<< ": flag already registered" << std::endl;
return false;
}
if (!unique_short_flag(short_name))
{
std::cerr << "Failed to register flag " << short_name << "("
<< full_name << ")"
<< ": flag already registered" << std::endl;
return false;
}
if (gf_store_->contain(full_name) || gf_store_->contain(short_name))
{
std::cerr << "Flag registered failed: flag \"" << full_name
<< "\", \"" << short_name
<< "\" conflict with global flag" << std::endl;
return false;
}
if (!full_name.empty())
{
inserted_full_names_.insert(full_name);
}
if (!short_name.empty())
{
inserted_short_names_.insert(short_name);
}
return true;
}
bool unique_full_flag(const std::string &name) const
{
return inserted_full_names_.find(name) == inserted_full_names_.end();
}
bool unique_short_flag(const std::string &name) const
{
return inserted_short_names_.find(name) == inserted_short_names_.end();
}
private:
std::set<std::string> inserted_full_names_;
std::set<std::string> inserted_short_names_;
flag::FlagStore::Pointer gf_store_;
};
} // namespace flag
} // namespace argparser
#endif
namespace argparser
{
class Parser;
class ParserStore
{
public:
using Pointer = std::unique_ptr<ParserStore>;
ParserStore() = default;
~ParserStore() = default;
ParserStore(const ParserStore &) = delete;
ParserStore(ParserStore &&) = delete;
ParserStore &operator=(const ParserStore &) = delete;
ParserStore &operator=(ParserStore &&) = delete;
const flag::AllocatedFlag &get(const std::string &name) const
{
if (flag_store_->has(name))
{
return flag_store_->get(name);
}
return gf_store_->get(name);
}
bool has(const std::string &name) const
{
return flag_store_->has(name) || gf_store_->has(name);
}
private:
friend Parser;
void link_flag_store(flag::FlagStore::Pointer p)
{
flag_store_ = p;
}
void link_global_flag_store(flag::FlagStore::Pointer p)
{
gf_store_ = p;
}
flag::FlagStore::Pointer flag_store_;
flag::FlagStore::Pointer gf_store_;
};
class Parser
{
public:
using Pointer = std::unique_ptr<Parser>;
using FlagPair = std::pair<std::string, std::string>;
using FlagPairs = std::list<FlagPair>;
Parser(std::shared_ptr<flag::FlagStore> global_flag_store,
const char *description)
: description_(description),
flag_store_(flag::FlagStore::new_instance()),
gf_store_(global_flag_store),
validator_(gf_store_),
store_(std::make_unique<ParserStore>())
{
store_->link_global_flag_store(gf_store_);
store_->link_flag_store(flag_store_);
}
Parser(const Parser &) = delete;
Parser(Parser &&) = delete;
Parser &operator=(const Parser &) = delete;
Parser &operator=(Parser &&) = delete;
void print_promt() const
{
if (!init_)
{
std::cerr << "ERR: parse() not called." << std::endl;
return;
}
std::cout << description_ << std::endl << std::endl;
print_usage();
print_command();
flag_store_->print_flags();
gf_store_->print_flags("Global Flag");
}
void print_promt(int argc, const char *argv[]) const
{
auto pairs = retrieve(argc, argv);
return print_promt(pairs);
}
std::string desc() const
{
return description_;
}
Parser &command(const std::string &command, const char *desc = "")
{
sub_parsers_.emplace(command,
std::make_unique<Parser>(gf_store_, desc));
max_command_len_ = std::max(max_command_len_, command.size());
return *sub_parsers_[command];
}
// TODO: make default has type?
template <typename T>
bool flag(T *flag,
const char *full_name,
const char *short_name,
const char *desc,
const char *default_val)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return flag_store_->add_flag(
flag, full_name, short_name, desc, default_val, false);
}
template <typename T>
bool flag(T *flag,
const char *full_name,
const char *short_name,
const char *desc)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return flag_store_->add_flag(
flag, full_name, short_name, desc, std::nullopt, true);
}
/**
* This function register flag to the internal FlagStore.
* The user can later retrieve the flag via
* int result = FlagStore::instance().get("--flag").to<int>();
*/
bool flag(const char *full_name, const char *short_name, const char *desc)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return flag_store_->add_flag(
full_name, short_name, desc, std::nullopt, true);
}
bool flag(const char *full_name,
const char *short_name,
const char *desc,
const char *default_val)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return flag_store_->add_flag(
full_name, short_name, desc, default_val, false);
}
template <typename T>
bool global_flag(T *flag,
const char *full_name,
const char *short_name,
const char *desc)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return gf_store_->add_flag(
flag, full_name, short_name, desc, std::nullopt, true);
}
template <typename T>
bool global_flag(T *flag,
const char *full_name,
const char *short_name,
const char *desc,
const char *default_val)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return gf_store_->add_flag(
flag, full_name, short_name, desc, default_val, false);
}
/**
* This function register flag to the internal FlagStore.
* The user can later retrieve the flag via
* int result = FlagStore::instance().get("--flag").to<int>();
*/
bool global_flag(const char *full_name,
const char *short_name,
const char *desc)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return gf_store_->add_flag(
full_name, short_name, desc, std::nullopt, true);
}
bool global_flag(const char *full_name,
const char *short_name,
const char *desc,
const char *default_val)
{
if (!validator_.validate(full_name, short_name))
{
return false;
}
return gf_store_->add_flag(
full_name, short_name, desc, default_val, false);
}
bool parse(int argc, const char *argv[])
{
command_path_.clear();
program_name = argv[0];
auto pairs = retrieve(argc, argv);
return do_parse(pairs, store_, command_path_);
}
std::vector<std::string> command_path() const
{
return command_path_;
}
const ParserStore &store() const
{
return *store_;
}
private:
bool init_{false};
std::string program_name;
std::string description_;
flag::FlagStore::Pointer flag_store_;
flag::FlagStore::Pointer gf_store_;
std::unordered_map<std::string, Pointer> sub_parsers_;
size_t max_command_len_{0};
flag::Validator validator_;
std::vector<std::string> command_path_;
ParserStore::Pointer store_;
void print_usage() const
{
if (!flag_store_->empty() || !sub_parsers_.empty())
{
std::cout << "Usage:" << std::endl;
}