cmArgumentParser: Store keyword action map with explicit name

This commit is contained in:
Brad King
2022-06-20 10:17:22 -04:00
parent 119e1f7fbc
commit 5955ec1992
2 changed files with 19 additions and 11 deletions
+4 -4
View File
@@ -8,7 +8,7 @@
namespace ArgumentParser { namespace ArgumentParser {
auto ActionMap::Emplace(cm::string_view name, Action action) auto KeywordActionMap::Emplace(cm::string_view name, KeywordAction action)
-> std::pair<iterator, bool> -> std::pair<iterator, bool>
{ {
auto const it = auto const it =
@@ -21,7 +21,7 @@ auto ActionMap::Emplace(cm::string_view name, Action action)
: std::make_pair(this->emplace(it, name, std::move(action)), true); : std::make_pair(this->emplace(it, name, std::move(action)), true);
} }
auto ActionMap::Find(cm::string_view name) const -> const_iterator auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator
{ {
auto const it = auto const it =
std::lower_bound(this->begin(), this->end(), name, std::lower_bound(this->begin(), this->end(), name,
@@ -76,8 +76,8 @@ void Instance::Bind(std::vector<std::vector<std::string>>& val)
void Instance::Consume(cm::string_view arg) void Instance::Consume(cm::string_view arg)
{ {
auto const it = this->Bindings.Find(arg); auto const it = this->Bindings.Keywords.Find(arg);
if (it != this->Bindings.end()) { if (it != this->Bindings.Keywords.end()) {
this->FinishKeyword(); this->FinishKeyword();
this->Keyword = it->first; this->Keyword = it->first;
if (this->ParsedKeywords != nullptr) { if (this->ParsedKeywords != nullptr) {
+15 -7
View File
@@ -22,16 +22,24 @@ class cmArgumentParser; // IWYU pragma: keep
namespace ArgumentParser { namespace ArgumentParser {
class Instance; class Instance;
using Action = std::function<void(Instance&)>; using KeywordAction = std::function<void(Instance&)>;
// using ActionMap = cm::flat_map<cm::string_view, Action>; // using KeywordActionMap = cm::flat_map<cm::string_view, KeywordAction>;
class ActionMap : public std::vector<std::pair<cm::string_view, Action>> class KeywordActionMap
: public std::vector<std::pair<cm::string_view, KeywordAction>>
{ {
public: public:
std::pair<iterator, bool> Emplace(cm::string_view name, Action action); std::pair<iterator, bool> Emplace(cm::string_view name,
KeywordAction action);
const_iterator Find(cm::string_view name) const; const_iterator Find(cm::string_view name) const;
}; };
class ActionMap
{
public:
KeywordActionMap Keywords;
};
class Base class Base
{ {
public: public:
@@ -39,12 +47,12 @@ public:
ArgumentParser::ActionMap Bindings; ArgumentParser::ActionMap Bindings;
bool MaybeBind(cm::string_view name, Action action) bool MaybeBind(cm::string_view name, KeywordAction action)
{ {
return this->Bindings.Emplace(name, std::move(action)).second; return this->Bindings.Keywords.Emplace(name, std::move(action)).second;
} }
void Bind(cm::string_view name, Action action) void Bind(cm::string_view name, KeywordAction action)
{ {
bool const inserted = this->MaybeBind(name, std::move(action)); bool const inserted = this->MaybeBind(name, std::move(action));
assert(inserted); assert(inserted);