Skip to content

Commit

Permalink
fixed problem with last parameters in group blocking other parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
muellan committed Dec 7, 2017
1 parent 0faf549 commit a5cab3d
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 16 deletions.
66 changes: 50 additions & 16 deletions include/clipp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3857,24 +3857,38 @@ class scoped_dfs_traverser
//discard other alternatives
match.pos_.skip_alternatives();
}
//if current param is not repeatable, go directly to next
if(!match->repeatable() && !match->is_group()) {
curMatched_ = false;
++match.pos_;
}
else {
curMatched_ = true;
}

if(match.pos_.level() > pos_.level()) {
scopes_.push(pos_.undo_point());
pos_ = std::move(match.pos_);
}
else if(match.pos_.level() < pos_.level()) {
return_to_level(match.pos_.level());
if(is_last_in_current_scope(match.pos_)) {
//if current param is not repeatable -> back to previous scope
if(!match->repeatable() && !match->is_group()) {
curMatched_ = false;
pos_ = std::move(match.pos_);
if(!scopes_.empty()) pos_.undo(scopes_.top());
}
else { //stay at match position
curMatched_ = true;
pos_ = std::move(match.pos_);
}
}
else {
pos_ = std::move(match.pos_);
else { //not last in current group
//if current param is not repeatable, go directly to next
if(!match->repeatable() && !match->is_group()) {
curMatched_ = false;
++match.pos_;
} else {
curMatched_ = true;
}

if(match.pos_.level() > pos_.level()) {
scopes_.push(pos_.undo_point());
pos_ = std::move(match.pos_);
}
else if(match.pos_.level() < pos_.level()) {
return_to_level(match.pos_.level());
}
else {
pos_ = std::move(match.pos_);
}
}
posAfterLastMatch_ = pos_;
}
Expand All @@ -3888,6 +3902,16 @@ class scoped_dfs_traverser
}

private:
//-----------------------------------------------------
bool is_last_in_current_scope(const dfs_traverser& pos)
{
if(scopes_.empty()) return pos.is_last_in_path();
//check if we would leave the current scope on ++
auto p = pos;
++p;
return p.level() < scopes_.top().level();
}

//-----------------------------------------------------
void check_repeat_group_start(const scoped_dfs_traverser& newMatch)
{
Expand Down Expand Up @@ -6109,6 +6133,16 @@ inline doc_string doc_label(const parameter& p)
return doc_string{"<?>"};
}

inline doc_string doc_label(const group&)
{
return "<group>";
}

inline doc_string doc_label(const pattern& p)
{
return p.is_group() ? doc_label(p.as_group()) : doc_label(p.as_param());
}


/*************************************************************************//**
*
Expand Down
24 changes: 24 additions & 0 deletions test/required_params_test.cpp → test/required_params_test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,23 @@ void test(int lineNo,
active v;
errors m;
auto cli = (
option("-r") & opt_value("R")
,
required("-a", "--aaa").set(v.a).if_missing(set(m.a))
& value("I", v.av).if_missing(set(m.av))
,
required("-b", "--bee").set(v.b).if_missing(set(m.b))
& value("J", v.bv).if_missing(set(m.bv))
,
option("-s") & opt_value("S")
,
required("-c", "--cee").set(v.c).if_missing(set(m.c))
& opt_value("K", v.cv).if_missing(set(m.cv))
,
required("-d", "--dee").set(v.d).if_missing(set(m.d))
& value("L", v.dv).if_missing(set(m.dv))
,
option("-t") & value("T")
);

run_wrapped_variants({ __FILE__, lineNo }, args, cli,
Expand Down Expand Up @@ -146,6 +152,24 @@ int main()
test(__LINE__, {"-d", "5", "-a", "2", "-c", "4", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-c", "4", "-a", "2", "-d", "5", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"-r", "R", "-a", "2", "-b", "3", "-c", "4", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-c", "4", "-r", "R", "-b", "3", "-a", "2", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-d", "5", "-c", "4", "-r", "R", "-b", "3", "-a", "2"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-d", "5", "-a", "2", "-c", "4", "-r", "R", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-c", "4", "-a", "2", "-d", "5", "-b", "3", "-r", "R"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"-s", "S", "-a", "2", "-b", "3", "-c", "4", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-c", "4", "-s", "S", "-b", "3", "-a", "2", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-d", "5", "-c", "4", "-s", "S", "-b", "3", "-a", "2"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-d", "5", "-a", "2", "-c", "4", "-s", "S", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-c", "4", "-a", "2", "-d", "5", "-b", "3", "-s", "S"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"-t", "T", "-a", "2", "-b", "3", "-c", "4", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-c", "4", "-t", "T", "-b", "3", "-a", "2", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-d", "5", "-c", "4", "-t", "T", "-b", "3", "-a", "2"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-d", "5", "-a", "2", "-c", "4", "-t", "T", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"-c", "4", "-a", "2", "-d", "5", "-b", "3", "-t", "T"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

}
catch(std::exception& e) {
std::cerr << e.what() << std::endl;
Expand Down
192 changes: 192 additions & 0 deletions test/required_params_test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*****************************************************************************
*
* CLIPP - command line interfaces for modern C++
*
* released under MIT license
*
* (c) 2017 André Müller; [email protected]
*
*****************************************************************************/

#include "testing.h"


//-------------------------------------------------------------------
struct active {
active() = default;
explicit
active(bool a_, bool b_, bool c_, bool d_,
int av_, int bv_, int cv_, int dv_):
a{a_}, b{b_}, c{c_}, d{d_},
av{av_}, bv{bv_}, cv{cv_}, dv{dv_}
{}
bool a = false, b = false, c = false, d = false;
int av = 0, bv = 0, cv = 0, dv = 0;

friend bool operator == (const active& x, const active& y) noexcept {
return (x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d &&
x.av == y.av && x.bv == y.bv && x.cv == y.cv && x.dv == y.dv );
}

template<class OStream>
friend OStream& operator << (OStream& os, const active& x) {
return os << '{' << x.a <<' '<< x.b <<' '<< x.c <<' '<< x.d
<< " | " << x.av <<' '<< x.bv <<' '<< x.cv <<' '<< x.dv << '}';
}
};

//---------------------------------------------------------------
struct errors {
errors() = default;
explicit
errors(bool a_, bool b_, bool c_, bool d_,
bool av_, bool bv_, bool cv_, bool dv_):
a{a_}, b{b_}, c{c_}, d{d_},
av{av_}, bv{bv_}, cv{cv_}, dv{dv_}
{}
bool a = false, b = false, c = false, d = false;
bool av = false, bv = false, cv = false, dv = false;

friend bool operator == (const errors& x, const errors& y) noexcept {
return (x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d &&
x.av == y.av && x.bv == y.bv && x.cv == y.cv && x.dv == y.dv );
}

template<class OStream>
friend OStream& operator << (OStream& os, const errors& x) {
return os << '{' << x.a <<' '<< x.b <<' '<< x.c <<' '<< x.d
<< " | " << x.av <<' '<< x.bv <<' '<< x.cv <<' '<< x.dv << '}';
}
};


//-------------------------------------------------------------------
void test(int lineNo,
const std::initializer_list<const char*> args,
const active& matches,
const errors& missing)
{
using namespace clipp;

active v;
errors m;
auto mode1 = (
command("mode1"),
option("-r") & opt_value("R")
,
required("-a", "--aaa").set(v.a).if_missing(set(m.a))
& value("I", v.av).if_missing(set(m.av))
,
required("-b", "--bee").set(v.b).if_missing(set(m.b))
& value("J", v.bv).if_missing(set(m.bv))
,
option("-s") & opt_value("S")
,
required("-c", "--cee").set(v.c).if_missing(set(m.c))
& opt_value("K", v.cv).if_missing(set(m.cv))
,
required("-d", "--dee").set(v.d).if_missing(set(m.d))
& value("L", v.dv).if_missing(set(m.dv))
,
option("-t") & value("T")
);

auto mode2 = ( command("mode2"), option("-u"), option("-v") );

auto cli = (mode1 | mode2);

run_wrapped_variants({ __FILE__, lineNo }, args, cli,
[&]{ v = active{}; m = errors{}; },
[&]{ return v == matches && m == missing; });
}


//-------------------------------------------------------------------
int main()
{
try {

test(__LINE__, {""}, active{0,0,0,0, 0,0,0,0}, errors{1,1,1,1, 1,1,0,1});
test(__LINE__, {"mode1"}, active{0,0,0,0, 0,0,0,0}, errors{1,1,1,1, 1,1,0,1});
test(__LINE__, {"mode2"}, active{0,0,0,0, 0,0,0,0}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"mode2", "-a"}, active{0,0,0,0, 0,0,0,0}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode2", "-b"}, active{0,0,0,0, 0,0,0,0}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode2", "-c"}, active{0,0,0,0, 0,0,0,0}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode2", "-d"}, active{0,0,0,0, 0,0,0,0}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"mode1", "-a"}, active{1,0,0,0, 0,0,0,0}, errors{0,1,1,1, 1,1,0,1});
test(__LINE__, {"mode1", "-b"}, active{0,1,0,0, 0,0,0,0}, errors{1,0,1,1, 1,1,0,1});
test(__LINE__, {"mode1", "-c"}, active{0,0,1,0, 0,0,0,0}, errors{1,1,0,1, 1,1,0,1});
test(__LINE__, {"mode1", "-d"}, active{0,0,0,1, 0,0,0,0}, errors{1,1,1,0, 1,1,0,1});

test(__LINE__, {"mode1", "--aaa"}, active{1,0,0,0, 0,0,0,0}, errors{0,1,1,1, 1,1,0,1});
test(__LINE__, {"mode1", "--bee"}, active{0,1,0,0, 0,0,0,0}, errors{1,0,1,1, 1,1,0,1});
test(__LINE__, {"mode1", "--cee"}, active{0,0,1,0, 0,0,0,0}, errors{1,1,0,1, 1,1,0,1});
test(__LINE__, {"mode1", "--dee"}, active{0,0,0,1, 0,0,0,0}, errors{1,1,1,0, 1,1,0,1});

test(__LINE__, {"mode1", "-a", "2"}, active{1,0,0,0, 2,0,0,0}, errors{0,1,1,1, 0,1,0,1});
test(__LINE__, {"mode1", "-b", "2"}, active{0,1,0,0, 0,2,0,0}, errors{1,0,1,1, 1,0,0,1});
test(__LINE__, {"mode1", "-c", "2"}, active{0,0,1,0, 0,0,2,0}, errors{1,1,0,1, 1,1,0,1});
test(__LINE__, {"mode1", "-d", "2"}, active{0,0,0,1, 0,0,0,2}, errors{1,1,1,0, 1,1,0,0});

test(__LINE__, {"mode1", "--aaa", "2"}, active{1,0,0,0, 2,0,0,0}, errors{0,1,1,1, 0,1,0,1});
test(__LINE__, {"mode1", "--bee", "2"}, active{0,1,0,0, 0,2,0,0}, errors{1,0,1,1, 1,0,0,1});
test(__LINE__, {"mode1", "--cee", "2"}, active{0,0,1,0, 0,0,2,0}, errors{1,1,0,1, 1,1,0,1});
test(__LINE__, {"mode1", "--dee", "2"}, active{0,0,0,1, 0,0,0,2}, errors{1,1,1,0, 1,1,0,0});

test(__LINE__, {"mode1", "-a", "2", "-b", "3"}, active{1,1,0,0, 2,3,0,0}, errors{0,0,1,1, 0,0,0,1});
test(__LINE__, {"mode1", "-a", "2", "-c", "3"}, active{1,0,1,0, 2,0,3,0}, errors{0,1,0,1, 0,1,0,1});
test(__LINE__, {"mode1", "-a", "2", "-d", "3"}, active{1,0,0,1, 2,0,0,3}, errors{0,1,1,0, 0,1,0,0});
test(__LINE__, {"mode1", "-b", "2", "-c", "3"}, active{0,1,1,0, 0,2,3,0}, errors{1,0,0,1, 1,0,0,1});
test(__LINE__, {"mode1", "-b", "2", "-d", "3"}, active{0,1,0,1, 0,2,0,3}, errors{1,0,1,0, 1,0,0,0});
test(__LINE__, {"mode1", "-c", "2", "-d", "3"}, active{0,0,1,1, 0,0,2,3}, errors{1,1,0,0, 1,1,0,0});

test(__LINE__, {"mode1", "-b", "3", "-a", "2"}, active{1,1,0,0, 2,3,0,0}, errors{0,0,1,1, 0,0,0,1});
test(__LINE__, {"mode1", "-c", "3", "-a", "2"}, active{1,0,1,0, 2,0,3,0}, errors{0,1,0,1, 0,1,0,1});
test(__LINE__, {"mode1", "-d", "3", "-a", "2"}, active{1,0,0,1, 2,0,0,3}, errors{0,1,1,0, 0,1,0,0});
test(__LINE__, {"mode1", "-c", "3", "-b", "2"}, active{0,1,1,0, 0,2,3,0}, errors{1,0,0,1, 1,0,0,1});
test(__LINE__, {"mode1", "-d", "3", "-b", "2"}, active{0,1,0,1, 0,2,0,3}, errors{1,0,1,0, 1,0,0,0});
test(__LINE__, {"mode1", "-d", "3", "-c", "2"}, active{0,0,1,1, 0,0,2,3}, errors{1,1,0,0, 1,1,0,0});

test(__LINE__, {"mode1", "-a", "2", "-b", "3", "-c", "4"}, active{1,1,1,0, 2,3,4,0}, errors{0,0,0,1, 0,0,0,1});
test(__LINE__, {"mode1", "-b", "3", "-c", "4", "-d", "5"}, active{0,1,1,1, 0,3,4,5}, errors{1,0,0,0, 1,0,0,0});
test(__LINE__, {"mode1", "-a", "2", "-b", "3", "-d", "4"}, active{1,1,0,1, 2,3,0,4}, errors{0,0,1,0, 0,0,0,0});

test(__LINE__, {"mode1", "-a", "2", "-c", "4", "-b", "3"}, active{1,1,1,0, 2,3,4,0}, errors{0,0,0,1, 0,0,0,1});
test(__LINE__, {"mode1", "-c", "4", "-b", "3", "-d", "5"}, active{0,1,1,1, 0,3,4,5}, errors{1,0,0,0, 1,0,0,0});
test(__LINE__, {"mode1", "-b", "3", "-a", "2", "-d", "4"}, active{1,1,0,1, 2,3,0,4}, errors{0,0,1,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "4", "-b", "3", "-a", "2"}, active{1,1,0,1, 2,3,0,4}, errors{0,0,1,0, 0,0,0,0});

test(__LINE__, {"mode1", "-a", "2", "-b", "3", "-c", "4", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-b", "3", "-a", "2", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-c", "4", "-b", "3", "-a", "2"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-a", "2", "-c", "4", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-a", "2", "-d", "5", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"mode1", "-r", "R", "-a", "2", "-b", "3", "-c", "4", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-r", "R", "-b", "3", "-a", "2", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-c", "4", "-r", "R", "-b", "3", "-a", "2"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-a", "2", "-c", "4", "-r", "R", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-a", "2", "-d", "5", "-b", "3", "-r", "R"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"mode1", "-s", "S", "-a", "2", "-b", "3", "-c", "4", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-s", "S", "-b", "3", "-a", "2", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-c", "4", "-s", "S", "-b", "3", "-a", "2"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-a", "2", "-c", "4", "-s", "S", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-a", "2", "-d", "5", "-b", "3", "-s", "S"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

test(__LINE__, {"mode1", "-t", "T", "-a", "2"}, active{1,0,0,0, 2,0,0,0}, errors{0,1,1,1, 0,1,0,1});

test(__LINE__, {"mode1", "-t", "T", "-a", "2", "-b", "3", "-c", "4", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-t", "T", "-b", "3", "-a", "2", "-d", "5"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-c", "4", "-t", "T", "-b", "3", "-a", "2"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-d", "5", "-a", "2", "-c", "4", "-t", "T", "-b", "3"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});
test(__LINE__, {"mode1", "-c", "4", "-a", "2", "-d", "5", "-b", "3", "-t", "T"}, active{1,1,1,1, 2,3,4,5}, errors{0,0,0,0, 0,0,0,0});

}
catch(std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
}

0 comments on commit a5cab3d

Please sign in to comment.