cmdopt2/args: move repeat into base arg

This commit is contained in:
Danny Robson 2022-03-21 10:48:24 +10:00
parent 153844eded
commit 9caba84fc6
2 changed files with 48 additions and 3 deletions

View File

@ -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> {
positional_t ignore (void) const;
int count = 1;
};
positional_t positional (char const *name);

View File

@ -223,6 +223,45 @@ test_required (cruft::TAP::logger &tap)
}
///////////////////////////////////////////////////////////////////////////////
static void
test_repeated (cruft::TAP::logger &tap)
{
static const struct {
std::vector<char const*> args;
std::vector<int> 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<int> 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 ();
}