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.
This commit is contained in:
Brad King
2022-07-20 15:44:44 -04:00
parent a0ff592bf4
commit 9a7efb6813
3 changed files with 22 additions and 1 deletions

View File

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

View File

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

View File

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