diff options
author | Brad King <brad.king@kitware.com> | 2022-06-20 14:12:04 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2022-07-27 11:03:32 (GMT) |
commit | 078e2aec8f7a0e6d0be91d0fd565d6c22032f666 (patch) | |
tree | 3bc04a620c5234b6daea0e4f2366ffa6951b8e1d /Source/cmArgumentParser.cxx | |
parent | 77fcb00a2b76518c0db861a96a8f4857a50b140e (diff) | |
download | CMake-078e2aec8f7a0e6d0be91d0fd565d6c22032f666.zip CMake-078e2aec8f7a0e6d0be91d0fd565d6c22032f666.tar.gz CMake-078e2aec8f7a0e6d0be91d0fd565d6c22032f666.tar.bz2 |
cmArgumentParser: Generalize internal state tracking
Use a `std::function` to support general actions on value arguments.
Diffstat (limited to 'Source/cmArgumentParser.cxx')
-rw-r--r-- | Source/cmArgumentParser.cxx | 65 |
1 files changed, 43 insertions, 22 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<Continue(cm::string_view)> 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<std::string>& val) { - this->CurrentString = &val; - this->CurrentList = nullptr; + this->KeywordValueFunc = [&val](cm::string_view arg) -> Continue { + static_cast<std::string&>(val) = std::string(arg); + return Continue::No; + }; this->ExpectValue = false; } void Instance::Bind(MaybeEmpty<std::vector<std::string>>& 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<std::vector<std::string>>& 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<std::vector<std::string>>& val) +void Instance::Bind(std::vector<std::vector<std::string>>& multiVal) { - this->CurrentString = nullptr; - this->CurrentList = (static_cast<void>(val.emplace_back()), &val.back()); + multiVal.emplace_back(); + std::vector<std::string>& 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() |