Browse Source

ArgumentParser: implement HasKeyword helper

Vito Gamberini 4 months ago
parent
commit
7238c8c999
2 changed files with 21 additions and 0 deletions
  1. 10 0
      Source/cmArgumentParser.h
  2. 11 0
      Tests/CMakeLib/testArgumentParser.cxx

+ 10 - 0
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;

+ 11 - 0
Tests/CMakeLib/testArgumentParser.cxx

@@ -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)) {