diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index 160dda0520..223c3ce699 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -394,6 +394,11 @@ public: this->Parse(result, args, unparsedArguments, pos); return result; } + + bool HasKeyword(cm::string_view key) const + { + return this->Bindings.Keywords.Find(key) != this->Bindings.Keywords.end(); + } }; template <> @@ -469,6 +474,11 @@ public: return parseResult; } + bool HasKeyword(cm::string_view key) const + { + return this->Bindings.Keywords.Find(key) != this->Bindings.Keywords.end(); + } + protected: using Base::Instance; using Base::BindKeywordMissingValue; diff --git a/Tests/CMakeLib/testArgumentParser.cxx b/Tests/CMakeLib/testArgumentParser.cxx index 2dbff01da3..4bf6a40ca6 100644 --- a/Tests/CMakeLib/testArgumentParser.cxx +++ b/Tests/CMakeLib/testArgumentParser.cxx @@ -300,6 +300,11 @@ bool testArgumentParserDynamic() return result.Func4(key, arg); }; + cmArgumentParser parserDynamic; + parserDynamic.Bind("OPTION_1"_s, result.Option1); + ASSERT_TRUE(parserDynamic.HasKeyword("OPTION_1"_s)); + ASSERT_TRUE(!parserDynamic.HasKeyword("NOT_AN_OPTION"_s)); + static_cast(result) = cmArgumentParser{} .Bind(0, result.Pos0) @@ -424,6 +429,9 @@ BIND_TRAILING(parserTrailingDerivedStatic, DerivedTrailingPos); bool testArgumentParserStatic() { + ASSERT_TRUE(parserStatic.HasKeyword("OPTION_1"_s)); + ASSERT_TRUE(!parserStatic.HasKeyword("NOT_AN_OPTION"_s)); + std::vector unparsedArguments; Result const result = parserStatic.Parse(args, &unparsedArguments); if (!verifyResult(result, unparsedArguments)) { @@ -438,6 +446,9 @@ bool testArgumentParserStatic() bool testArgumentParserDerivedStatic() { + ASSERT_TRUE(parserDerivedStatic.HasKeyword("OPTION_1"_s)); + ASSERT_TRUE(!parserDerivedStatic.HasKeyword("NOT_AN_OPTION"_s)); + std::vector unparsedArguments; Derived const result = parserDerivedStatic.Parse(args, &unparsedArguments); if (!verifyResult(result, unparsedArguments)) {