diff --git a/cmdopt2/args.hpp b/cmdopt2/args.hpp index cbe8ab27..8daa8793 100644 --- a/cmdopt2/args.hpp +++ b/cmdopt2/args.hpp @@ -36,6 +36,7 @@ namespace cruft::cmdopt2 { template struct ops_t : argument_t { template + requires (!std::is_convertible_v) BaseT bind (ValueT&&) = delete; template @@ -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 { diff --git a/test/cmdopt2.cpp b/test/cmdopt2.cpp index b704cf3a..a3b27d7c 100644 --- a/test/cmdopt2.cpp +++ b/test/cmdopt2.cpp @@ -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 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 (); } \ No newline at end of file