Skip to content

Commit

Permalink
Add ParseResult::contains method (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
levonwaveye authored Oct 23, 2024
1 parent 63d1b65 commit 10a7a64
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/cxxopts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,12 @@ CXXOPTS_DIAGNOSTIC_POP
return viter->second.count();
}

bool
contains(const std::string& o) const
{
return static_cast<bool>(count(o));
}

const OptionValue&
operator[](const std::string& option) const
{
Expand Down
3 changes: 3 additions & 0 deletions test/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ TEST_CASE("Basic options", "[options]")
auto result = options.parse(argc, actual_argv);

CHECK(result.count("long") == 1);
CHECK(result.contains("long"));
CHECK(result.count("s") == 1);
CHECK(result.count("value") == 1);
CHECK(result.count("a") == 1);
Expand All @@ -112,6 +113,8 @@ TEST_CASE("Basic options", "[options]")
CHECK(arguments[2].key() == "value");
CHECK(arguments[3].key() == "av");

CHECK(result.count("nothing") == 0);
CHECK_FALSE(result.contains("nothing"));
CHECK_THROWS_AS(result["nothing"].as<std::string>(), cxxopts::exceptions::option_has_no_value);

CHECK(options.program() == "tester");
Expand Down

0 comments on commit 10a7a64

Please sign in to comment.