浏览代码

cmArgumentParser: Offer private binding to cmParseArgumentsCommand

The `keywordsMissingValue` argument to `Parse()` is now needed only for
the `cmake_parse_arguments` result `_KEYWORDS_MISSING_VALUES`.  Offer
its implementation a private binding for this.  Our internal clients can
use `ArgumentParser::NonEmpty<>` and friends to enforce the presence of
values.
Brad King 3 年之前
父节点
当前提交
9a7efb6813
共有 3 个文件被更改,包括 22 次插入1 次删除
  1. 3 0
      Source/cmArgumentParser.cxx
  2. 11 0
      Source/cmArgumentParser.h
  3. 8 1
      Source/cmParseArgumentsCommand.cxx

+ 3 - 0
Source/cmArgumentParser.cxx

@@ -116,6 +116,9 @@ void Instance::FinishKeyword()
     if (this->KeywordsMissingValue != nullptr) {
       this->KeywordsMissingValue->emplace_back(this->Keyword);
     }
+    if (this->Bindings.KeywordMissingValue) {
+      this->Bindings.KeywordMissingValue(*this, this->Keyword);
+    }
   }
 }
 

+ 11 - 0
Source/cmArgumentParser.h

@@ -64,6 +64,7 @@ AsParseResultPtr(Result&)
 
 class Instance;
 using KeywordAction = std::function<void(Instance&)>;
+using KeywordNameAction = std::function<void(Instance&, cm::string_view)>;
 
 // using KeywordActionMap = cm::flat_map<cm::string_view, KeywordAction>;
 class KeywordActionMap
@@ -79,6 +80,7 @@ class ActionMap
 {
 public:
   KeywordActionMap Keywords;
+  KeywordNameAction KeywordMissingValue;
 };
 
 class Base
@@ -100,6 +102,12 @@ public:
     assert(inserted);
     static_cast<void>(inserted);
   }
+
+  void BindKeywordMissingValue(KeywordNameAction action)
+  {
+    assert(!this->Bindings.KeywordMissingValue);
+    this->Bindings.KeywordMissingValue = std::move(action);
+  }
 };
 
 class Instance
@@ -233,6 +241,9 @@ public:
   }
 
 protected:
+  using Base::Instance;
+  using Base::BindKeywordMissingValue;
+
   template <typename T>
   bool Bind(cm::string_view name, T& ref)
   {

+ 8 - 1
Source/cmParseArgumentsCommand.cxx

@@ -48,6 +48,12 @@ using options_set = std::set<cm::string_view>;
 
 struct UserArgumentParser : public cmArgumentParser<void>
 {
+  void BindKeywordsMissingValue(std::vector<cm::string_view>& ref)
+  {
+    this->cmArgumentParser<void>::BindKeywordMissingValue(
+      [&ref](Instance&, cm::string_view arg) { ref.emplace_back(arg); });
+  }
+
   template <typename T, typename H>
   void Bind(std::vector<std::string> const& names,
             std::map<std::string, T>& ref, H duplicateKey)
@@ -211,8 +217,9 @@ bool cmParseArgumentsCommand(std::vector<std::string> const& args,
   }
 
   std::vector<cm::string_view> keywordsMissingValues;
+  parser.BindKeywordsMissingValue(keywordsMissingValues);
 
-  parser.Parse(list, &unparsed, &keywordsMissingValues);
+  parser.Parse(list, &unparsed);
 
   PassParsedArguments(
     prefix, status.GetMakefile(), options, singleValArgs, multiValArgs,