Newer
Older
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
prefix_ = p; return *this;
}
/** @brief only allow parameters with given prefix */
param_filter& prefix(arg_string&& p) noexcept {
prefix_ = std::move(p); return *this;
}
const arg_string& prefix() const noexcept { return prefix_; }
/** @brief only allow parameters with given requirement status */
param_filter& required(tri t) noexcept { required_ = t; return *this; }
tri required() const noexcept { return required_; }
/** @brief only allow parameters with given blocking status */
param_filter& blocking(tri t) noexcept { blocking_ = t; return *this; }
tri blocking() const noexcept { return blocking_; }
/** @brief only allow parameters with given repeatable status */
param_filter& repeatable(tri t) noexcept { repeatable_ = t; return *this; }
tri repeatable() const noexcept { return repeatable_; }
/** @brief only allow parameters with given docstring status */
param_filter& has_doc(tri t) noexcept { hasDoc_ = t; return *this; }
tri has_doc() const noexcept { return hasDoc_; }
/** @brief returns true, if parameter satisfies all filters */
bool operator() (const parameter& p) const noexcept {
if(!prefix_.empty()) {
if(!std::any_of(p.flags().begin(), p.flags().end(),
[&](const arg_string& flag){
return str::has_prefix(flag, prefix_);
})) return false;
}
if(required() != p.required()) return false;
if(blocking() != p.blocking()) return false;
if(repeatable() != p.repeatable()) return false;
if(has_doc() != !p.doc().empty()) return false;
return true;
}
private:
arg_string prefix_;
tri required_ = tri::either;
tri blocking_ = tri::either;
tri repeatable_ = tri::either;
tri exclusive_ = tri::either;
tri hasDoc_ = tri::yes;
};
/*************************************************************************//**
*
* @brief documentation formatting options
*
*****************************************************************************/
class doc_formatting
{
public:
using string = doc_string;
/** @brief determines column where documentation printing starts */
doc_formatting& start_column(int col) { startCol_ = col; return *this; }
int start_column() const noexcept { return startCol_; }
/** @brief determines column where docstrings start */
doc_formatting& doc_column(int col) { docCol_ = col; return *this; }
int doc_column() const noexcept { return docCol_; }
/** @brief determines indent of documentation lines
* for children of a documented group */
doc_formatting& indent_size(int indent) { indentSize_ = indent; return *this; }
int indent_size() const noexcept { return indentSize_; }
/** @brief determines string to be used
* if a parameter has no flags and no label */
doc_formatting& empty_label(const string& label) {
emptyLabel_ = label;
return *this;
}
const string& empty_label() const noexcept { return emptyLabel_; }
/** @brief determines string for separating parameters */
doc_formatting& param_separator(const string& sep) {
paramSep_ = sep;
return *this;
}
const string& param_separator() const noexcept { return paramSep_; }
/** @brief determines string for separating groups (in usage lines) */
doc_formatting& group_separator(const string& sep) {
groupSep_ = sep;
return *this;
}
const string& group_separator() const noexcept { return groupSep_; }
/** @brief determines string for separating alternative parameters */
doc_formatting& alternative_param_separator(const string& sep) {
altParamSep_ = sep;
return *this;
}
const string& alternative_param_separator() const noexcept { return altParamSep_; }
/** @brief determines string for separating alternative groups */
doc_formatting& alternative_group_separator(const string& sep) {
altGroupSep_ = sep;
return *this;
}
const string& alternative_group_separator() const noexcept { return altGroupSep_; }
/** @brief determines string for separating flags of the same parameter */
doc_formatting& flag_separator(const string& sep) {
flagSep_ = sep;
return *this;
}
const string& flag_separator() const noexcept { return flagSep_; }
/** @brief determnines strings surrounding parameter labels */
doc_formatting&
surround_labels(const string& prefix, const string& postfix) {
labelPre_ = prefix;
labelPst_ = postfix;
return *this;
}
const string& label_prefix() const noexcept { return labelPre_; }
const string& label_postfix() const noexcept { return labelPst_; }
/** @brief determnines strings surrounding optional parameters/groups */
doc_formatting&
surround_optional(const string& prefix, const string& postfix) {
optionPre_ = prefix;
optionPst_ = postfix;
return *this;
}
const string& optional_prefix() const noexcept { return optionPre_; }
const string& optional_postfix() const noexcept { return optionPst_; }
/** @brief determnines strings surrounding repeatable parameters/groups */
doc_formatting&
surround_repeat(const string& prefix, const string& postfix) {
repeatPre_ = prefix;
repeatPst_ = postfix;
return *this;
}
const string& repeat_prefix() const noexcept { return repeatPre_; }
const string& repeat_postfix() const noexcept { return repeatPst_; }
/** @brief determnines strings surrounding exclusive groups */
doc_formatting&
surround_alternatives(const string& prefix, const string& postfix) {
alternPre_ = prefix;
alternPst_ = postfix;
return *this;
}
const string& alternatives_prefix() const noexcept { return alternPre_; }
const string& alternatives_postfix() const noexcept { return alternPst_; }
/** @brief determnines strings surrounding alternative flags */
doc_formatting&
surround_alternative_flags(const string& prefix, const string& postfix) {
alternFlagPre_ = prefix;
alternFlagPst_ = postfix;
return *this;
}
const string& alternative_flags_prefix() const noexcept { return alternFlagPre_; }
const string& alternative_flags_postfix() const noexcept { return alternFlagPst_; }
/** @brief determnines strings surrounding non-exclusive groups */
doc_formatting&
surround_group(const string& prefix, const string& postfix) {
groupPre_ = prefix;
groupPst_ = postfix;
return *this;
}
const string& group_prefix() const noexcept { return groupPre_; }
const string& group_postfix() const noexcept { return groupPst_; }
/** @brief determnines strings surrounding joinable groups */
doc_formatting&
surround_joinable(const string& prefix, const string& postfix) {
joinablePre_ = prefix;
joinablePst_ = postfix;
return *this;
}
const string& joinable_prefix() const noexcept { return joinablePre_; }
const string& joinable_postfix() const noexcept { return joinablePst_; }
/** @brief determines maximum number of flags per parameter to be printed
* in detailed parameter documentation lines */
doc_formatting& max_flags_per_param_in_doc(int max) {
maxAltInDocs_ = max > 0 ? max : 0;
return *this;
}
int max_flags_per_param_in_doc() const noexcept { return maxAltInDocs_; }
/** @brief determines maximum number of flags per parameter to be printed
* in usage lines */
doc_formatting& max_flags_per_param_in_usage(int max) {
maxAltInUsage_ = max > 0 ? max : 0;
return *this;
}
int max_flags_per_param_in_usage() const noexcept { return maxAltInUsage_; }
/** @brief determines number of empty rows after one single-line
* documentation entry */
doc_formatting& line_spacing(int lines) {
lineSpc_ = lines > 0 ? lines : 0;
return *this;
}
int line_spacing() const noexcept { return lineSpc_; }
/** @brief determines number of empty rows before and after a paragraph;
* a paragraph is defined by a documented group or if
* a parameter documentation entry used more than one line */
doc_formatting& paragraph_spacing(int lines) {
paragraphSpc_ = lines > 0 ? lines : 0;
return *this;
}
int paragraph_spacing() const noexcept { return paragraphSpc_; }
/** @brief determines if alternative flags with a common prefix should
* be printed in a merged fashion */
doc_formatting& merge_alternative_flags_with_common_prefix(bool yes = true) {
mergeAltCommonPfx_ = yes;
return *this;
}
bool merge_alternative_flags_with_common_prefix() const noexcept {
return mergeAltCommonPfx_;
}
/** @brief determines if joinable flags with a common prefix should
* be printed in a merged fashion */
doc_formatting& merge_joinable_with_common_prefix(bool yes = true) {
mergeJoinableCommonPfx_ = yes;
return *this;
}
bool merge_joinable_with_common_prefix() const noexcept {
return mergeJoinableCommonPfx_;
}
/** @brief determines if children of exclusive groups should be printed
* on individual lines if the exceed 'alternatives_min_split_size'
*/
doc_formatting& split_alternatives(bool yes = true) {
splitTopAlt_ = yes;
return *this;
}
bool split_alternatives() const noexcept {
return splitTopAlt_;
}
/** @brief determines how many children exclusive groups can have before
* their children are printed on individual usage lines */
doc_formatting& alternatives_min_split_size(int size) {
groupSplitSize_ = size > 0 ? size : 0;
return *this;
}
int alternatives_min_split_size() const noexcept { return groupSplitSize_; }
private:
string paramSep_ = string(" ");
string groupSep_ = string(" ");
string altParamSep_ = string("|");
string altGroupSep_ = string(" | ");
string flagSep_ = string(", ");
string labelPre_ = string("<");
string labelPst_ = string(">");
string optionPre_ = string("[");
string optionPst_ = string("]");
string repeatPre_ = string("");
string repeatPst_ = string("...");
string groupPre_ = string("(");
string groupPst_ = string(")");
string alternPre_ = string("(");
string alternPst_ = string(")");
string alternFlagPre_ = string("");
string alternFlagPst_ = string("");
string joinablePre_ = string("(");
string joinablePst_ = string(")");
string emptyLabel_ = string("");
int startCol_ = 8;
int docCol_ = 20;
int indentSize_ = 4;
int maxAltInUsage_ = 1;
int maxAltInDocs_ = 32;
int lineSpc_ = 0;
int paragraphSpc_ = 1;
int groupSplitSize_ = 3;
bool splitTopAlt_ = true;
bool mergeAltCommonPfx_ = false;
bool mergeJoinableCommonPfx_ = true;
};
/*************************************************************************//**
*
* @brief generates usage lines
*
* @details lazily evaluated
*
*****************************************************************************/
class usage_lines
{
public:
using string = doc_string;
usage_lines(const group& params, string prefix = "",
const doc_formatting& fmt = doc_formatting{})
:
params_(params), fmt_(fmt), prefix_(std::move(prefix))
{
if(!prefix_.empty()) prefix_ += ' ';
if(fmt_.start_column() > 0) prefix_.insert(0, fmt.start_column(), ' ');
}
usage_lines(const group& params, const doc_formatting& fmt):
usage_lines(params, "", fmt)
{}
usage_lines& ommit_outermost_group_surrounders(bool yes) {
ommitOutermostSurrounders_ = yes;
return *this;
}
bool ommit_outermost_group_surrounders() const {
return ommitOutermostSurrounders_;
}
template<class OStream>
inline friend OStream& operator << (OStream& os, const usage_lines& p) {
p.print_usage(os);
return os;
}
string str() const {
std::ostringstream os; os << *this; return os.str();
}
private:
const group& params_;
doc_formatting fmt_;
string prefix_;
bool ommitOutermostSurrounders_ = false;
//-----------------------------------------------------
struct context {
group::depth_first_traverser pos;
std::stack<string> separators;
std::stack<string> postfixes;
int level = 0;
const group* outermost = nullptr;
bool linestart = false;
bool useOutermost = true;
int line = 0;
bool is_singleton() const noexcept {
return linestart && pos.is_last_in_path();
}
bool is_alternative() const noexcept {
return pos.parent().exclusive();
}
};
/***************************************************************//**
*
* @brief writes usage text for command line parameters
*
*******************************************************************/
template<class OStream>
void print_usage(OStream& os) const
{
context cur;
cur.pos = params_.begin_dfs();
cur.linestart = true;
cur.level = cur.pos.level();
cur.outermost = ¶ms_;
print_usage(os, cur, prefix_);
}
/***************************************************************//**
*
* @brief writes usage text for command line parameters
*
* @param prefix all that goes in front of current things to print
*
*******************************************************************/
template<class OStream>
void print_usage(OStream& os, context cur, string prefix) const
{
if(!cur.pos) return;
std::ostringstream buf;
if(cur.linestart) buf << prefix;
const auto initPos = buf.tellp();
cur.level = cur.pos.level();
if(cur.useOutermost) {
//we cannot start outside of the outermost group
//so we have to treat it separately
start_group(buf, cur.pos.parent(), cur);
if(!cur.pos) {
os << buf.str();
return;
}
}
else {
//don't visit siblings of starter node
cur.pos.skip_siblings();
}
check_end_group(buf, cur);
do {
if(buf.tellp() > initPos) cur.linestart = false;
if(!cur.linestart && !cur.pos.is_first_in_group()) {
buf << cur.separators.top();
}
if(cur.pos->is_group()) {
start_group(buf, cur.pos->as_group(), cur);
if(!cur.pos) {
os << buf.str();
return;
}
}
else {
buf << param_label(cur.pos->as_param(), cur);
++cur.pos;
}
check_end_group(buf, cur);
} while(cur.pos);
os << buf.str();
}
/***************************************************************//**
*
* @brief handles pattern group surrounders and separators
* and alternative splitting
*
*******************************************************************/
void start_group(std::ostringstream& os,
const group& group, context& cur) const
{
//does cur.pos already point to a member or to group itself?
//needed for special treatment of outermost group
const bool alreadyInside = &(cur.pos.parent()) == &group;
auto lbl = joined_label(group, cur);
if(!lbl.empty()) {
os << lbl;
cur.linestart = false;
//skip over entire group as its label has already been created
if(alreadyInside) {
cur.pos.next_after_siblings();
} else {
cur.pos.next_sibling();
}
}
else {
const bool splitAlternatives = group.exclusive() &&
fmt_.split_alternatives() &&
std::any_of(group.begin(), group.end(),
[this](const pattern& p) {
return int(p.param_count()) >= fmt_.alternatives_min_split_size();
});
if(splitAlternatives) {
cur.postfixes.push("");
cur.separators.push("");
//recursively print alternative paths in decision-DAG
//enter group?
if(!alreadyInside) ++cur.pos;
cur.linestart = true;
cur.useOutermost = false;
auto pfx = os.str();
os.str("");
//print paths in DAG starting at each group member
for(std::size_t i = 0; i < group.size(); ++i) {
std::stringstream buf;
cur.outermost = cur.pos->is_group() ? &(cur.pos->as_group()) : nullptr;
print_usage(buf, cur, pfx);
if(buf.tellp() > int(pfx.size())) {
os << buf.str();
if(i < group.size()-1) {
if(cur.line > 0) {
os << string(fmt_.line_spacing(), '\n');
}
++cur.line;
os << '\n';
}
}
cur.pos.next_sibling(); //do not descend into memebers
}
cur.pos.invalidate(); //signal end-of-path
return;
}
else {
//pre & postfixes, separators
auto surround = group_surrounders(group, cur);
os << surround.first;
cur.postfixes.push(std::move(surround.second));
cur.separators.push(group_separator(group, fmt_));
//descend into group?
if(!alreadyInside) ++cur.pos;
}
}
cur.level = cur.pos.level();
}
/***************************************************************//**
*
*******************************************************************/
void check_end_group(std::ostringstream& os, context& cur) const
{
for(; cur.level > cur.pos.level(); --cur.level) {
os << cur.postfixes.top();
cur.postfixes.pop();
cur.separators.pop();
}
cur.level = cur.pos.level();
}
/***************************************************************//**
*
* @brief makes usage label for one command line parameter
*
*******************************************************************/
string param_label(const parameter& p, const context& cur) const
{
const auto& parent = cur.pos.parent();
const bool startsOptionalSequence =
parent.size() > 1 && p.blocking() && cur.pos.is_first_in_group();
const bool outermost =
ommitOutermostSurrounders_ && cur.outermost == &parent;
const bool showopt = !cur.is_alternative() && !p.required()
&& !startsOptionalSequence && !outermost;
const bool showrep = p.repeatable() && !outermost;
string lbl;
if(showrep) lbl += fmt_.repeat_prefix();
if(showopt) lbl += fmt_.optional_prefix();
const auto& flags = p.flags();
if(!flags.empty()) {
const int n = std::min(fmt_.max_flags_per_param_in_usage(),
int(flags.size()));
const bool surrAlt = n > 1 && !showopt && !cur.is_singleton();
if(surrAlt) lbl += fmt_.alternative_flags_prefix();
bool sep = false;
for(int i = 0; i < n; ++i) {
if(sep) {
if(cur.is_singleton())
lbl += fmt_.alternative_group_separator();
else
lbl += fmt_.flag_separator();
}
lbl += flags[i];
sep = true;
}
if(surrAlt) lbl += fmt_.alternative_flags_postfix();
}
else {
if(!p.label().empty()) {
lbl += fmt_.label_prefix()
+ p.label()
+ fmt_.label_postfix();
} else if(!fmt_.empty_label().empty()) {
lbl += fmt_.label_prefix()
+ fmt_.empty_label()
+ fmt_.label_postfix();
} else {
return "";
}
}
if(showopt) lbl += fmt_.optional_postfix();
if(showrep) lbl += fmt_.repeat_postfix();
return lbl;
}
/***************************************************************//**
*
* @brief prints flags in one group in a merged fashion
*
*******************************************************************/
string joined_label(const group& params, const context& cur) const
{
if(!fmt_.merge_alternative_flags_with_common_prefix() &&
!fmt_.merge_joinable_with_common_prefix()) return "";
const bool flagsonly = std::all_of(params.begin(), params.end(),
[](const pattern& p){
return p.is_param() && !p.as_param().flags().empty();
});
if(!flagsonly) return "";
const bool showOpt = params.all_optional() &&
!(ommitOutermostSurrounders_ && cur.outermost == ¶ms);
auto pfx = params.common_flag_prefix();
if(pfx.empty()) return "";
const auto n = pfx.size();
if(params.exclusive() &&
fmt_.merge_alternative_flags_with_common_prefix())
{
string lbl;
if(showOpt) lbl += fmt_.optional_prefix();
lbl += pfx + fmt_.alternatives_prefix();
bool first = true;
for(const auto& p : params) {
if(p.is_param()) {
if(first)
first = false;
else
lbl += fmt_.alternative_param_separator();
lbl += p.as_param().flags().front().substr(n);
}
}
lbl += fmt_.alternatives_postfix();
if(showOpt) lbl += fmt_.optional_postfix();
return lbl;
}
//no alternatives, but joinable flags
else if(params.joinable() &&
fmt_.merge_joinable_with_common_prefix())
{
const bool allSingleChar = std::all_of(params.begin(), params.end(),
[&](const pattern& p){
return p.is_param() &&
p.as_param().flags().front().substr(n).size() == 1;
});
if(allSingleChar) {
string lbl;
if(showOpt) lbl += fmt_.optional_prefix();
lbl += pfx;
for(const auto& p : params) {
if(p.is_param())
lbl += p.as_param().flags().front().substr(n);
}
if(showOpt) lbl += fmt_.optional_postfix();
return lbl;
}
}
return "";
}
/***************************************************************//**
*
* @return symbols with which to surround a group
*
*******************************************************************/
std::pair<string,string>
group_surrounders(const group& group, const context& cur) const
{
string prefix;
string postfix;
const bool isOutermost = &group == cur.outermost;
if(isOutermost && ommitOutermostSurrounders_)
return {string{}, string{}};
if(group.exclusive()) {
if(group.all_optional()) {
prefix = fmt_.optional_prefix();
postfix = fmt_.optional_postfix();
if(group.all_flagless()) {
prefix += fmt_.label_prefix();
postfix = fmt_.label_prefix() + postfix;
}
} else if(group.all_flagless()) {
prefix = fmt_.label_prefix();
postfix = fmt_.label_postfix();
} else if(!cur.is_singleton() || !isOutermost) {
prefix = fmt_.alternatives_prefix();
postfix = fmt_.alternatives_postfix();
}
}
else if(group.size() > 1 &&
group.front().blocking() && !group.front().required())
{
prefix = fmt_.optional_prefix();
postfix = fmt_.optional_postfix();
}
else if(group.size() > 1 && cur.is_alternative() &&
&group != cur.outermost)
{
prefix = fmt_.group_prefix();
postfix = fmt_.group_postfix();
}
else if(!group.exclusive() &&
group.joinable() && !cur.linestart)
{
prefix = fmt_.joinable_prefix();
postfix = fmt_.joinable_postfix();
}
if(group.repeatable()) {
if(prefix.empty()) prefix = fmt_.group_prefix();
prefix = fmt_.repeat_prefix() + prefix;
if(postfix.empty()) postfix = fmt_.group_postfix();
postfix += fmt_.repeat_postfix();
}
return {std::move(prefix), std::move(postfix)};
}
/***************************************************************//**
*
* @return symbol that separates members of a group
*
*******************************************************************/
static string
group_separator(const group& group, const doc_formatting& fmt)
{
const bool only1ParamPerMember = std::all_of(group.begin(), group.end(),
[](const pattern& p) { return p.param_count() < 2; });
if(only1ParamPerMember) {
if(group.exclusive()) {
return fmt.alternative_param_separator();
} else {
return fmt.param_separator();
}
}
else { //there is at least one large group inside
if(group.exclusive()) {
return fmt.alternative_group_separator();
} else {
return fmt.group_separator();
}
}
}
};
/*************************************************************************//**
*
* @brief generates parameter and group documentation from docstrings
*
* @details lazily evaluated
*
*****************************************************************************/
class documentation
{
public:
using string = doc_string;
documentation(const group& cli,
const doc_formatting& fmt = doc_formatting{},
const param_filter& filter = param_filter{})
:
cli_(cli), fmt_{fmt}, usgFmt_{fmt}, filter_{filter}
{
//necessary, because we re-use "usage_lines" to generate
//labels for documented groups
usgFmt_.max_flags_per_param_in_usage(
usgFmt_.max_flags_per_param_in_doc());
}
documentation(const group& params,
const param_filter& filter,
const doc_formatting& fmt = doc_formatting{})
:
documentation(params, fmt, filter)
{}
template<class OStream>
inline friend OStream& operator << (OStream& os, const documentation& p) {
printed prn = printed::nothing;
p.print_doc(os, p.cli_, prn);
return os;
}
string str() const {
std::ostringstream os; os << *this; return os.str();
}
private:
using dfs_traverser = group::depth_first_traverser;
enum class printed { nothing, line, paragraph };
const group& cli_;
doc_formatting fmt_;
doc_formatting usgFmt_;
param_filter filter_;
/***************************************************************//**
*
* @brief writes full documentation text for command line parameters
*
*******************************************************************/
template<class OStream>
void print_doc(OStream& os, const group& params,
printed& sofar,
int indentLvl = 0) const
{
if(params.empty()) return;
//if group itself doesn't have docstring
if(params.doc().empty()) {
for(const auto& p : params) {
print_doc(os, p, sofar, indentLvl);
}
}
else { //group itself does have docstring
bool anyDocInside = std::any_of(params.begin(), params.end(),
[](const pattern& p){ return !p.doc().empty(); });
if(anyDocInside) { //group docstring as title, then child entries
if(sofar != printed::nothing) {
os << string(fmt_.paragraph_spacing() + 1, '\n');
}
auto indent = string(fmt_.start_column(), ' ');
if(indentLvl > 0) indent += string(fmt_.indent_size() * indentLvl, ' ');
os << indent << params.doc() << '\n';
sofar = printed::nothing;
for(const auto& p : params) {
print_doc(os, p, sofar, indentLvl + 1);
}
sofar = printed::paragraph;
}
else { //group label first then group docstring
auto lbl = usage_lines(params, usgFmt_)
.ommit_outermost_group_surrounders(true).str();
str::trim(lbl);
print_entry(os, lbl, params.doc(), fmt_, sofar, indentLvl);
}
}
}
/***************************************************************//**
*
* @brief writes documentation text for one group or parameter
*
*******************************************************************/
template<class OStream>
void print_doc(OStream& os, const pattern& ptrn,
printed& sofar, int indentLvl) const
{
if(ptrn.is_group()) {
print_doc(os, ptrn.as_group(), sofar, indentLvl);
}
else {
const auto& p = ptrn.as_param();
if(!filter_(p)) return;
print_entry(os, param_label(p, fmt_), p.doc(), fmt_, sofar, indentLvl);
}
}
/*********************************************************************//**
*
* @brief prints one entry = label + docstring
*
************************************************************************/
template<class OStream>
static void
print_entry(OStream& os,
const string& label, const string& docstr,
const doc_formatting& fmt, printed& sofar, int indentLvl)
{
if(label.empty()) return;
auto indent = string(fmt.start_column(), ' ');
if(indentLvl > 0) indent += string(fmt.indent_size() * indentLvl, ' ');
const auto len = int(indent.size() + label.size());
const bool oneline = len < fmt.doc_column();
if(oneline) {
if(sofar == printed::line)
os << string(fmt.line_spacing() + 1, '\n');
else if(sofar == printed::paragraph)
os << string(fmt.paragraph_spacing() + 1, '\n');
}
else if(sofar != printed::nothing) {
os << string(fmt.paragraph_spacing() + 1, '\n');
}
sofar = oneline ? printed::line : printed::paragraph;
os << indent << label;
if(!docstr.empty()) {
if(oneline) {
os << string(fmt.doc_column() - len, ' ');
} else {
os << '\n' << string(fmt.doc_column(), ' ');
}
os << docstr;
}
}
/*********************************************************************//**
*
* @brief makes label for one parameter
*
************************************************************************/
static doc_string
param_label(const parameter& param, const doc_formatting& fmt)
{
doc_string lbl;
if(param.repeatable()) lbl += fmt.repeat_prefix();
const auto& flags = param.flags();
if(!flags.empty()) {
lbl += flags[0];
const int n = std::min(fmt.max_flags_per_param_in_doc(),
int(flags.size()));
for(int i = 1; i < n; ++i) {
lbl += fmt.flag_separator() + flags[i];
}
}
else if(!param.label().empty() || !fmt.empty_label().empty()) {
lbl += fmt.label_prefix();
if(!param.label().empty()) {
lbl += param.label();
} else {
lbl += fmt.empty_label();
}
lbl += fmt.label_postfix();
}
if(param.repeatable()) lbl += fmt.repeat_postfix();
return lbl;
}
};
/*************************************************************************//**
*
* @brief stores strings for man page sections
*
*****************************************************************************/
class man_page
{
public:
//---------------------------------------------------------------
using string = doc_string;
//---------------------------------------------------------------
/** @brief man page section */
class section {
public:
using string = doc_string;
section(string stitle, string scontent):
title_{std::move(stitle)}, content_{std::move(scontent)}
{}
const string& title() const noexcept { return title_; }
const string& content() const noexcept { return content_; }
private:
string title_;
string content_;
};
private:
using section_store = std::vector<section>;