From 078e2aec8f7a0e6d0be91d0fd565d6c22032f666 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Jun 2022 10:12:04 -0400 Subject: cmArgumentParser: Generalize internal state tracking Use a `std::function` to support general actions on value arguments. --- Source/cmArgumentParser.cxx | 65 ++++++++++++++++++++++++++++++--------------- Source/cmArgumentParser.h | 11 ++++++-- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/Source/cmArgumentParser.cxx b/Source/cmArgumentParser.cxx index 4379b29..a2ab87f 100644 --- a/Source/cmArgumentParser.cxx +++ b/Source/cmArgumentParser.cxx @@ -34,46 +34,63 @@ 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) +{ + this->KeywordValueFunc = std::move(f); + this->ExpectValue = true; +} + void Instance::Bind(bool& val) { val = true; - this->CurrentString = nullptr; - this->CurrentList = nullptr; + this->KeywordValueFunc = nullptr; this->ExpectValue = false; } void Instance::Bind(std::string& val) { - this->CurrentString = &val; - this->CurrentList = nullptr; + this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { + val = std::string(arg); + return Continue::No; + }; this->ExpectValue = true; } void Instance::Bind(Maybe& val) { - this->CurrentString = &val; - this->CurrentList = nullptr; + this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { + static_cast(val) = std::string(arg); + return Continue::No; + }; this->ExpectValue = false; } void Instance::Bind(MaybeEmpty>& val) { - this->CurrentString = nullptr; - this->CurrentList = &val; + this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { + val.emplace_back(arg); + return Continue::Yes; + }; this->ExpectValue = false; } void Instance::Bind(NonEmpty>& val) { - this->CurrentString = nullptr; - this->CurrentList = &val; + this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { + val.emplace_back(arg); + return Continue::Yes; + }; this->ExpectValue = true; } -void Instance::Bind(std::vector>& val) +void Instance::Bind(std::vector>& multiVal) { - this->CurrentString = nullptr; - this->CurrentList = (static_cast(val.emplace_back()), &val.back()); + 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; } @@ -90,17 +107,21 @@ void Instance::Consume(cm::string_view arg) return; } - if (this->CurrentString != nullptr) { - this->CurrentString->assign(std::string(arg)); - this->CurrentString = nullptr; - this->CurrentList = nullptr; - } else if (this->CurrentList != nullptr) { - this->CurrentList->emplace_back(arg); - } else if (this->UnparsedArguments != nullptr) { - this->UnparsedArguments->emplace_back(arg); + if (this->KeywordValueFunc) { + switch (this->KeywordValueFunc(arg)) { + case Continue::Yes: + break; + case Continue::No: + this->KeywordValueFunc = nullptr; + break; + } + this->ExpectValue = false; + return; } - this->ExpectValue = false; + if (this->UnparsedArguments != nullptr) { + this->UnparsedArguments->emplace_back(arg); + } } void Instance::FinishKeyword() diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index 33b8fff..a417eb4 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -62,6 +62,12 @@ AsParseResultPtr(Result&) return nullptr; } +enum class Continue +{ + No, + Yes, +}; + class Instance; using KeywordAction = std::function; using KeywordNameAction = std::function; @@ -87,6 +93,7 @@ public: class Base { public: + using Continue = ArgumentParser::Continue; using Instance = ArgumentParser::Instance; using ParseResult = ArgumentParser::ParseResult; @@ -129,6 +136,7 @@ public: { } + void Bind(std::function f); void Bind(bool& val); void Bind(std::string& val); void Bind(Maybe& val); @@ -162,8 +170,7 @@ private: void* Result = nullptr; cm::string_view Keyword; - std::string* CurrentString = nullptr; - std::vector* CurrentList = nullptr; + std::function KeywordValueFunc; bool ExpectValue = false; void Consume(cm::string_view arg); -- cgit v0.12