mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Const-correct ArgumentParser
This commit is contained in:
parent
e1ea7ec50a
commit
4ede429264
@ -925,7 +925,7 @@ public:
|
||||
* @throws std::bad_any_cast if the option is not of type T
|
||||
*/
|
||||
template <typename T = std::string>
|
||||
auto present(std::string_view aArgumentName) -> std::optional<T> {
|
||||
auto present(std::string_view aArgumentName) const -> std::optional<T> {
|
||||
return (*this)[aArgumentName].present<T>();
|
||||
}
|
||||
|
||||
|
@ -93,4 +93,27 @@ TEST_CASE("ArgumentParser is CopyConstructible and CopyAssignable" *
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("ArgumentParser is const-correct after construction and parsing" *
|
||||
test_suite("value_semantics")) {
|
||||
GIVEN("a parser") {
|
||||
argparse::ArgumentParser parser("test");
|
||||
parser.add_argument("--foo", "-f").help("I am foo");
|
||||
parser.add_description("A description");
|
||||
parser.add_epilog("An epilog");
|
||||
|
||||
WHEN("becomes const-qualified") {
|
||||
parser.parse_args({"./main", "--foo", "baz"});
|
||||
const auto const_parser = std::move(parser);
|
||||
|
||||
THEN("only const methods are accessible") {
|
||||
REQUIRE(const_parser.help().str().size() > 0);
|
||||
REQUIRE(const_parser.present<std::string>("--foo"));
|
||||
REQUIRE(const_parser.is_used("-f"));
|
||||
REQUIRE(const_parser.get("-f") == "baz");
|
||||
REQUIRE(const_parser["-f"] == std::string("baz"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user