Merge pull request #108 from Bedzior/const-correct-argument-parser

Const-correct ArgumentParser
This commit is contained in:
Pranav 2021-08-07 07:22:12 -05:00 committed by GitHub
commit ccf3920ce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -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>();
}

View File

@ -28,6 +28,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
test_append.cpp
test_compound_arguments.cpp
test_container_arguments.cpp
test_const_correct.cpp
test_help.cpp
test_invalid_arguments.cpp
test_is_used.cpp

View File

@ -0,0 +1,27 @@
#include <argparse/argparse.hpp>
#include <doctest.hpp>
using doctest::test_suite;
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"));
}
}
}
}