1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 20:46:37 +08:00

ArgumentParser: implement HasKeyword helper

This commit is contained in:
Vito Gamberini
2025-07-08 17:44:07 -04:00
parent b557cd0f49
commit 7238c8c999
2 changed files with 21 additions and 0 deletions

View File

@@ -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;

View File

@@ -300,6 +300,11 @@ bool testArgumentParserDynamic()
return result.Func4(key, arg);
};
cmArgumentParser<void> 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<ArgumentParser::ParseResult&>(result) =
cmArgumentParser<void>{}
.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<std::string> 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<std::string> unparsedArguments;
Derived const result = parserDerivedStatic.Parse(args, &unparsedArguments);
if (!verifyResult(result, unparsedArguments)) {