|
|
@@ -16,10 +16,13 @@
|
|
|
|
|
|
#include "cmArgumentParserTypes.h" // IWYU pragma: keep
|
|
|
|
|
|
+template <typename Result>
|
|
|
+class cmArgumentParser; // IWYU pragma: keep
|
|
|
+
|
|
|
namespace ArgumentParser {
|
|
|
|
|
|
class Instance;
|
|
|
-using Action = std::function<void(Instance&, void*)>;
|
|
|
+using Action = std::function<void(Instance&)>;
|
|
|
|
|
|
// using ActionMap = cm::flat_map<cm::string_view, Action>;
|
|
|
class ActionMap : public std::vector<std::pair<cm::string_view, Action>>
|
|
|
@@ -32,8 +35,16 @@ public:
|
|
|
class Instance
|
|
|
{
|
|
|
public:
|
|
|
- Instance(ActionMap const& bindings)
|
|
|
+ Instance(ActionMap const& bindings,
|
|
|
+ std::vector<std::string>* unparsedArguments,
|
|
|
+ std::vector<cm::string_view>* keywordsMissingValue,
|
|
|
+ std::vector<cm::string_view>* parsedKeywords,
|
|
|
+ void* result = nullptr)
|
|
|
: Bindings(bindings)
|
|
|
+ , UnparsedArguments(unparsedArguments)
|
|
|
+ , KeywordsMissingValue(keywordsMissingValue)
|
|
|
+ , ParsedKeywords(parsedKeywords)
|
|
|
+ , Result(result)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
@@ -54,16 +65,21 @@ public:
|
|
|
this->Bind(*optVal);
|
|
|
}
|
|
|
|
|
|
- void Consume(cm::string_view arg, void* result,
|
|
|
- std::vector<std::string>* unparsedArguments,
|
|
|
- std::vector<cm::string_view>* keywordsMissingValue,
|
|
|
- std::vector<cm::string_view>* parsedKeywords);
|
|
|
+ void Consume(cm::string_view arg);
|
|
|
|
|
|
private:
|
|
|
ActionMap const& Bindings;
|
|
|
+ std::vector<std::string>* UnparsedArguments = nullptr;
|
|
|
+ std::vector<cm::string_view>* KeywordsMissingValue = nullptr;
|
|
|
+ std::vector<cm::string_view>* ParsedKeywords = nullptr;
|
|
|
+ void* Result = nullptr;
|
|
|
+
|
|
|
std::string* CurrentString = nullptr;
|
|
|
std::vector<std::string>* CurrentList = nullptr;
|
|
|
bool ExpectValue = false;
|
|
|
+
|
|
|
+ template <typename Result>
|
|
|
+ friend class ::cmArgumentParser;
|
|
|
};
|
|
|
|
|
|
} // namespace ArgumentParser
|
|
|
@@ -80,8 +96,9 @@ public:
|
|
|
bool const inserted =
|
|
|
this->Bindings
|
|
|
.Emplace(name,
|
|
|
- [member](ArgumentParser::Instance& instance, void* result) {
|
|
|
- instance.Bind(static_cast<Result*>(result)->*member);
|
|
|
+ [member](ArgumentParser::Instance& instance) {
|
|
|
+ instance.Bind(
|
|
|
+ static_cast<Result*>(instance.Result)->*member);
|
|
|
})
|
|
|
.second;
|
|
|
assert(inserted), (void)inserted;
|
|
|
@@ -94,10 +111,11 @@ public:
|
|
|
std::vector<cm::string_view>* keywordsMissingValue = nullptr,
|
|
|
std::vector<cm::string_view>* parsedKeywords = nullptr) const
|
|
|
{
|
|
|
- ArgumentParser::Instance instance(this->Bindings);
|
|
|
+ ArgumentParser::Instance instance(this->Bindings, unparsedArguments,
|
|
|
+ keywordsMissingValue, parsedKeywords,
|
|
|
+ &result);
|
|
|
for (cm::string_view arg : args) {
|
|
|
- instance.Consume(arg, &result, unparsedArguments, keywordsMissingValue,
|
|
|
- parsedKeywords);
|
|
|
+ instance.Consume(arg);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -133,10 +151,10 @@ public:
|
|
|
std::vector<cm::string_view>* keywordsMissingValue = nullptr,
|
|
|
std::vector<cm::string_view>* parsedKeywords = nullptr) const
|
|
|
{
|
|
|
- ArgumentParser::Instance instance(this->Bindings);
|
|
|
+ ArgumentParser::Instance instance(this->Bindings, unparsedArguments,
|
|
|
+ keywordsMissingValue, parsedKeywords);
|
|
|
for (cm::string_view arg : args) {
|
|
|
- instance.Consume(arg, nullptr, unparsedArguments, keywordsMissingValue,
|
|
|
- parsedKeywords);
|
|
|
+ instance.Consume(arg);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -145,10 +163,9 @@ protected:
|
|
|
bool Bind(cm::string_view name, T& ref)
|
|
|
{
|
|
|
return this->Bindings
|
|
|
- .Emplace(name,
|
|
|
- [&ref](ArgumentParser::Instance& instance, void*) {
|
|
|
- instance.Bind(ref);
|
|
|
- })
|
|
|
+ .Emplace(
|
|
|
+ name,
|
|
|
+ [&ref](ArgumentParser::Instance& instance) { instance.Bind(ref); })
|
|
|
.second;
|
|
|
}
|
|
|
|