cmdopt2: add convenience function for lambda handlers

This commit is contained in:
Danny Robson 2022-05-24 15:09:49 +10:00
parent 1e4d47acf9
commit d06ee4995c
2 changed files with 49 additions and 0 deletions

View File

@ -36,6 +36,7 @@ namespace cruft::cmdopt2 {
template <typename BaseT>
struct ops_t : argument_t {
template <typename ValueT>
requires (!std::is_convertible_v<ValueT, acceptor1_t>)
BaseT bind (ValueT&&) = delete;
template <typename ValueT>
@ -86,6 +87,14 @@ namespace cruft::cmdopt2 {
return get ();
}
BaseT
bind (acceptor1_t _acceptor1)
{
CHECK (!acceptor1);
acceptor1 = std::move (_acceptor1);
return get ();
}
BaseT
get (void) const
{

View File

@ -262,6 +262,45 @@ test_repeated (cruft::TAP::logger &tap)
}
///////////////////////////////////////////////////////////////////////////////
static void
test_bind (cruft::TAP::logger &tap)
{
static constexpr
char const* VALUES[] = {
"one",
"two",
"three"
};
std::vector<char const*> values;
using namespace cruft::cmdopt2;
parser p ("test suite");
p.add (
keyword ("value")
.flag ('v')
.repeat (true)
.bind ([&] (char const *str) { values.push_back (str); })
);
char const *argv[] = { "./cmd", "--value", VALUES[0], "--value", VALUES[1], "--value", VALUES[2] };
auto const consumed = p.parse (std::ssize (argv), argv);
tap.expect_eq (consumed, std::ssize (argv), "bound function: consumed");
tap.expect (
std::equal (
std::begin (values),
std::end (values),
std::begin (VALUES),
std::end (VALUES)
),
"bound function: values"
);
}
///////////////////////////////////////////////////////////////////////////////
int main ()
{
@ -270,5 +309,6 @@ int main ()
test_presence (tap);
test_required (tap);
test_repeated (tap);
test_bind (tap);
return tap.status ();
}