Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const-correct ArgumentParser #108

Merged
merged 2 commits into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ class ArgumentParser {
* @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>();
}

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/test_const_correct.cpp
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}
}