Newer
Older
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
dfs_traverser lastMatch_;
dfs_traverser posAfterLastMatch_;
std::stack<dfs_traverser::memento> scopes_;
bool curMatched_ = false;
bool ignoreBlocks_ = false;
bool repeatGroupStarted_ = false;
bool repeatGroupContinues_ = false;
};
/*****************************************************************************
*
* some parameter property predicates
*
*****************************************************************************/
struct select_all {
bool operator () (const parameter&) const noexcept { return true; }
};
struct select_flags {
bool operator () (const parameter& p) const noexcept {
return !p.flags().empty();
}
};
struct select_values {
bool operator () (const parameter& p) const noexcept {
return p.flags().empty();
}
};
/*************************************************************************//**
*
* @brief result of a matching operation
*
*****************************************************************************/
class match_t {
public:
match_t() = default;
match_t(arg_string s, scoped_dfs_traverser p):
str_{std::move(s)}, pos_{std::move(p)}
{}
const arg_string& str() const noexcept { return str_; }
const scoped_dfs_traverser& pos() const noexcept { return pos_; }
explicit operator bool() const noexcept { return !str_.empty(); }
private:
arg_string str_;
scoped_dfs_traverser pos_;
};
/*************************************************************************//**
*
* @brief finds the first parameter that matches a given string
* candidate parameters are traversed using a scoped DFS traverser
*
*****************************************************************************/
template<class Predicate>
match_t
full_match(scoped_dfs_traverser pos, const arg_string& arg,
const Predicate& select)
{
if(arg.empty()) return match_t{};
while(pos) {
if(pos->is_param()) {
const auto& param = pos->as_param();
if(select(param)) {
const auto match = param.match(arg);
if(match && match.length() == arg.size()) {
return match_t{arg, std::move(pos)};
}
}
}
++pos;
}
return match_t{};
}
/*************************************************************************//**
*
* @brief finds the first parameter that matches any (non-empty) prefix
* of a given string;
* candidate parameters are traversed using a scoped DFS traverser
*
*****************************************************************************/
template<class Predicate>
match_t
prefix_match(scoped_dfs_traverser pos, const arg_string& arg,
const Predicate& select)
{
if(arg.empty()) return match_t{};
while(pos) {
if(pos->is_param()) {
const auto& param = pos->as_param();
if(select(param)) {
const auto match = param.match(arg);
if(match.prefix()) {
if(match.length() == arg.size()) {
return match_t{arg, std::move(pos)};
}
else {
return match_t{arg.substr(match.at(), match.length()),
std::move(pos)};
}
}
}
}
++pos;
}
return match_t{};
}
/*************************************************************************//**
*
* @brief finds the first parameter that partially matches a given string;
* candidate parameters are traversed using a scoped DFS traverser
*
*****************************************************************************/
template<class Predicate>
match_t
partial_match(scoped_dfs_traverser pos, const arg_string& arg,
const Predicate& select)
{
if(arg.empty()) return match_t{};
while(pos) {
if(pos->is_param()) {
const auto& param = pos->as_param();
if(select(param)) {
const auto match = param.match(arg);
if(match) {
return match_t{arg.substr(match.at(), match.length()),
std::move(pos)};
}
}
}
++pos;
}
return match_t{};
}
} //namespace detail
/***************************************************************//**
*
* @brief default command line arguments parser
*
*******************************************************************/
class parser
{
public:
using dfs_traverser = group::depth_first_traverser;
using scoped_dfs_traverser = detail::scoped_dfs_traverser;
/*****************************************************//**
* @brief arg -> parameter mapping
*********************************************************/
class arg_mapping {
public:
friend class parser;
explicit
arg_mapping(arg_index idx, arg_string s,
const dfs_traverser& match)
:
index_{idx}, arg_{std::move(s)}, match_{match},
repeat_{0}, startsRepeatGroup_{false},
blocked_{false}, conflict_{false}
{}
explicit
arg_mapping(arg_index idx, arg_string s) :
index_{idx}, arg_{std::move(s)}, match_{},
repeat_{0}, startsRepeatGroup_{false},
blocked_{false}, conflict_{false}
{}
arg_index index() const noexcept { return index_; }
const arg_string& arg() const noexcept { return arg_; }
const parameter* param() const noexcept {
return match_ && match_->is_param()
? &(match_->as_param()) : nullptr;
}
std::size_t repeat() const noexcept { return repeat_; }
bool blocked() const noexcept { return blocked_; }
bool conflict() const noexcept { return conflict_; }
bool bad_repeat() const noexcept {
if(!param()) return false;
return repeat_ > 0 && !param()->repeatable()
&& !match_.repeat_group();
}
bool any_error() const noexcept {
return !match_ || blocked() || conflict() || bad_repeat();
}
private:
arg_index index_;
arg_string arg_;
dfs_traverser match_;
std::size_t repeat_;
bool startsRepeatGroup_;
bool blocked_;
bool conflict_;
};
/*****************************************************//**
* @brief references a non-matched, required parameter
*********************************************************/
class missing_event {
public:
explicit
missing_event(const parameter* p, arg_index after):
param_{p}, aftIndex_{after}
{}
const parameter* param() const noexcept { return param_; }
arg_index after_index() const noexcept { return aftIndex_; }
private:
const parameter* param_;
arg_index aftIndex_;
};
//-----------------------------------------------------
using missing_events = std::vector<missing_event>;
using arg_mappings = std::vector<arg_mapping>;
private:
struct miss_candidate {
miss_candidate(dfs_traverser p, arg_index idx,
bool firstInRepeatGroup = false):
pos{std::move(p)}, index{idx},
startsRepeatGroup{firstInRepeatGroup}
{}
dfs_traverser pos;
arg_index index;
bool startsRepeatGroup;
};
using miss_candidates = std::vector<miss_candidate>;
public:
//---------------------------------------------------------------
/** @brief initializes parser with a command line interface
* @param offset = argument index offset used for reports
* */
explicit
parser(const group& root, arg_index offset = 0):
root_{&root}, pos_{root},
index_{offset-1}, eaten_{0},
args_{}, missCand_{}, blocked_{false}
{
for_each_potential_miss(dfs_traverser{root},
[this](const dfs_traverser& p){
missCand_.emplace_back(p, index_);
});
}
//---------------------------------------------------------------
/** @brief processes one command line argument */
bool operator() (const arg_string& arg)
{
++eaten_;
++index_;
if(!valid() || arg.empty()) return false;
if(!blocked_ && try_match(arg)) return true;
if(try_match_blocked(arg)) return false;
//skipping of blocking & required patterns is not allowed
if(!blocked_ && !pos_.matched() && pos_->required() && pos_->blocking()) {
blocked_ = true;
return false;
}
add_nomatch(arg);
return false;
}
//---------------------------------------------------------------
/** @brief returns range of argument -> parameter mappings */
const arg_mappings& args() const {
return args_;
}
/** @brief returns list of missing events */
missing_events missed() const {
missing_events misses;
misses.reserve(missCand_.size());
for(auto i = missCand_.begin(); i != missCand_.end(); ++i) {
misses.emplace_back(&(i->pos->as_param()), i->index);
}
return misses;
}
/** @brief returns number of processed command line arguments */
arg_index parse_count() const noexcept { return eaten_; }
/** @brief returns false if previously processed command line arguments
* lead to an invalid / inconsistent parsing result
*/
bool valid() const noexcept { return bool(pos_); }
/** @brief returns false if previously processed command line arguments
* lead to an invalid / inconsistent parsing result
*/
explicit operator bool() const noexcept { return valid(); }
private:
//---------------------------------------------------------------
using match_t = detail::match_t;
//---------------------------------------------------------------
/** @brief try to match argument with unreachable parameter */
bool try_match_blocked(const arg_string& arg)
{
//try to match ahead (using temporary parser)
if(pos_) {
auto ahead = *this;
if(try_match_blocked(std::move(ahead), arg)) return true;
}
//try to match from the beginning (using temporary parser)
if(root_) {
parser all{*root_, index_+1};
if(try_match_blocked(std::move(all), arg)) return true;
}
return false;
}
//---------------------------------------------------------------
bool try_match_blocked(parser&& parse, const arg_string& arg)
{
const auto nold = int(parse.args_.size());
parse.pos_.ignore_blocking(true);
if(!parse.try_match(arg)) return false;
for(auto i = parse.args_.begin() + nold; i != parse.args_.end(); ++i) {
args_.push_back(*i);
args_.back().blocked_ = true;
}
return true;
}
//---------------------------------------------------------------
/** @brief try to find a parameter/pattern that matches 'arg' */
bool try_match(const arg_string& arg)
{
//Note: flag-params will always take precedence over value-params
if(try_match_full(arg, detail::select_flags{})) return true;
if(try_match_joined_flags(arg)) return true;
if(try_match_joined_sequence(arg, detail::select_flags{})) return true;
if(try_match_full(arg, detail::select_values{})) return true;
if(try_match_joined_sequence(arg, detail::select_all{})) return true;
if(try_match_joined_params(arg)) return true;
return false;
}
//---------------------------------------------------------------
template<class Predicate>
bool try_match_full(const arg_string& arg, const Predicate& select)
{
auto match = detail::full_match(pos_, arg, select);
if(!match) return false;
add_match(match);
return true;
}
//---------------------------------------------------------------
template<class Predicate>
bool try_match_joined_sequence(arg_string arg, const Predicate& acceptFirst)
{
auto fstMatch = detail::prefix_match(pos_, arg, acceptFirst);
if(!fstMatch) return false;
if(fstMatch.str().size() == arg.size()) {
add_match(fstMatch);
return true;
}
if(!fstMatch.pos()->blocking()) return false;
auto pos = fstMatch.pos();
pos.ignore_blocking(true);
const auto parent = &pos.parent();
if(!pos->repeatable()) ++pos;
arg.erase(0, fstMatch.str().size());
std::vector<match_t> matches { std::move(fstMatch) };
while(!arg.empty() && pos &&
pos->blocking() && pos->is_param() &&
(&pos.parent() == parent))
{
auto match = pos->as_param().match(arg);
if(match.prefix()) {
matches.emplace_back(arg.substr(0,match.length()), pos);
arg.erase(0, match.length());
if(!pos->repeatable()) ++pos;
}
else {
if(!pos->repeatable()) return false;
++pos;
}
}
if(!arg.empty() || matches.empty()) return false;
for(const auto& m : matches) add_match(m);
return true;
}
//-----------------------------------------------------
bool try_match_joined_flags(const arg_string& arg)
{
return try_match_joined([&](const group& g) {
if(try_match_joined(g, arg, detail::select_flags{},
g.common_flag_prefix()) )
{
return true;
}
return false;
});
}
//---------------------------------------------------------------
bool try_match_joined_params(const arg_string& arg)
{
return try_match_joined([&](const group& g) {
if(try_match_joined(g, arg, detail::select_all{}) ) {
return true;
}
return false;
});
}
//-----------------------------------------------------
template<class Predicate>
bool try_match_joined(const group& joinGroup, arg_string arg,
const Predicate& pred,
const arg_string& prefix = "")
{
parser parse {joinGroup};
std::vector<match_t> matches;
while(!arg.empty()) {
auto match = detail::prefix_match(parse.pos_, arg, pred);
if(!match) return false;
arg.erase(0, match.str().size());
//make sure prefix is always present after the first match
//ensures that, e.g., flags "-a" and "-b" will be found in "-ab"
if(!arg.empty() && !prefix.empty() && arg.find(prefix) != 0 &&
prefix != match.str())
{
arg.insert(0,prefix);
}
parse.add_match(match);
matches.push_back(std::move(match));
}
if(!arg.empty() || matches.empty()) return false;
if(!parse.missCand_.empty()) return false;
for(const auto& a : parse.args_) if(a.any_error()) return false;
//replay matches onto *this
for(const auto& m : matches) add_match(m);
return true;
}
//-----------------------------------------------------
template<class Predicate>
bool try_match_joined(const Predicate& pred)
{
if(pos_ && pos_.parent().joinable()) {
const auto& g = pos_.parent();
if(pred(g)) return true;
return false;
}
auto pos = pos_;
while(pos) {
if(pos->is_group() && pos->as_group().joinable()) {
const auto& g = pos->as_group();
if(pred(g)) return true;
pos.next_sibling();
}
else {
++pos;
}
}
return false;
}
//---------------------------------------------------------------
void add_nomatch(const arg_string& arg) {
args_.emplace_back(index_, arg);
}
//---------------------------------------------------------------
void add_match(const match_t& match)
{
const auto& pos = match.pos();
if(!pos || !pos->is_param() || match.str().empty()) return;
pos_.next_after_match(pos);
arg_mapping newArg{index_, match.str(), pos.base()};
newArg.repeat_ = occurrences_of(&pos->as_param());
newArg.conflict_ = check_conflicts(pos.base());
newArg.startsRepeatGroup_ = pos_.start_of_repeat_group();
args_.push_back(std::move(newArg));
add_miss_candidates_after(pos);
clean_miss_candidates_for(pos.base());
discard_alternative_miss_candidates(pos.base());
}
//-----------------------------------------------------
bool check_conflicts(const dfs_traverser& match)
{
if(pos_.start_of_repeat_group()) return false;
bool conflict = false;
for(const auto& m : match.stack()) {
if(m.parent->exclusive()) {
for(auto i = args_.rbegin(); i != args_.rend(); ++i) {
if(!i->blocked()) {
for(const auto& c : i->match_.stack()) {
//sibling within same exclusive group => conflict
if(c.parent == m.parent && c.cur != m.cur) {
conflict = true;
i->conflict_ = true;
}
}
}
//check for conflicts only within current repeat cycle
if(i->startsRepeatGroup_) break;
}
}
}
return conflict;
}
//-----------------------------------------------------
void clean_miss_candidates_for(const dfs_traverser& match)
{
auto i = std::find_if(missCand_.rbegin(), missCand_.rend(),
[&](const miss_candidate& m) {
return &(*m.pos) == &(*match);
});
if(i != missCand_.rend()) {
missCand_.erase(prev(i.base()));
}
}
//-----------------------------------------------------
void discard_alternative_miss_candidates(const dfs_traverser& match)
{
if(missCand_.empty()) return;
//find out, if miss candidate is sibling of one of the same
//alternative groups that the current match is a member of
//if so, we can discard the miss
//go through all exclusive groups of matching pattern
for(const auto& m : match.stack()) {
if(m.parent->exclusive()) {
for(auto i = int(missCand_.size())-1; i >= 0; --i) {
bool removed = false;
for(const auto& c : missCand_[i].pos.stack()) {
//sibling within same exclusive group => discard
if(c.parent == m.parent && c.cur != m.cur) {
missCand_.erase(missCand_.begin() + i);
if(missCand_.empty()) return;
removed = true;
break;
}
}
//remove miss candidates only within current repeat cycle
if(i > 0 && removed) {
if(missCand_[i-1].startsRepeatGroup) break;
} else {
if(missCand_[i].startsRepeatGroup) break;
}
}
}
}
}
//-----------------------------------------------------
void add_miss_candidates_after(const scoped_dfs_traverser& match)
{
auto npos = match.base();
if(npos.is_alternative()) npos.skip_alternatives();
++npos;
//need to add potential misses if:
//either new repeat group was started
const auto newRepGroup = match.repeat_group();
if(newRepGroup) {
if(pos_.start_of_repeat_group()) {
for_each_potential_miss(std::move(npos),
[&,this](const dfs_traverser& pos) {
//only add candidates within repeat group
if(newRepGroup == pos.repeat_group()) {
missCand_.emplace_back(pos, index_, true);
}
});
}
}
//... or an optional blocking param was hit
else if(match->blocking() && !match->required() &&
npos.level() >= match.base().level())
{
for_each_potential_miss(std::move(npos),
[&,this](const dfs_traverser& pos) {
//only add new candidates
if(std::find_if(missCand_.begin(), missCand_.end(),
[&](const miss_candidate& c){
return &(*c.pos) == &(*pos);
}) == missCand_.end())
{
missCand_.emplace_back(pos, index_);
}
});
}
}
//-----------------------------------------------------
template<class Action>
static void
for_each_potential_miss(dfs_traverser pos, Action&& action)
{
const auto level = pos.level();
while(pos && pos.level() >= level) {
if(pos->is_group() ) {
const auto& g = pos->as_group();
if(g.all_optional() || (g.exclusive() && g.any_optional())) {
pos.next_sibling();
} else {
++pos;
}
} else { //param
if(pos->required()) {
action(pos);
++pos;
} else if(pos->blocking()) { //optional + blocking
pos.next_after_siblings();
} else {
++pos;
}
}
}
}
//---------------------------------------------------------------
std::size_t occurrences_of(const parameter* p) const
{
auto i = std::find_if(args_.rbegin(), args_.rend(),
[p](const arg_mapping& a){ return a.param() == p; });
if(i != args_.rend()) return i->repeat() + 1;
return 0;
}
//---------------------------------------------------------------
const group* root_;
scoped_dfs_traverser pos_;
arg_index index_;
arg_index eaten_;
arg_mappings args_;
miss_candidates missCand_;
bool blocked_;
};
/*************************************************************************//**
*
* @brief contains argument -> parameter mappings
* and missing parameters
*
*****************************************************************************/
class parsing_result
{
public:
using arg_mapping = parser::arg_mapping;
using arg_mappings = parser::arg_mappings;
using missing_event = parser::missing_event;
using missing_events = parser::missing_events;
using iterator = arg_mappings::const_iterator;
//-----------------------------------------------------
/** @brief default: empty redult */
parsing_result() = default;
parsing_result(arg_mappings arg2param, missing_events misses):
arg2param_{std::move(arg2param)}, missing_{std::move(misses)}
{}
//-----------------------------------------------------
/** @brief returns number of arguments that could not be mapped to
* a parameter
*/
arg_mappings::size_type
unmapped_args_count() const noexcept {
return std::count_if(arg2param_.begin(), arg2param_.end(),
[](const arg_mapping& a){ return !a.param(); });
}
/** @brief returns if any argument could only be matched by an
* unreachable parameter
*/
bool any_blocked() const noexcept {
return std::any_of(arg2param_.begin(), arg2param_.end(),
[](const arg_mapping& a){ return a.blocked(); });
}
/** @brief returns if any argument matched more than one parameter
* that were mutually exclusive */
bool any_conflict() const noexcept {
return std::any_of(arg2param_.begin(), arg2param_.end(),
[](const arg_mapping& a){ return a.conflict(); });
}
/** @brief returns if any parameter matched repeatedly although
* it was not allowed to */
bool any_bad_repeat() const noexcept {
return std::any_of(arg2param_.begin(), arg2param_.end(),
[](const arg_mapping& a){ return a.bad_repeat(); });
}
/** @brief returns true if any parsing error / violation of the
* command line interface definition occured */
bool any_error() const noexcept {
return unmapped_args_count() > 0 || !missing().empty() ||
any_blocked() || any_conflict() || any_bad_repeat();
}
/** @brief returns true if no parsing error / violation of the
* command line interface definition occured */
explicit operator bool() const noexcept { return !any_error(); }
/** @brief access to range of missing parameter match events */
const missing_events& missing() const noexcept { return missing_; }
/** @brief returns non-mutating iterator to position of
* first argument -> parameter mapping */
iterator begin() const noexcept { return arg2param_.begin(); }
/** @brief returns non-mutating iterator to position one past the
* last argument -> parameter mapping */
iterator end() const noexcept { return arg2param_.end(); }
private:
//-----------------------------------------------------
arg_mappings arg2param_;
missing_events missing_;
};
namespace detail {
namespace {
/*************************************************************************//**
*
* @brief correct some common problems
* does not - and MUST NOT - change the number of arguments
* (no insertion, no deletion)
*
*****************************************************************************/
void sanitize_args(arg_list& args)
{
//e.g. {"-o12", ".34"} -> {"-o", "12.34"}
if(args.empty()) return;
for(auto i = begin(args)+1; i != end(args); ++i) {
if(i != begin(args) && i->size() > 1 &&
i->find('.') == 0 && std::isdigit((*i)[1]) )
{
//find trailing digits in previous arg
using std::prev;
auto& prv = *prev(i);
auto fstDigit = std::find_if_not(prv.rbegin(), prv.rend(),
[](arg_string::value_type c){
return std::isdigit(c);
}).base();
//handle leading sign
if(fstDigit > prv.begin() &&
(*prev(fstDigit) == '+' || *prev(fstDigit) == '-'))
{
--fstDigit;
}
//prepend digits from previous arg
i->insert(begin(*i), fstDigit, end(prv));
//erase digits in previous arg
prv.erase(fstDigit, end(prv));
}
}
}
/*************************************************************************//**
*
* @brief executes actions based on a parsing result
*
*****************************************************************************/
void execute_actions(const parsing_result& res)
{
for(const auto& m : res) {
if(m.param()) {
const auto& param = *(m.param());
if(m.repeat() > 0) param.notify_repeated(m.index());
if(m.blocked()) param.notify_blocked(m.index());
if(m.conflict()) param.notify_conflict(m.index());
//main action
if(!m.any_error()) param.execute_actions(m.arg());
}
}
for(auto m : res.missing()) {
if(m.param()) m.param()->notify_missing(m.after_index());
}
}
/*************************************************************************//**
*
* @brief parses input args
*
*****************************************************************************/
static parsing_result
parse_args(const arg_list& args, const group& cli,
arg_index offset = 0)
{
//parse args and store unrecognized arg indices
parser parse{cli, offset};
for(const auto& arg : args) {
parse(arg);
if(!parse.valid()) break;
}
return parsing_result{parse.args(), parse.missed()};
}
/*************************************************************************//**
*
* @brief parses input args & executes actions
*
*****************************************************************************/
static parsing_result
parse_and_execute(const arg_list& args, const group& cli,
arg_index offset = 0)
{
auto result = parse_args(args, cli, offset);
execute_actions(result);
return result;
}
} //anonymous namespace
} // namespace detail
/*************************************************************************//**
*
* @brief parses vector of arg strings and executes actions
*
*****************************************************************************/
inline parsing_result
parse(arg_list args, const group& cli)
{
detail::sanitize_args(args);
return detail::parse_and_execute(args, cli);
}
/*************************************************************************//**
*
* @brief parses initializer_list of C-style arg strings and executes actions
*
*****************************************************************************/
inline parsing_result
parse(std::initializer_list<const char*> arglist, const group& cli)
{
arg_list args;
args.reserve(arglist.size());
for(auto a : arglist) {
if(std::strlen(a) > 0) args.push_back(a);
}
return parse(std::move(args), cli);
}
/*************************************************************************//**
*
* @brief parses range of arg strings and executes actions
*
*****************************************************************************/
template<class InputIterator>
inline parsing_result
parse(InputIterator first, InputIterator last, const group& cli)
{
return parse(arg_list(first,last), cli);
}
/*************************************************************************//**
*
* @brief parses the standard array of command line arguments; omits argv[0]
*
*****************************************************************************/
inline parsing_result
parse(const int argc, char* argv[], const group& cli, arg_index offset = 1)
{
arg_list args;
if(offset < argc) args.assign(argv+offset, argv+argc);
detail::sanitize_args(args);
return detail::parse_and_execute(args, cli, offset);
}
/*************************************************************************//**
*
* @brief filter predicate for parameters and groups;
* Can be used to limit documentation generation to parameter subsets.
*
*****************************************************************************/
class param_filter
{
public:
/** @brief only allow parameters with given prefix */
param_filter& prefix(const arg_string& p) noexcept {