From 10a7a647791fa3a24ec4f572f2573a6e0aaa881b Mon Sep 17 00:00:00 2001 From: levonwaveye <135352287+levonwaveye@users.noreply.github.com> Date: Wed, 23 Oct 2024 13:14:32 -0700 Subject: [PATCH] Add ParseResult::contains method (#440) --- include/cxxopts.hpp | 6 ++++++ test/options.cpp | 3 +++ 2 files changed, 9 insertions(+) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index a2e71fa..2819f50 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -1743,6 +1743,12 @@ CXXOPTS_DIAGNOSTIC_POP return viter->second.count(); } + bool + contains(const std::string& o) const + { + return static_cast(count(o)); + } + const OptionValue& operator[](const std::string& option) const { diff --git a/test/options.cpp b/test/options.cpp index 647934c..60d2f05 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -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); @@ -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(), cxxopts::exceptions::option_has_no_value); CHECK(options.program() == "tester");