diff --git a/cmdopt2/args.hpp b/cmdopt2/args.hpp index a514215f..cbe8ab27 100644 --- a/cmdopt2/args.hpp +++ b/cmdopt2/args.hpp @@ -111,13 +111,18 @@ namespace cruft::cmdopt2 { res.required_ = val; return res; } - }; + bool repeat_; + BaseT repeat (bool val) const + { + BaseT res = get (); + res.repeat_ = val; + return res; + } + }; struct positional_t : public ops_t { positional_t ignore (void) const; - - int count = 1; }; positional_t positional (char const *name); diff --git a/test/cmdopt2.cpp b/test/cmdopt2.cpp index a5b851c8..d5185f4e 100644 --- a/test/cmdopt2.cpp +++ b/test/cmdopt2.cpp @@ -223,6 +223,45 @@ test_required (cruft::TAP::logger &tap) } +/////////////////////////////////////////////////////////////////////////////// +static void +test_repeated (cruft::TAP::logger &tap) +{ + static const struct { + std::vector args; + std::vector values; + } TESTS[] = { + { .args = { "cmd" }, .values = { } }, + { .args = { "cmd", "1", }, .values = { 1, } }, + { .args = { "cmd", "1", "1", }, .values = { 1, 1, } }, + { .args = { "cmd", "-v", "1", }, .values = { 1, } }, + { .args = { "cmd", "-v", "1", "-v", "1"}, .values = { 1, 1 } }, + { .args = { "cmd", "--foo", "1", }, .values = { 1, } }, + { .args = { "cmd", "--foo", "1", "--foo", "1"}, .values = { 1, 1, } }, + }; + + std::vector values; + + using namespace cruft::cmdopt2; + parser p ("test suite"); + p.add (keyword ("value").flag ('v').repeat (true).bind (values)); + p.add (keyword ("foo").repeat (true).bind (values)); + p.add (positional ("var").repeat (true).bind (values)); + + for (auto const &t: TESTS) { + values.clear (); + + auto const consumed = p.parse (int (t.args.size ()), t.args.data ()); + tap.expect ( + consumed == int (t.args.size ()) + and values == t.values, + "{}", + fmt::join (t.args.begin (), t.args.end (), " ") + ); + } +} + + /////////////////////////////////////////////////////////////////////////////// int main () { @@ -230,5 +269,6 @@ int main () test_combinations (tap); test_presence (tap); test_required (tap); + test_repeated (tap); return tap.status (); } \ No newline at end of file