forked from raspberrypi/picotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.h
1035 lines (912 loc) · 34.3 KB
/
cli.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
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _CLI_H
#define _CLI_H
#include <algorithm>
#include <exception>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <numeric>
#include <sstream>
#include <limits>
/**
* Note this is a hastily hacked together command line parser.
*
* I like the syntax of clipp; but it seemed really buggy, and writing something less functional
* with similar syntax seemed like the quickest way to go.
*
* Ironically this is probably just as buggy off the happy path as clipp appeared to be, but
* in this case the happy path is ours!
*/
namespace cli {
typedef std::string string;
template<typename T> using vector = std::vector<T>;
template<typename A, typename B> using map = std::map<A, B>;
template<typename A, typename B> using pair = std::pair<A, B>;
template<typename T> using shared_ptr = std::shared_ptr<T>;
auto join = [](const vector<string> &range, const string &separator) {
if (range.empty()) return string();
return accumulate(
next(begin(range)), // there is at least 1 element, so OK.
end(range),
range[0], // the initial value
[&separator](auto result, const auto &value) {
return result + separator + value;
});
};
struct parse_error : public std::exception {
explicit parse_error(string what) : _what(std::move(what)) {}
const char *what() const noexcept override {
return _what.c_str();
}
private:
string _what;
};
struct group;
template<typename T>
struct matchable_derived;
template <typename K, typename V> struct map_and_order {
map<K,V> _map;
vector<K> _order;
V& operator[](const K& key) {
auto i = _map.find(key);
if (i == _map.end()) {
_order.push_back(key);
}
return _map[key];
}
vector<K> ordered_keys() {
return _order;
}
};
struct option_map {
typedef map_and_order<string, map_and_order<string, vector<pair<string, string>>>> container;
void add(const string& major_group, const string& minor_group, const string& option, const string& description) {
auto &v = contents[major_group][minor_group];
// we don't want to repeat the same option
if (std::find_if(v.begin(), v.end(), [&](const auto &x) { return x.first == option; }) == v.end()) {
v.emplace_back(option, description);
}
}
container contents;
};
struct matchable;
enum struct match_type {
not_yet,
match,
error,
no_match,
};
struct opaque_settings {
virtual shared_ptr<opaque_settings> copy() = 0;
virtual void save_into() = 0;
virtual void apply_from() = 0;
};
struct settings_holder {
explicit settings_holder(shared_ptr<opaque_settings> settings) : settings(settings) {}
settings_holder(const settings_holder &other) {
settings = other.settings->copy();
}
settings_holder& operator=(const settings_holder&) = default;
void save_into() {
settings->save_into();
}
void apply_from() {
settings->apply_from();
}
shared_ptr<opaque_settings> settings;
};
struct match_state {
vector<string> remaining_args;
string error_message;
int match_count = 0;
int error_count = 0;
// if we are an error for something mising; we should rather report on an up next
// unsupported option than our error message
bool prefer_unknown_option_message = false;
std::map<const matchable *, int> matchable_counts;
settings_holder settings;
match_state(const settings_holder& settings) : settings(settings) {}
void apply_settings_from() {
settings.apply_from();
}
void save_settings_into() {
settings.save_into();
}
match_type match_value(const matchable *matchable, std::function<bool(const string&)> filter);
match_type check_min_max(const matchable *matchable);
int get_match_count(const std::shared_ptr<matchable>& element) {
return matchable_counts[element.get()];
}
match_type update_stats(match_type type, const matchable *matchable) {
assert(type != match_type::not_yet);
if (type == match_type::match) {
match_count++;
matchable_counts[matchable]++;
} else if (type == match_type::error) {
error_count++;
matchable_counts[matchable]++;
}
return type;
}
match_type match_if_equal(const matchable *matchable, const string& s);
};
struct matcher {
};
struct matchable {
matchable() = default;
virtual ~matchable() = default;
explicit matchable(string name) : _name(std::move(name)) {}
std::function<string(string)> action = [](const string&) { return ""; };
std::function<string()> missing;
virtual match_type match(match_state& m) const { return match_type::no_match; }
string name() const {
return _name;
}
virtual std::vector<string> synopsys() const {
return {_name};
}
virtual bool is_optional() const {
return !_min;
}
bool doc_non_optional() const {
return _doc_non_optional;
}
bool force_expand_help() const {
return _force_expand_help;
}
string collapse_synopsys() const {
return _collapse_synopsys;
}
string doc() const {
return _doc;
}
virtual bool get_option_help(string major_group, string minor_group, option_map &options) const {
return false;
}
int min() const {
return _min;
}
int max() const {
return _max;
}
protected:
string _name;
string _doc;
int _min = 1;
int _max = 1;
bool _doc_non_optional = false;
bool _force_expand_help = false;
string _collapse_synopsys = "";
};
template<typename D>
struct matchable_derived : public matchable {
matchable_derived() = default;
explicit matchable_derived(string name) : matchable(std::move(name)) {}
D &on_action(std::function<string(const string&)> action) {
this->action = action;
return *static_cast<D *>(this);
}
D &if_missing(std::function<string()> missing) {
this->missing = missing;
return *static_cast<D *>(this);
}
D &operator%(const string& doc) {
_doc = doc;
return *static_cast<D *>(this);
}
D &required() {
_min = 1;
_max = std::max(_min, _max);
return *static_cast<D *>(this);
}
D &repeatable() {
_max = std::numeric_limits<int>::max();
return *static_cast<D *>(this);
}
D &min(int v) {
_min = v;
return *static_cast<D *>(this);
}
D &doc_non_optional(bool v) {
_doc_non_optional = v;
return *static_cast<D *>(this);
}
D &force_expand_help(bool v) {
_force_expand_help = v;
return *static_cast<D *>(this);
}
D &collapse_synopsys(string v) {
_collapse_synopsys = v;
return *static_cast<D *>(this);
}
D &max(int v) {
_max = v;
return *static_cast<D *>(this);
}
std::shared_ptr<matchable> to_ptr() const {
return std::shared_ptr<matchable>(new D(*static_cast<const D *>(this)));
}
template<typename T>
group operator&(const matchable_derived<T> &m);
template<typename T>
group operator|(const matchable_derived<T> &m);
template<typename T>
group operator+(const matchable_derived<T> &m);
};
template<typename D>
struct value_base : public matchable_derived<D> {
std::function<bool(const string&)> exclusion_filter = [](const string &x){return false;};
explicit value_base(string name) : matchable_derived<D>(std::move(name)) {
this->_min = 1;
this->_max = 1;
}
vector<string> synopsys() const override {
string s = string("<") + this->_name + ">";
if (this->_max > 1) s += "..";
return {s};
}
bool get_option_help(string major_group, string minor_group, option_map &options) const override {
if (this->doc().empty()) {
return false;
}
options.add(major_group, minor_group, string("<") + this->_name + ">", this->doc());
return true;
}
match_type match(match_state& ms) const override {
match_type rc = ms.check_min_max(this);
if (rc == match_type::not_yet) {
rc = ms.match_value(this, exclusion_filter);
}
return rc;
}
D &with_exclusion_filter(std::function<bool(const string&)> exclusion_filter) {
this->exclusion_filter = exclusion_filter;
return *static_cast<D *>(this);
}
};
struct option : public matchable_derived<option> {
explicit option(char short_opt) : option(short_opt, "") {}
explicit option(string _long_opt) : option(0, std::move(_long_opt)) {}
option(char _short_opt, string _long_opt) {
_min = 0;
short_opt = _short_opt ? "-" + string(1, _short_opt) : "";
long_opt = std::move(_long_opt);
_name = short_opt.empty() ? long_opt : short_opt;
}
bool get_option_help(string major_group, string minor_group, option_map &options) const override {
if (doc().empty()) return false;
string label = short_opt.empty() ? "" : _name;
if (!long_opt.empty()) {
if (!label.empty()) label += ", ";
label += long_opt;
}
options.add(major_group, minor_group, label, doc());
return true;
}
template<typename T>
option &set(T &t) {
// note we cannot capture "this"
on_action([&t](const string& value) {
t = true;
return "";
});
return *this;
}
template<typename T>
option &clear(T &t) {
// note we cannot capture "this"
on_action([&t](const string& value) {
t = false;
return "";
});
return *this;
}
match_type match(match_state &ms) const override {
match_type rc = ms.match_if_equal(this, short_opt);
if (rc == match_type::no_match) {
rc = ms.match_if_equal(this, long_opt);
}
return rc;
}
private:
string short_opt;
string long_opt;
};
struct value : public value_base<value> {
explicit value(string name) : value_base(std::move(name)) {}
template<typename T>
value &set(T &t) {
on_action([&](const string& value) {
t = value;
return "";
});
return *this;
}
template<typename T> value &add_to(T &t) {
// note we cannot capture "this"
on_action([&t](const string& value) {
t.push_back(value);
return "";
});
return *this;
}
};
struct integer : public value_base<integer> {
explicit integer(string name) : value_base(std::move(name)) {}
template<typename T>
static std::string parse_string(std::string value, T& out) {
size_t pos = 0;
long lvalue = std::numeric_limits<long>::max();
int64_t base = 10;
if (value.find("0x") == 0) {
value = value.substr(2);
base = 16;
} else if (value.find("0b") == 0) {
value = value.substr(2);
base = 2;
}
try {
lvalue = std::stoll(value, &pos, base);
if (pos != value.length()) {
return "Garbage after integer value: " + value.substr(pos);
}
} catch (std::invalid_argument&) {
return value + " is not a valid integer";
} catch (std::out_of_range&) {
}
if (lvalue != (int64_t)lvalue) {
return value + " is too big";
}
out = (int64_t)lvalue;
return "";
}
template<typename T>
integer &set(T &t) {
int64_t min = _min_value;
int64_t max = _max_value;
int64_t invalid_bits = _invalid_bits;
std::string invalid_bits_error = _invalid_bits_error;
string nm = "<" + name() + ">";
// note we cannot capture "this"
on_action([&t, min, max, nm, invalid_bits, invalid_bits_error](const string& value) {
int64_t tmp = 0;
std::string err = parse_string(value, tmp);
t = tmp;
if (!err.empty()) return err;
if (t < min) {
return nm + " must be >= " + std::to_string(min);
}
if (t > max) {
return nm + " must be <= " + std::to_string(max);
}
if (t & invalid_bits) {
return nm + " " + invalid_bits_error;
}
return string("");
});
return *this;
}
template<typename T>
integer &add_to(T &t) {
int64_t min = _min_value;
int64_t max = _max_value;
int64_t invalid_bits = _invalid_bits;
std::string invalid_bits_error = _invalid_bits_error;
string nm = "<" + name() + ">";
// note we cannot capture "this"
on_action([&t, min, max, nm, invalid_bits, invalid_bits_error](const string& value) {
int64_t tmp = 0;
std::string err = parse_string(value, tmp);
if (!err.empty()) return err;
if (tmp < min) {
return nm + " must be >= " + std::to_string(min);
}
if (tmp > max) {
return nm + " must be <= " + std::to_string(max);
}
if (tmp & invalid_bits) {
return nm + " " + invalid_bits_error;
}
t.push_back(tmp);
return string("");
});
return *this;
}
integer& min_value(int64_t v) {
_min_value = v;
return *this;
}
integer& max_value(int64_t v) {
_max_value = v;
return *this;
}
integer& invalid_bits(int64_t bits, std::string error) {
_invalid_bits = bits;
_invalid_bits_error = error;
return *this;
}
int64_t _min_value = 0;
int64_t _max_value = std::numeric_limits<int64_t>::max();
std::string _invalid_bits_error;
int64_t _invalid_bits = 0;
};
struct hex : public value_base<hex> {
explicit hex(string name) : value_base(std::move(name)) {}
template<typename T>
hex &set(T &t) {
unsigned int min = _min_value;
unsigned int max = _max_value;
string nm = "<" + name() + ">";
// note we cannot capture "this"
on_action([&t, min, max, nm](string value) {
auto ovalue = value;
if (value.find("0x") == 0) value = value.substr(2);
size_t pos = 0;
long lvalue = std::numeric_limits<long>::max();
try {
lvalue = std::stoul(value, &pos, 16);
if (pos != value.length()) {
return "Garbage after hex value: " + value.substr(pos);
}
} catch (std::invalid_argument&) {
return ovalue + " is not a valid hex value";
} catch (std::out_of_range&) {
}
if (lvalue != (unsigned int)lvalue) {
return value + " is not a valid 32 bit value";
}
t = (unsigned int)lvalue;
if (t < min) {
std::stringstream ss;
ss << nm << " must be >= 0x" << std::hex << std::to_string(min);
return ss.str();
}
if (t > max) {
std::stringstream ss;
ss << nm << " must be M= 0x" << std::hex << std::to_string(min);
return ss.str();
}
return string("");
});
return *this;
}
template<typename T>
hex &add_to(T &t) {
unsigned int min = _min_value;
unsigned int max = _max_value;
string nm = "<" + name() + ">";
// note we cannot capture "this"
on_action([&t, min, max, nm](string value) {
auto ovalue = value;
if (value.find("0x") == 0) value = value.substr(2);
size_t pos = 0;
long lvalue = std::numeric_limits<long>::max();
try {
lvalue = std::stoul(value, &pos, 16);
if (pos != value.length()) {
return "Garbage after hex value: " + value.substr(pos);
}
} catch (std::invalid_argument&) {
return ovalue + " is not a valid hex value";
} catch (std::out_of_range&) {
}
if (lvalue != (unsigned int)lvalue) {
return value + " is not a valid 32 bit value";
}
unsigned int tmp = (unsigned int)lvalue;
if (tmp < min) {
std::stringstream ss;
ss << nm << " must be >= 0x" << std::hex << std::to_string(min);
return ss.str();
}
if (tmp > max) {
std::stringstream ss;
ss << nm << " must be <= 0x" << std::hex << std::to_string(max);
return ss.str();
}
t.push_back(tmp);
return string("");
});
return *this;
}
hex& min_value(unsigned int v) {
_min_value = v;
return *this;
}
hex& max_value(unsigned int v) {
_max_value = v;
return *this;
}
unsigned int _min_value = 0;
unsigned int _max_value = std::numeric_limits<unsigned int>::max();
};
struct group : public matchable_derived<group> {
enum group_type {
sequence,
set,
exclusive,
collapse,
};
public:
group() : type(set) {}
template<typename T>
explicit group(const T &t) : type(set), elements{t.to_ptr()} {}
template<class Matchable, class... Matchables>
group(Matchable m, Matchable ms...) : type(set), elements{m, ms} {}
group &set_type(group_type t) {
type = t;
return *this;
}
group &major_group(string g) {
_major_group = std::move(g);
return *this;
}
static string decorate(const matchable &e, string s) {
if (e.is_optional() && !e.doc_non_optional()) {
return string("[") + s + "]";
} else {
return s;
}
}
vector<string> synopsys() const override {
vector<string> rc;
switch (type) {
case set:
case sequence: {
std::vector<std::vector<string>> tmp{{}};
if (_collapse_synopsys.empty()) {
for (auto &x : elements) {
auto xs = x->synopsys();
if (xs.size() == 1) {
for (auto &s : tmp) {
s.push_back(decorate(*x, xs[0]));
}
} else {
auto save = tmp;
tmp.clear();
for (auto &v : save) {
for (auto &s : xs) {
auto nv = v;
nv.push_back(decorate(*x, s));
tmp.push_back(nv);
}
}
}
}
} else {
std::vector<std::string> xs = {"[" + _collapse_synopsys + "]"};
for (auto &s : tmp) {
s.push_back(xs[0]);
}
}
for (const auto &v : tmp) {
rc.push_back(join(v, " "));
}
break;
}
case exclusive:
for (auto &x : elements) {
auto xs = x->synopsys();
std::transform(xs.begin(), xs.end(), std::back_inserter(rc), [&](const auto &s) {
return decorate(*x, s);
});
}
break;
default:
assert(false);
break;
}
return rc;
}
// group operator|(const group &g) {
// return matchable_derived::operator|(g);
// }
//
// group operator&(const group &g) {
// return matchable_derived::operator&(g);
// }
//
// group operator+(const group &g) {
// return matchable_derived::operator+(g);
// }
bool no_match_beats_error() const {
return _no_match_beats_error;
}
group &no_match_beats_error(bool v) {
_no_match_beats_error = v;
return *this;
}
template<typename T>
group operator&(const matchable_derived<T> &m) {
if (type == sequence) {
elements.push_back(m.to_ptr());
return *this;
}
return matchable_derived::operator&(m);
}
template<typename T>
group operator|(const matchable_derived<T> &m) {
if (type == exclusive) {
elements.push_back(m.to_ptr());
return *this;
}
return matchable_derived::operator|(m);
}
template<typename T>
group operator+(const matchable_derived<T> &m) {
if (type == set) {
elements.push_back(m.to_ptr());
return *this;
}
return matchable_derived::operator+(m);
}
bool get_option_help(string major_group, string minor_group, option_map &options) const override {
// todo beware.. this check is necessary as is, but I'm not sure what removing it breaks in terms of formatting :-(
if (is_optional() && !this->_doc_non_optional && !this->_force_expand_help) {
options.add(major_group, minor_group, synopsys()[0], doc());
return true;
}
if (!doc().empty()) {
minor_group = doc();
}
if (!_major_group.empty()) {
major_group = _major_group;
}
for (const auto &e : elements) {
e->get_option_help(major_group, minor_group, options);
}
return true;
}
match_type match(match_state& ms) const override {
match_type rc = ms.check_min_max(this);
if (rc == match_type::no_match) return rc;
assert(rc == match_type::not_yet);
switch(type) {
case sequence:
rc = match_sequence(ms);
break;
case set:
rc = match_set(ms);
break;
default:
rc = match_exclusive(ms);
break;
}
return ms.update_stats(rc, this);
}
match_type match_sequence(match_state& ms) const {
match_type rc = match_type::no_match;
for(const auto& e : elements) {
rc = e->match(ms);
assert(rc != match_type::not_yet);
if (rc != match_type::match) {
break;
}
}
return rc;
}
match_type match_set(match_state& ms) const {
// because of repeatability, we keep matching until there is nothing left to match
// vector<match_type> types(elements.size(), match_type::not_yet);
bool had_any_matches = false;
bool final_pass = false;
do {
bool matches_this_time = false;
bool errors_this_time = false;
bool not_min_this_time = false;
for (size_t i=0;i<elements.size();i++) {
// if (types[i] == match_type::not_yet) {
auto ms_prime = ms;
ms_prime.apply_settings_from();
match_type t = elements[i]->match(ms_prime);
assert(t != match_type::not_yet);
if (t == match_type::match) {
// we got a match, so record in ms and try again
// (if the matchable isn't repeatable it will no match next time)
// types[i] = match_type::not_yet;
ms_prime.save_settings_into();
ms = ms_prime;
had_any_matches = true;
matches_this_time = true;
} else if (t == match_type::error) {
if (final_pass) {
ms_prime.save_settings_into();
ms = ms_prime;
return t;
}
errors_this_time = true;
} else {
if (ms.get_match_count(elements[i]) < elements[i]->min()) {
if (final_pass) {
ms.error_message = elements[i]->missing ? elements[i]->missing() : "missing required argument";
return match_type::error;
}
not_min_this_time = true;
}
}
// }
}
if (final_pass) break;
if (!matches_this_time) {
if (errors_this_time || not_min_this_time) {
final_pass = true;
} else {
break;
}
}
} while (true);
return had_any_matches ? match_type::match : match_type::no_match;
}
match_type match_exclusive(match_state& ms) const {
vector<match_state> matches(elements.size(), ms);
vector<match_type> types(elements.size(), match_type::no_match);
int elements_with_errors = 0;
int elements_with_no_match = 0;
int error_at = -1;
int error_match_count = -1;
for (size_t i=0;i<elements.size();i++) {
match_type t;
matches[i].apply_settings_from();
do {
t = elements[i]->match(matches[i]);
assert(t != match_type::not_yet);
if (t != match_type::no_match) {
types[i] = t;
}
} while (t == match_type::match);
matches[i].save_settings_into();
if (types[i] == match_type::match) {
ms = matches[i];
return match_type::match;
} else if (types[i] == match_type::error) {
if (matches[i].match_count > error_match_count) {
error_match_count = matches[i].match_count;
error_at = i;
}
elements_with_errors++;
} else if (types[i] == match_type::no_match) {
elements_with_no_match++;
}
}
if (elements_with_no_match && (!elements_with_errors || no_match_beats_error())) {
return match_type::no_match;
}
if (elements_with_errors) {
ms = matches[error_at];
ms.apply_settings_from(); // todo perhaps want to apply the previous settings instead?
return match_type::error;
} else {
// back out any modified settings
ms.apply_settings_from();
return match_type::no_match;
}
}
private:
string _major_group;
group_type type;
vector<std::shared_ptr<matchable>> elements;
bool _no_match_beats_error = true;
};
template<typename D>
template<typename T>
group matchable_derived<D>::operator|(const matchable_derived<T> &m) {
return group{this->to_ptr(), m.to_ptr()}.set_type(group::exclusive);
}
template<typename D>
template<typename T>
group matchable_derived<D>::operator&(const matchable_derived<T> &m) {
int _min = matchable::min();
int _max = matchable::max();
min(1);
max(1);
return group{this->to_ptr(), m.to_ptr()}.set_type(group::sequence).min(_min).max(_max);
}
template<typename D>
template<typename T>
group matchable_derived<D>::operator+(const matchable_derived<T> &m) {
return group{this->to_ptr(), m.to_ptr()};
}
vector<string> make_args(int argc, char **argv) {
vector<string> args;
for (int i = 1; i < argc; i++) {
string arg(argv[i]);
if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') {
// expand collapsed args (unconditionally for now)
for (auto c = arg.begin() + 1; c != arg.end(); c++) {
args.push_back("-" + string(1, *c));
}
} else {
args.push_back(arg);
}
}
return args;
}
match_type match_state::check_min_max(const matchable *matchable) {
if (matchable_counts[matchable] < matchable->min()) {
return match_type::not_yet;
}
if (matchable_counts[matchable] >= matchable->max()) {
return match_type::no_match;
}
return match_type::not_yet;
}
match_type match_state::match_if_equal(const matchable *matchable, const string& s) {
if (remaining_args.empty()) return match_type::no_match;
if (remaining_args[0] == s) {
auto message = matchable->action(s);
assert(message.empty());
remaining_args.erase(remaining_args.begin());
return update_stats(match_type::match, matchable);
}
return match_type::no_match;
}
match_type match_state::match_value(const matchable *matchable, std::function<bool(const string&)> exclusion_filter) {
// treat an excluded value as missing
bool empty = remaining_args.empty() || exclusion_filter(remaining_args[0]);
if (empty) {
if (matchable_counts[matchable] < matchable->min()) {
prefer_unknown_option_message = !remaining_args.empty();
error_message = matchable->missing ? matchable->missing() : "missing <" + matchable->name() +">";
return update_stats(match_type::error, matchable);
}
return match_type::no_match;
}
auto message = matchable->action(remaining_args[0]);
if (!message.empty()) {
error_message = message;
return update_stats(match_type::error, matchable);
}
remaining_args.erase(remaining_args.begin());
return update_stats(match_type::match, matchable);
}
template<typename S> struct typed_settings final : public opaque_settings {
explicit typed_settings(S& settings) : root_settings(settings), settings(settings) {
}
shared_ptr<cli::opaque_settings> copy() override {
auto c = std::make_shared<typed_settings<S>>(*this);
c->settings = settings;
return c;
}
void save_into() override {
settings = root_settings;
}