argparse/test/test_parent_parsers.cpp
Sean Robinson a8900c2019 Replace simple type-converting Argument.action with Argument.scan in tests
Argument.scan handles simple string to numeric type conversions, removing
the need to create a lambda.  Argument.action is still necessary for more
complex conversions and those are left unchanged.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-08-24 09:25:49 -07:00

39 lines
1.3 KiB
C++

#include <doctest.hpp>
#include <argparse/argparse.hpp>
using doctest::test_suite;
TEST_CASE("Add parent parsers" * test_suite("parent_parsers")) {
argparse::ArgumentParser parent_parser("main");
parent_parser.add_argument("--verbose")
.default_value(false)
.implicit_value(true);
argparse::ArgumentParser child_parser("foo");
child_parser.add_parents(parent_parser);
child_parser.parse_args({ "./main", "--verbose"});
REQUIRE(child_parser["--verbose"] == true);
REQUIRE(parent_parser["--verbose"] == false);
}
TEST_CASE("Add parent to multiple parent parsers" *
test_suite("parent_parsers")) {
argparse::ArgumentParser parent_parser("main");
parent_parser.add_argument("--parent")
.default_value(0)
.scan<'i', int>();
argparse::ArgumentParser foo_parser("foo");
foo_parser.add_argument("foo");
foo_parser.add_parents(parent_parser);
foo_parser.parse_args({ "./main", "--parent", "2", "XXX" });
REQUIRE(foo_parser["--parent"] == 2);
REQUIRE(foo_parser["foo"] == std::string("XXX"));
REQUIRE(parent_parser["--parent"] == 0);
argparse::ArgumentParser bar_parser("bar");
bar_parser.add_argument("--bar");
bar_parser.parse_args({ "./main", "--bar", "YYY" });
REQUIRE(bar_parser["--bar"] == std::string("YYY"));
}