mirror of
https://github.com/Kitware/CMake.git
synced 2026-03-06 22:58:35 -06:00
cmArgumentParser: Generalize expected argument count
Replace the boolean expectation with an integer count.
This commit is contained in:
@@ -34,64 +34,69 @@ auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator
|
|||||||
return (it != this->end() && it->first == name) ? it : this->end();
|
return (it != this->end() && it->first == name) ? it : this->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Bind(std::function<Continue(cm::string_view)> f)
|
void Instance::Bind(std::function<Continue(cm::string_view)> f,
|
||||||
|
ExpectAtLeast expect)
|
||||||
{
|
{
|
||||||
this->KeywordValueFunc = std::move(f);
|
this->KeywordValueFunc = std::move(f);
|
||||||
this->ExpectValue = true;
|
this->KeywordValuesExpected = expect.Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Bind(bool& val)
|
void Instance::Bind(bool& val)
|
||||||
{
|
{
|
||||||
val = true;
|
val = true;
|
||||||
this->KeywordValueFunc = nullptr;
|
this->Bind(nullptr, ExpectAtLeast{ 0 });
|
||||||
this->ExpectValue = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Bind(std::string& val)
|
void Instance::Bind(std::string& val)
|
||||||
{
|
{
|
||||||
this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
|
this->Bind(
|
||||||
val = std::string(arg);
|
[&val](cm::string_view arg) -> Continue {
|
||||||
return Continue::No;
|
val = std::string(arg);
|
||||||
};
|
return Continue::No;
|
||||||
this->ExpectValue = true;
|
},
|
||||||
|
ExpectAtLeast{ 1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Bind(Maybe<std::string>& val)
|
void Instance::Bind(Maybe<std::string>& val)
|
||||||
{
|
{
|
||||||
this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
|
this->Bind(
|
||||||
static_cast<std::string&>(val) = std::string(arg);
|
[&val](cm::string_view arg) -> Continue {
|
||||||
return Continue::No;
|
static_cast<std::string&>(val) = std::string(arg);
|
||||||
};
|
return Continue::No;
|
||||||
this->ExpectValue = false;
|
},
|
||||||
|
ExpectAtLeast{ 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
|
void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
|
||||||
{
|
{
|
||||||
this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
|
this->Bind(
|
||||||
val.emplace_back(arg);
|
[&val](cm::string_view arg) -> Continue {
|
||||||
return Continue::Yes;
|
val.emplace_back(arg);
|
||||||
};
|
return Continue::Yes;
|
||||||
this->ExpectValue = false;
|
},
|
||||||
|
ExpectAtLeast{ 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
|
void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
|
||||||
{
|
{
|
||||||
this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
|
this->Bind(
|
||||||
val.emplace_back(arg);
|
[&val](cm::string_view arg) -> Continue {
|
||||||
return Continue::Yes;
|
val.emplace_back(arg);
|
||||||
};
|
return Continue::Yes;
|
||||||
this->ExpectValue = true;
|
},
|
||||||
|
ExpectAtLeast{ 1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
|
void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
|
||||||
{
|
{
|
||||||
multiVal.emplace_back();
|
multiVal.emplace_back();
|
||||||
std::vector<std::string>& val = multiVal.back();
|
std::vector<std::string>& val = multiVal.back();
|
||||||
this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue {
|
this->Bind(
|
||||||
val.emplace_back(arg);
|
[&val](cm::string_view arg) -> Continue {
|
||||||
return Continue::Yes;
|
val.emplace_back(arg);
|
||||||
};
|
return Continue::Yes;
|
||||||
this->ExpectValue = false;
|
},
|
||||||
|
ExpectAtLeast{ 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Consume(cm::string_view arg)
|
void Instance::Consume(cm::string_view arg)
|
||||||
@@ -100,6 +105,7 @@ void Instance::Consume(cm::string_view arg)
|
|||||||
if (it != this->Bindings.Keywords.end()) {
|
if (it != this->Bindings.Keywords.end()) {
|
||||||
this->FinishKeyword();
|
this->FinishKeyword();
|
||||||
this->Keyword = it->first;
|
this->Keyword = it->first;
|
||||||
|
this->KeywordValuesSeen = 0;
|
||||||
if (this->Bindings.ParsedKeyword) {
|
if (this->Bindings.ParsedKeyword) {
|
||||||
this->Bindings.ParsedKeyword(*this, it->first);
|
this->Bindings.ParsedKeyword(*this, it->first);
|
||||||
}
|
}
|
||||||
@@ -115,7 +121,7 @@ void Instance::Consume(cm::string_view arg)
|
|||||||
this->KeywordValueFunc = nullptr;
|
this->KeywordValueFunc = nullptr;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
this->ExpectValue = false;
|
++this->KeywordValuesSeen;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +135,7 @@ void Instance::FinishKeyword()
|
|||||||
if (this->Keyword.empty()) {
|
if (this->Keyword.empty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this->ExpectValue) {
|
if (this->KeywordValuesSeen < this->KeywordValuesExpected) {
|
||||||
if (this->ParseResults != nullptr) {
|
if (this->ParseResults != nullptr) {
|
||||||
this->ParseResults->AddKeywordError(this->Keyword,
|
this->ParseResults->AddKeywordError(this->Keyword,
|
||||||
" missing required value\n");
|
" missing required value\n");
|
||||||
|
|||||||
@@ -68,6 +68,16 @@ enum class Continue
|
|||||||
Yes,
|
Yes,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ExpectAtLeast
|
||||||
|
{
|
||||||
|
std::size_t Count = 0;
|
||||||
|
|
||||||
|
ExpectAtLeast(std::size_t count)
|
||||||
|
: Count(count)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class Instance;
|
class Instance;
|
||||||
using KeywordAction = std::function<void(Instance&)>;
|
using KeywordAction = std::function<void(Instance&)>;
|
||||||
using KeywordNameAction = std::function<void(Instance&, cm::string_view)>;
|
using KeywordNameAction = std::function<void(Instance&, cm::string_view)>;
|
||||||
@@ -93,6 +103,7 @@ public:
|
|||||||
class Base
|
class Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using ExpectAtLeast = ArgumentParser::ExpectAtLeast;
|
||||||
using Continue = ArgumentParser::Continue;
|
using Continue = ArgumentParser::Continue;
|
||||||
using Instance = ArgumentParser::Instance;
|
using Instance = ArgumentParser::Instance;
|
||||||
using ParseResult = ArgumentParser::ParseResult;
|
using ParseResult = ArgumentParser::ParseResult;
|
||||||
@@ -136,7 +147,7 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bind(std::function<Continue(cm::string_view)> f);
|
void Bind(std::function<Continue(cm::string_view)> f, ExpectAtLeast expect);
|
||||||
void Bind(bool& val);
|
void Bind(bool& val);
|
||||||
void Bind(std::string& val);
|
void Bind(std::string& val);
|
||||||
void Bind(Maybe<std::string>& val);
|
void Bind(Maybe<std::string>& val);
|
||||||
@@ -170,8 +181,9 @@ private:
|
|||||||
void* Result = nullptr;
|
void* Result = nullptr;
|
||||||
|
|
||||||
cm::string_view Keyword;
|
cm::string_view Keyword;
|
||||||
|
std::size_t KeywordValuesSeen = 0;
|
||||||
|
std::size_t KeywordValuesExpected = 0;
|
||||||
std::function<Continue(cm::string_view)> KeywordValueFunc;
|
std::function<Continue(cm::string_view)> KeywordValueFunc;
|
||||||
bool ExpectValue = false;
|
|
||||||
|
|
||||||
void Consume(cm::string_view arg);
|
void Consume(cm::string_view arg);
|
||||||
void FinishKeyword();
|
void FinishKeyword();
|
||||||
|
|||||||
Reference in New Issue
Block a user