20
20
#include < regex>
21
21
#include < iostream>
22
22
#include < cassert>
23
+ #include < cstddef>
23
24
24
25
using namespace docopt ;
25
26
@@ -119,7 +120,7 @@ bool LeafPattern::match(PatternList& left, std::vector<std::shared_ptr<LeafPatte
119
120
return false ;
120
121
}
121
122
122
- left.erase (left.begin ()+match.first );
123
+ left.erase (left.begin ()+static_cast <std:: ptrdiff_t >( match.first ) );
123
124
124
125
auto same_name = std::find_if (collected.begin (), collected.end (), [&](std::shared_ptr<LeafPattern> const & p) {
125
126
return p->name ()==name ();
@@ -170,7 +171,7 @@ Option Option::parse(std::string const& option_description)
170
171
auto double_space = option_description.find (" " );
171
172
<
10000
code class="diff-text syntax-highlighted-line"> auto options_end = option_description.end ();
172
173
if (double_space != std::string::npos) {
173
- options_end = option_description.begin () + double_space;
174
+ options_end = option_description.begin () + static_cast <std:: ptrdiff_t >( double_space) ;
174
175
}
175
176
176
177
static const std::regex pattern {" (-{1,2})?(.*?)([,= ]|$)" };
@@ -340,7 +341,7 @@ std::pair<size_t, std::shared_ptr<LeafPattern>> Option::single_match(PatternList
340
341
#pragma mark -
341
342
#pragma mark Parsing stuff
342
343
343
- std::vector<PatternList> transform (PatternList pattern);
344
+ static std::vector<PatternList> transform (PatternList pattern);
344
345
345
346
void BranchPattern::fix_repeating_arguments ()
346
347
{
@@ -385,7 +386,7 @@ void BranchPattern::fix_repeating_arguments()
385
386
}
386
387
}
387
388
388
- std::vector<PatternList> transform (PatternList pattern)
389
+ static std::vector<PatternList> transform (PatternList pattern)
389
390
{
390
391
std::vector<PatternList> result;
391
392
@@ -512,7 +513,7 @@ class Tokens {
512
513
std::string the_rest () const {
513
514
if (!*this )
514
515
return {};
515
- return join (fTokens .begin ()+fIndex ,
516
+ return join (fTokens .begin ()+static_cast <std:: ptrdiff_t >( fIndex ) ,
516
517
fTokens .end (),
517
518
" " );
518
519
}
@@ -546,7 +547,7 @@ std::vector<T*> flat_filter(Pattern& pattern) {
546
547
return ret;
547
548
}
548
549
549
- std::vector<std::string> parse_section (std::string const & name, std::string const & source) {
550
+ static std::vector<std::string> parse_section (std::string const & name, std::string const & source) {
550
551
// ECMAScript regex only has "?=" for a non-matching lookahead. In order to make sure we always have
551
552
// a newline to anchor our matching, we have to avoid matching the final newline of each grouping.
552
553
// Therefore, our regex is adjusted from the docopt Python one to use ?= to match the newlines before
@@ -571,7 +572,7 @@ std::vector<std::string> parse_section(std::string const& name, std::string cons
571
572
return ret;
572
573
}
573
574
574
- bool is_argument_spec (std::string const & token) {
575
+ static bool is_argument_spec (std::string const & token) {
575
576
if (token.empty ())
576
577
return false ;
577
578
@@ -593,7 +594,7 @@ std::vector<std::string> longOptions(I iter, I end) {
593
594
return ret;
594
595
}
595
596
596
- PatternList parse_long (Tokens& tokens, std::vector<Option>& options)
597
+ static PatternList parse_long (Tokens& tokens, std::vector<Option>& options)
597
598
{
598
599
// long ::= '--' chars [ ( ' ' | '=' ) chars ] ;
599
600
std::string longOpt, equal;
@@ -665,7 +666,7 @@ PatternList parse_long(Tokens& tokens, std::vector<Option>& options)
665
666
return ret;
666
667
}
667
668
668
- PatternList parse_short (Tokens& tokens, std::vector<Option>& options)
669
+ static PatternList parse_short (Tokens& tokens, std::vector<Option>& options)
669
670
{
670
671
// shorts ::= '-' ( chars )* [ [ ' ' ] chars ] ;
671
672
@@ -706,8 +707,8 @@ PatternList parse_short(Tokens& tokens, std::vector<Option>& options)
706
707
if (o->argCount ()) {
707
708
if (i == token.end ()) {
708
709
// consume the next token
709
- auto const & token = tokens.current ();
710
- if (token .empty () || token ==" --" ) {
710
+ auto const & ttoken = tokens.current ();
711
+ if (ttoken .empty () || ttoken ==" --" ) {
711
712
std::string error = shortOpt + " requires an argument" ;
712
713
throw Tokens::OptionError (std::move (error));
713
714
}
@@ -729,9 +730,9 @@ PatternList parse_short(Tokens& tokens, std::vector<Option>& options)
729
730
return ret;
730
731
}
731
732
732
- PatternList parse_expr (Tokens& tokens, std::vector<Option>& options);
733
+ static PatternList parse_expr (Tokens& tokens, std::vector<Option>& options);
733
734
734
- PatternList parse_atom (Tokens& tokens, std::vector<Option>& options)
735
+ static PatternList parse_atom (Tokens& tokens, std::vector<Option>& options)
735
736
{
736
737
// atom ::= '(' expr ')' | '[' expr ']' | 'options'
737
738
// | long | shorts | argument | command ;
@@ -778,7 +779,7 @@ PatternList parse_atom(Tokens& tokens, std::vector<Option>& options)
778
779
return ret;
779
780
}
780
781
781
- PatternList parse_seq (Tokens& tokens, std::vector<Option>& options)
782
+ static PatternList parse_seq (Tokens& tokens, std::vector<Option>& options)
782
783
{
783
784
// seq ::= ( atom [ '...' ] )* ;"""
784
785
@@ -802,15 +803,15 @@ PatternList parse_seq(Tokens& tokens, std::vector<Option>& options)
802
803
return ret;
803
804
}
804
805
805
- std::shared_ptr<Pattern> maybe_collapse_to_required (PatternList&& seq)
806
+ static std::shared_ptr<Pattern> maybe_collapse_to_required (PatternList&& seq)
806
807
{
807
808
if (seq.size ()==1 ) {
808
809
return std::move (seq[0 ]);
809
810
}
810
811
return std::make_shared<Required>(std::move (seq));
811
812
}
812
813
813
- std::shared_ptr<Pattern> maybe_collapse_to_either (PatternList&& seq)
814
+ static std::shared_ptr<Pattern> maybe_collapse_to_either (PatternList&& seq)
814
815
{
815
816
if (seq.size ()==1 ) {
816
817
return std::move (seq[0 ]);
@@ -839,7 +840,7 @@ PatternList parse_expr(Tokens& tokens, std::vector<Option>& options)
839
840
return { maybe_collapse_to_either (std::move (ret)) };
840
841
}
841
842
842
- Required parse_pattern (std::string const & source, std::vector<Option>& options)
843
+ static Required parse_pattern (std::string const & source, std::vector<Option>& options)
843
844
{
844
845
auto tokens = Tokens::from_pattern (source);
845
846
auto result = parse_expr (tokens, options);
@@ -852,25 +853,25 @@ Required parse_pattern(std::string const& source, std::vector<Option>& options)
852
853
}
853
854
854
855
855
- std::string formal_usage (std::string const & section) {
856
+ static std::string formal_usage (std::string const & section) {
856
857
std::string ret = " (" ;
857
858
858
859
auto i = section.find (' :' )+1 ; // skip past "usage:"
859
860
auto parts = split (section, i);
860
- for (size_t i = 1 ; i < parts.size (); ++i ) {
861
- if (parts[i ] == parts[0 ]) {
861
+ for (size_t ii = 1 ; ii < parts.size (); ++ii ) {
862
+ if (parts[ii ] == parts[0 ]) {
862
863
ret += " ) | (" ;
863
864
} else {
864
865
ret.push_back (' ' );
865
- ret += parts[i ];
866
+ ret += parts[ii ];
866
867
}
867
868
}
868
869
869
870
ret += " )" ;
870
871
return ret;
871
872
}
872
873
873
- PatternList parse_argv (Tokens tokens, std::vector<Option>& options, bool options_first)
874
+ static PatternList parse_argv (Tokens tokens, std::vector<Option>& options, bool options_first)
874
875
{
875
876
// Parse command-line argument vector.
876
877
//
@@ -907,7 +908,7 @@ PatternList parse_argv(Tokens tokens, std::vector<Option>& options, bool options
907
908
return ret;
908
909
}
909
910
910
- std::vector<Option> parse_defaults (std::string const & doc) {
911
+ static std::vector<Option> parse_defaults (std::string const & doc) {
911
912
// This pattern is a bit more complex than the python docopt one due to lack of
912
913
// re.split. Effectively, it grabs any line with leading whitespace and then a
913
914
// hyphen; it stops grabbing when it hits another line that also looks like that.
@@ -920,7 +921,7 @@ std::vector<Option> parse_defaults(std::string const& doc) {
920
921
std::vector<Option> defaults;
921
922
922
923
for (auto s : parse_section (" options:" , doc)) {
923
- s.erase (s.begin (), s.begin ()+s.find (' :' )+1 ); // get rid of "options:"
924
+ s.erase (s.begin (), s.begin ()+static_cast <std:: ptrdiff_t >( s.find (' :' ) )+1 ); // get rid of "options:"
924
925
925
926
std::for_each (std::sregex_iterator{ s.begin (), s.end (), pattern },
926
927
std::sregex_iterator{},
@@ -937,7 +938,7 @@ std::vector<Option> parse_defaults(std::string const& doc) {
937
938
return defaults;
938
939
}
939
940
940
- bool isOptionSet (PatternList const & options, std::string const & opt1, std::string const & opt2 = " " ) {
941
+ static bool isOptionSet (PatternList const & options, std::string const & opt1, std::string const & opt2 = " " ) {
941
942
return std::any_of (options.begin (), options.end (), [&](std::shared_ptr<Pattern const > const & opt) -> bool {
942
943
auto const & name = opt->name ();
943
944
if (name==opt1 || (!opt2.empty () && name==opt2)) {
@@ -947,7 +948,7 @@ bool isOptionSet(PatternList const& options, std::string const& opt1, std::strin
947
948
});
948
949
}
949
950
950
- void extras (bool help, bool version, PatternList const & options) {
951
+ static void extras (bool help, bool version, PatternList const & options) {
951
952
if (help && isOptionSet (options, " -h" , " --help" )) {
952
953
throw DocoptExitHelp ();
953
954
}
@@ -958,7 +959,7 @@ void extras(bool help, bool version, PatternList const& options) {
958
959
}
959
960
960
961
// Parse the doc string and generate the Pattern tree
961
- std::pair<Required, std::vector<Option>> create_pattern_tree (std::string const & doc)
962
+ static std::pair<Required, std::vector<Option>> create_pattern_tree (std::string const & doc)
962
963
{
963
964
auto usage_sections = parse_section (" usage:" , doc);
964
965
if (usage_sections.empty ()) {
0 commit comments