From f5d2f6076abe9247ec6a4fc5130267ecc256ab81 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 26 Jul 2022 14:30:22 -0400 Subject: cmArgumentParser: Generalize expected argument count Replace the boolean expectation with an integer count. --- Source/cmArgumentParser.cxx | 68 ++++++++++++++++++++++++--------------------- Source/cmArgumentParser.h | 16 +++++++++-- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/Source/cmArgumentParser.cxx b/Source/cmArgumentParser.cxx index a2ab87f..bd31b46 100644 --- a/Source/cmArgumentParser.cxx +++ b/Source/cmArgumentParser.cxx @@ -34,64 +34,69 @@ auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator return (it != this->end() && it->first == name) ? it : this->end(); } -void Instance::Bind(std::function f) +void Instance::Bind(std::function f, + ExpectAtLeast expect) { this->KeywordValueFunc = std::move(f); - this->ExpectValue = true; + this->KeywordValuesExpected = expect.Count; } void Instance::Bind(bool& val) { val = true; - this->KeywordValueFunc = nullptr; - this->ExpectValue = false; + this->Bind(nullptr, ExpectAtLeast{ 0 }); } void Instance::Bind(std::string& val) { - this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { - val = std::string(arg); - return Continue::No; - }; - this->ExpectValue = true; + this->Bind( + [&val](cm::string_view arg) -> Continue { + val = std::string(arg); + return Continue::No; + }, + ExpectAtLeast{ 1 }); } void Instance::Bind(Maybe& val) { - this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { - static_cast(val) = std::string(arg); - return Continue::No; - }; - this->ExpectValue = false; + this->Bind( + [&val](cm::string_view arg) -> Continue { + static_cast(val) = std::string(arg); + return Continue::No; + }, + ExpectAtLeast{ 0 }); } void Instance::Bind(MaybeEmpty>& val) { - this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { - val.emplace_back(arg); - return Continue::Yes; - }; - this->ExpectValue = false; + this->Bind( + [&val](cm::string_view arg) -> Continue { + val.emplace_back(arg); + return Continue::Yes; + }, + ExpectAtLeast{ 0 }); } void Instance::Bind(NonEmpty>& val) { - this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { - val.emplace_back(arg); - return Continue::Yes; - }; - this->ExpectValue = true; + this->Bind( + [&val](cm::string_view arg) -> Continue { + val.emplace_back(arg); + return Continue::Yes; + }, + ExpectAtLeast{ 1 }); } void Instance::Bind(std::vector>& multiVal) { multiVal.emplace_back(); std::vector& val = multiVal.back(); - this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { - val.emplace_back(arg); - return Continue::Yes; - }; - this->ExpectValue = false; + this->Bind( + [&val](cm::string_view arg) -> Continue { + val.emplace_back(arg); + return Continue::Yes; + }, + ExpectAtLeast{ 0 }); } void Instance::Consume(cm::string_view arg) @@ -100,6 +105,7 @@ void Instance::Consume(cm::string_view arg) if (it != this->Bindings.Keywords.end()) { this->FinishKeyword(); this->Keyword = it->first; + this->KeywordValuesSeen = 0; if (this->Bindings.ParsedKeyword) { this->Bindings.ParsedKeyword(*this, it->first); } @@ -115,7 +121,7 @@ void Instance::Consume(cm::string_view arg) this->KeywordValueFunc = nullptr; break; } - this->ExpectValue = false; + ++this->KeywordValuesSeen; return; } @@ -129,7 +135,7 @@ void Instance::FinishKeyword() if (this->Keyword.empty()) { return; } - if (this->ExpectValue) { + if (this->KeywordValuesSeen < this->KeywordValuesExpected) { if (this->ParseResults != nullptr) { this->ParseResults->AddKeywordError(this->Keyword, " missing required value\n"); diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index a417eb4..d0406e4 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -68,6 +68,16 @@ enum class Continue Yes, }; +struct ExpectAtLeast +{ + std::size_t Count = 0; + + ExpectAtLeast(std::size_t count) + : Count(count) + { + } +}; + class Instance; using KeywordAction = std::function; using KeywordNameAction = std::function; @@ -93,6 +103,7 @@ public: class Base { public: + using ExpectAtLeast = ArgumentParser::ExpectAtLeast; using Continue = ArgumentParser::Continue; using Instance = ArgumentParser::Instance; using ParseResult = ArgumentParser::ParseResult; @@ -136,7 +147,7 @@ public: { } - void Bind(std::function f); + void Bind(std::function f, ExpectAtLeast expect); void Bind(bool& val); void Bind(std::string& val); void Bind(Maybe& val); @@ -170,8 +181,9 @@ private: void* Result = nullptr; cm::string_view Keyword; + std::size_t KeywordValuesSeen = 0; + std::size_t KeywordValuesExpected = 0; std::function KeywordValueFunc; - bool ExpectValue = false; void Consume(cm::string_view arg); void FinishKeyword(); -- cgit v0.12