From 77fcb00a2b76518c0db861a96a8f4857a50b140e Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 26 Jul 2022 14:03:32 -0400 Subject: cmArgumentParser: Propagate constructors through binding wrapper types --- Source/cmArgumentParserTypes.h | 40 +++++++++++++++++++++++++++++++++++ Tests/CMakeLib/testArgumentParser.cxx | 14 ++++++++++++ 2 files changed, 54 insertions(+) diff --git a/Source/cmArgumentParserTypes.h b/Source/cmArgumentParserTypes.h index 9afa5c7..fe8e4ca 100644 --- a/Source/cmArgumentParserTypes.h +++ b/Source/cmArgumentParserTypes.h @@ -4,21 +4,61 @@ #include "cmConfigure.h" // IWYU pragma: keep +#if defined(__SUNPRO_CC) + +# include +# include + +namespace ArgumentParser { + +template +struct Maybe; +template <> +struct Maybe : public std::string +{ + using std::string::basic_string; +}; + +template +struct MaybeEmpty; +template +struct MaybeEmpty> : public std::vector +{ + using std::vector::vector; +}; + +template +struct NonEmpty; +template +struct NonEmpty> : public std::vector +{ + using std::vector::vector; +}; + +} // namespace ArgumentParser + +#else + namespace ArgumentParser { template struct Maybe : public T { + using T::T; }; template struct MaybeEmpty : public T { + using T::T; }; template struct NonEmpty : public T { + using T::T; }; } // namespace ArgumentParser + +#endif diff --git a/Tests/CMakeLib/testArgumentParser.cxx b/Tests/CMakeLib/testArgumentParser.cxx index 5460356..dad5b85 100644 --- a/Tests/CMakeLib/testArgumentParser.cxx +++ b/Tests/CMakeLib/testArgumentParser.cxx @@ -39,6 +39,15 @@ struct Result : public ArgumentParser::ParseResult cm::optional>> Multi3; cm::optional>> Multi4; + ArgumentParser::Maybe UnboundMaybe{ 'u', 'n', 'b', 'o', + 'u', 'n', 'd' }; + ArgumentParser::MaybeEmpty> UnboundMaybeEmpty{ + 1, "unbound" + }; + ArgumentParser::NonEmpty> UnboundNonEmpty{ + 1, "unbound" + }; + std::vector ParsedKeywords; }; @@ -69,6 +78,7 @@ bool verifyResult(Result const& result, { static std::vector const foobar = { "foo", "bar" }; static std::vector const barfoo = { "bar", "foo" }; + static std::vector const unbound = { "unbound" }; static std::vector const parsedKeywords = { /* clang-format off */ "OPTION_1", @@ -133,6 +143,10 @@ bool verifyResult(Result const& result, ASSERT_TRUE(unparsedArguments.size() == 1); ASSERT_TRUE(unparsedArguments[0] == "bar"); + ASSERT_TRUE(result.UnboundMaybe == "unbound"); + ASSERT_TRUE(result.UnboundMaybeEmpty == unbound); + ASSERT_TRUE(result.UnboundNonEmpty == unbound); + ASSERT_TRUE(result.ParsedKeywords == parsedKeywords); ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size()); -- cgit v0.12 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 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 From 1f2eb63d1c77279f69e18144daa3d0eb0d035b01 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Jun 2022 10:15:46 -0400 Subject: cmArgumentParser: Add callback bindings --- Source/cmArgumentParser.h | 91 +++++++++++++++++++++++++++ Tests/CMakeLib/testArgumentParser.cxx | 113 +++++++++++++++++++++++++++++++++- 2 files changed, 201 insertions(+), 3 deletions(-) diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index d0406e4..fd7b69a 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -209,6 +209,70 @@ public: return *this; } + cmArgumentParser& Bind(cm::static_string_view name, + Continue (Result::*member)(cm::string_view), + ExpectAtLeast expect = { 1 }) + { + this->Base::Bind(name, [member, expect](Instance& instance) { + Result* result = static_cast(instance.Result); + instance.Bind( + [result, member](cm::string_view arg) -> Continue { + return (result->*member)(arg); + }, + expect); + }); + return *this; + } + + cmArgumentParser& Bind(cm::static_string_view name, + Continue (Result::*member)(cm::string_view, + cm::string_view), + ExpectAtLeast expect = { 1 }) + { + this->Base::Bind(name, [member, expect](Instance& instance) { + Result* result = static_cast(instance.Result); + cm::string_view keyword = instance.Keyword; + instance.Bind( + [result, member, keyword](cm::string_view arg) -> Continue { + return (result->*member)(keyword, arg); + }, + expect); + }); + return *this; + } + + cmArgumentParser& Bind(cm::static_string_view name, + std::function f, + ExpectAtLeast expect = { 1 }) + { + this->Base::Bind(name, [f, expect](Instance& instance) { + Result* result = static_cast(instance.Result); + instance.Bind( + [result, &f](cm::string_view arg) -> Continue { + return f(*result, arg); + }, + expect); + }); + return *this; + } + + cmArgumentParser& Bind( + cm::static_string_view name, + std::function f, + ExpectAtLeast expect = { 1 }) + { + this->Base::Bind(name, [f, expect](Instance& instance) { + Result* result = static_cast(instance.Result); + cm::string_view keyword = instance.Keyword; + instance.Bind( + [result, keyword, &f](cm::string_view arg) -> Continue { + return f(*result, keyword, arg); + }, + expect); + }); + return *this; + } + cmArgumentParser& BindParsedKeywords( std::vector Result::*member) { @@ -252,6 +316,33 @@ public: return *this; } + cmArgumentParser& Bind(cm::static_string_view name, + std::function f, + ExpectAtLeast expect = { 1 }) + { + this->Base::Bind(name, [f, expect](Instance& instance) { + instance.Bind([&f](cm::string_view arg) -> Continue { return f(arg); }, + expect); + }); + return *this; + } + + cmArgumentParser& Bind( + cm::static_string_view name, + std::function f, + ExpectAtLeast expect = { 1 }) + { + this->Base::Bind(name, [f, expect](Instance& instance) { + cm::string_view keyword = instance.Keyword; + instance.Bind( + [keyword, &f](cm::string_view arg) -> Continue { + return f(keyword, arg); + }, + expect); + }); + return *this; + } + cmArgumentParser& BindParsedKeywords(std::vector& ref) { this->Base::BindParsedKeyword( diff --git a/Tests/CMakeLib/testArgumentParser.cxx b/Tests/CMakeLib/testArgumentParser.cxx index dad5b85..d5533f8 100644 --- a/Tests/CMakeLib/testArgumentParser.cxx +++ b/Tests/CMakeLib/testArgumentParser.cxx @@ -1,6 +1,7 @@ /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying file Copyright.txt or https://cmake.org/licensing for details. */ +#include #include #include #include @@ -39,6 +40,43 @@ struct Result : public ArgumentParser::ParseResult cm::optional>> Multi3; cm::optional>> Multi4; + bool Func0_ = false; + ArgumentParser::Continue Func0(cm::string_view) + { + Func0_ = true; + return ArgumentParser::Continue::No; + } + + std::string Func1_; + ArgumentParser::Continue Func1(cm::string_view arg) + { + Func1_ = std::string(arg); + return ArgumentParser::Continue::No; + } + + std::map> Func2_; + ArgumentParser::Continue Func2(cm::string_view key, cm::string_view arg) + { + Func2_[std::string(key)].emplace_back(arg); + return key == "FUNC_2b" ? ArgumentParser::Continue::Yes + : ArgumentParser::Continue::No; + } + + std::vector Func3_; + ArgumentParser::Continue Func3(cm::string_view arg) + { + Func3_.emplace_back(arg); + return ArgumentParser::Continue::Yes; + } + + std::map> Func4_; + ArgumentParser::Continue Func4(cm::string_view key, cm::string_view arg) + { + Func4_[std::string(key)].emplace_back(arg); + return key == "FUNC_4b" ? ArgumentParser::Continue::Yes + : ArgumentParser::Continue::No; + } + ArgumentParser::Maybe UnboundMaybe{ 'u', 'n', 'b', 'o', 'u', 'n', 'd' }; ArgumentParser::MaybeEmpty> UnboundMaybeEmpty{ @@ -70,6 +108,13 @@ std::initializer_list const args = { "MULTI_3", "foo", "bar", // multi list with first list with two elems "MULTI_3", "bar", "foo", // multi list with second list with two elems // "MULTI_4", // multi list arg that is not present + "FUNC_0", // callback arg missing value + "FUNC_1", "foo", "ign1", // callback with one arg + unparsed value + "FUNC_2a", "foo", "ign2", // callback with keyword-dependent arg count + "FUNC_2b", "bar", "zot", // callback with keyword-dependent arg count + "FUNC_3", "foo", "bar", // callback with list arg ... + "FUNC_4a", "foo", "ign4", // callback with keyword-dependent arg count + "FUNC_4b", "bar", "zot", // callback with keyword-dependent arg count /* clang-format on */ }; @@ -94,13 +139,29 @@ bool verifyResult(Result const& result, "MULTI_2", "MULTI_3", "MULTI_3", + "FUNC_0", + "FUNC_1", + "FUNC_2a", + "FUNC_2b", + "FUNC_3", + "FUNC_4a", + "FUNC_4b", /* clang-format on */ }; + static std::map> const func2map = { + { "FUNC_2a", { "foo" } }, { "FUNC_2b", { "bar", "zot" } } + }; + static std::map> const func4map = { + { "FUNC_4a", { "foo" } }, { "FUNC_4b", { "bar", "zot" } } + }; static std::map const keywordErrors = { { "STRING_1"_s, " missing required value\n" }, { "LIST_1"_s, " missing required value\n" }, - { "LIST_4"_s, " missing required value\n" } + { "LIST_4"_s, " missing required value\n" }, + { "FUNC_0"_s, " missing required value\n" } }; + static std::vector const unparsed = { "bar", "ign1", "ign2", + "ign4" }; #define ASSERT_TRUE(x) \ do { \ @@ -140,8 +201,13 @@ bool verifyResult(Result const& result, ASSERT_TRUE((*result.Multi3)[1] == barfoo); ASSERT_TRUE(!result.Multi4); - ASSERT_TRUE(unparsedArguments.size() == 1); - ASSERT_TRUE(unparsedArguments[0] == "bar"); + ASSERT_TRUE(result.Func0_ == false); + ASSERT_TRUE(result.Func1_ == "foo"); + ASSERT_TRUE(result.Func2_ == func2map); + ASSERT_TRUE(result.Func3_ == foobar); + ASSERT_TRUE(result.Func4_ == func4map); + + ASSERT_TRUE(unparsedArguments == unparsed); ASSERT_TRUE(result.UnboundMaybe == "unbound"); ASSERT_TRUE(result.UnboundMaybeEmpty == unbound); @@ -164,6 +230,12 @@ bool testArgumentParserDynamic() Result result; std::vector unparsedArguments; + std::function + func4 = [&result](cm::string_view key, + cm::string_view arg) -> ArgumentParser::Continue { + return result.Func4(key, arg); + }; + static_cast(result) = cmArgumentParser{} .Bind("OPTION_1"_s, result.Option1) @@ -182,12 +254,37 @@ bool testArgumentParserDynamic() .Bind("MULTI_2"_s, result.Multi2) .Bind("MULTI_3"_s, result.Multi3) .Bind("MULTI_4"_s, result.Multi4) + .Bind("FUNC_0"_s, + [&result](cm::string_view arg) -> ArgumentParser::Continue { + return result.Func0(arg); + }) + .Bind("FUNC_1"_s, + [&result](cm::string_view arg) -> ArgumentParser::Continue { + return result.Func1(arg); + }) + .Bind("FUNC_2a"_s, + [&result](cm::string_view key, cm::string_view arg) + -> ArgumentParser::Continue { return result.Func2(key, arg); }) + .Bind("FUNC_2b"_s, + [&result](cm::string_view key, cm::string_view arg) + -> ArgumentParser::Continue { return result.Func2(key, arg); }) + .Bind("FUNC_3"_s, + [&result](cm::string_view arg) -> ArgumentParser::Continue { + return result.Func3(arg); + }) + .Bind("FUNC_4a"_s, func4) + .Bind("FUNC_4b"_s, func4) .BindParsedKeywords(result.ParsedKeywords) .Parse(args, &unparsedArguments); return verifyResult(result, unparsedArguments); } +static auto const parserStaticFunc4 = + [](Result& result, cm::string_view key, + cm::string_view arg) -> ArgumentParser::Continue { + return result.Func4(key, arg); +}; static auto const parserStatic = // cmArgumentParser{} .Bind("OPTION_1"_s, &Result::Option1) @@ -206,6 +303,16 @@ static auto const parserStatic = // .Bind("MULTI_2"_s, &Result::Multi2) .Bind("MULTI_3"_s, &Result::Multi3) .Bind("MULTI_4"_s, &Result::Multi4) + .Bind("FUNC_0"_s, &Result::Func0) + .Bind("FUNC_1"_s, &Result::Func1) + .Bind("FUNC_2a"_s, &Result::Func2) + .Bind("FUNC_2b"_s, &Result::Func2) + .Bind("FUNC_3"_s, + [](Result& result, cm::string_view arg) -> ArgumentParser::Continue { + return result.Func3(arg); + }) + .Bind("FUNC_4a"_s, parserStaticFunc4) + .Bind("FUNC_4b"_s, parserStaticFunc4) .BindParsedKeywords(&Result::ParsedKeywords) /* keep semicolon on own line */; -- cgit v0.12 From 236bacc2445ab93106bc9ed8c53299db9282f174 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 26 Jul 2022 16:25:29 -0400 Subject: cmArgumentParser: Offer bindings for positional arguments --- Source/cmArgumentParser.cxx | 27 ++++++++++++++- Source/cmArgumentParser.h | 64 +++++++++++++++++++++++++++++------ Tests/CMakeLib/testArgumentParser.cxx | 7 ++++ 3 files changed, 87 insertions(+), 11 deletions(-) diff --git a/Source/cmArgumentParser.cxx b/Source/cmArgumentParser.cxx index bd31b46..05f8ce8 100644 --- a/Source/cmArgumentParser.cxx +++ b/Source/cmArgumentParser.cxx @@ -34,6 +34,25 @@ auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator return (it != this->end() && it->first == name) ? it : this->end(); } +auto PositionActionMap::Emplace(std::size_t pos, PositionAction action) + -> std::pair +{ + auto const it = std::lower_bound( + this->begin(), this->end(), pos, + [](value_type const& elem, std::size_t k) { return elem.first < k; }); + return (it != this->end() && it->first == pos) + ? std::make_pair(it, false) + : std::make_pair(this->emplace(it, pos, std::move(action)), true); +} + +auto PositionActionMap::Find(std::size_t pos) const -> const_iterator +{ + auto const it = std::lower_bound( + this->begin(), this->end(), pos, + [](value_type const& elem, std::size_t k) { return elem.first < k; }); + return (it != this->end() && it->first == pos) ? it : this->end(); +} + void Instance::Bind(std::function f, ExpectAtLeast expect) { @@ -99,7 +118,7 @@ void Instance::Bind(std::vector>& multiVal) ExpectAtLeast{ 0 }); } -void Instance::Consume(cm::string_view arg) +void Instance::Consume(std::size_t pos, cm::string_view arg) { auto const it = this->Bindings.Keywords.Find(arg); if (it != this->Bindings.Keywords.end()) { @@ -125,6 +144,12 @@ void Instance::Consume(cm::string_view arg) return; } + auto const pit = this->Bindings.Positions.Find(pos); + if (pit != this->Bindings.Positions.end()) { + pit->second(*this, pos, arg); + return; + } + if (this->UnparsedArguments != nullptr) { this->UnparsedArguments->emplace_back(arg); } diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index fd7b69a..c25e2f6 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -5,6 +5,7 @@ #include "cmConfigure.h" // IWYU pragma: keep #include +#include #include #include #include @@ -81,6 +82,8 @@ struct ExpectAtLeast class Instance; using KeywordAction = std::function; using KeywordNameAction = std::function; +using PositionAction = + std::function; // using KeywordActionMap = cm::flat_map; class KeywordActionMap @@ -92,12 +95,22 @@ public: const_iterator Find(cm::string_view name) const; }; +// using PositionActionMap = cm::flat_map; +class PositionActionMap + : public std::vector> +{ +public: + std::pair Emplace(std::size_t pos, PositionAction action); + const_iterator Find(std::size_t pos) const; +}; + class ActionMap { public: KeywordActionMap Keywords; KeywordNameAction KeywordMissingValue; KeywordNameAction ParsedKeyword; + PositionActionMap Positions; }; class Base @@ -133,6 +146,14 @@ public: assert(!this->Bindings.KeywordMissingValue); this->Bindings.KeywordMissingValue = std::move(action); } + + void Bind(std::size_t pos, PositionAction action) + { + bool const inserted = + this->Bindings.Positions.Emplace(pos, std::move(action)).second; + assert(inserted); + static_cast(inserted); + } }; class Instance @@ -166,10 +187,10 @@ public: } template - void Parse(Range const& args) + void Parse(Range const& args, std::size_t pos = 0) { for (cm::string_view arg : args) { - this->Consume(arg); + this->Consume(pos++, arg); } this->FinishKeyword(); } @@ -185,7 +206,7 @@ private: std::size_t KeywordValuesExpected = 0; std::function KeywordValueFunc; - void Consume(cm::string_view arg); + void Consume(std::size_t pos, cm::string_view arg); void FinishKeyword(); template @@ -273,6 +294,18 @@ public: return *this; } + cmArgumentParser& Bind(std::size_t position, + cm::optional Result::*member) + { + this->Base::Bind( + position, + [member](Instance& instance, std::size_t, cm::string_view arg) { + Result* result = static_cast(instance.Result); + result->*member = arg; + }); + return *this; + } + cmArgumentParser& BindParsedKeywords( std::vector Result::*member) { @@ -285,22 +318,23 @@ public: template bool Parse(Result& result, Range const& args, - std::vector* unparsedArguments) const + std::vector* unparsedArguments, + std::size_t pos = 0) const { using ArgumentParser::AsParseResultPtr; ParseResult* parseResultPtr = AsParseResultPtr(result); Instance instance(this->Bindings, parseResultPtr, unparsedArguments, &result); - instance.Parse(args); + instance.Parse(args, pos); return parseResultPtr ? static_cast(*parseResultPtr) : true; } template - Result Parse(Range const& args, - std::vector* unparsedArguments) const + Result Parse(Range const& args, std::vector* unparsedArguments, + std::size_t pos = 0) const { Result result; - this->Parse(result, args, unparsedArguments); + this->Parse(result, args, unparsedArguments, pos); return result; } }; @@ -343,6 +377,15 @@ public: return *this; } + cmArgumentParser& Bind(std::size_t position, cm::optional& ref) + { + this->Base::Bind(position, + [&ref](Instance&, std::size_t, cm::string_view arg) { + ref = std::string(arg); + }); + return *this; + } + cmArgumentParser& BindParsedKeywords(std::vector& ref) { this->Base::BindParsedKeyword( @@ -352,11 +395,12 @@ public: template ParseResult Parse(Range const& args, - std::vector* unparsedArguments) const + std::vector* unparsedArguments, + std::size_t pos = 0) const { ParseResult parseResult; Instance instance(this->Bindings, &parseResult, unparsedArguments); - instance.Parse(args); + instance.Parse(args, pos); return parseResult; } diff --git a/Tests/CMakeLib/testArgumentParser.cxx b/Tests/CMakeLib/testArgumentParser.cxx index d5533f8..496b873 100644 --- a/Tests/CMakeLib/testArgumentParser.cxx +++ b/Tests/CMakeLib/testArgumentParser.cxx @@ -40,6 +40,8 @@ struct Result : public ArgumentParser::ParseResult cm::optional>> Multi3; cm::optional>> Multi4; + cm::optional Pos1; + bool Func0_ = false; ArgumentParser::Continue Func0(cm::string_view) { @@ -93,6 +95,7 @@ std::initializer_list const args = { /* clang-format off */ "OPTION_1", // option // "OPTION_2", // option that is not present + "pos1", // position index 1 "STRING_1", // string arg missing value "STRING_2", "foo", "bar", // string arg + unparsed value, presence captured // "STRING_3", // string arg that is not present @@ -201,6 +204,8 @@ bool verifyResult(Result const& result, ASSERT_TRUE((*result.Multi3)[1] == barfoo); ASSERT_TRUE(!result.Multi4); + ASSERT_TRUE(result.Pos1 == "pos1"); + ASSERT_TRUE(result.Func0_ == false); ASSERT_TRUE(result.Func1_ == "foo"); ASSERT_TRUE(result.Func2_ == func2map); @@ -254,6 +259,7 @@ bool testArgumentParserDynamic() .Bind("MULTI_2"_s, result.Multi2) .Bind("MULTI_3"_s, result.Multi3) .Bind("MULTI_4"_s, result.Multi4) + .Bind(1, result.Pos1) .Bind("FUNC_0"_s, [&result](cm::string_view arg) -> ArgumentParser::Continue { return result.Func0(arg); @@ -303,6 +309,7 @@ static auto const parserStatic = // .Bind("MULTI_2"_s, &Result::Multi2) .Bind("MULTI_3"_s, &Result::Multi3) .Bind("MULTI_4"_s, &Result::Multi4) + .Bind(1, &Result::Pos1) .Bind("FUNC_0"_s, &Result::Func0) .Bind("FUNC_1"_s, &Result::Func1) .Bind("FUNC_2a"_s, &Result::Func2) -- cgit v0.12