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

View File

@@ -8,7 +8,7 @@
namespace ArgumentParser {
auto ActionMap::Emplace(cm::string_view name, Action action)
auto KeywordActionMap::Emplace(cm::string_view name, KeywordAction action)
-> std::pair<iterator, bool>
{
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);
}
auto ActionMap::Find(cm::string_view name) const -> const_iterator
auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator
{
auto const it =
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)
{
auto const it = this->Bindings.Find(arg);
if (it != this->Bindings.end()) {
auto const it = this->Bindings.Keywords.Find(arg);
if (it != this->Bindings.Keywords.end()) {
this->FinishKeyword();
this->Keyword = it->first;
if (this->ParsedKeywords != nullptr) {

View File

@@ -22,16 +22,24 @@ class cmArgumentParser; // IWYU pragma: keep
namespace ArgumentParser {
class Instance;
using Action = std::function<void(Instance&)>;
using KeywordAction = std::function<void(Instance&)>;
// using ActionMap = cm::flat_map<cm::string_view, Action>;
class ActionMap : public std::vector<std::pair<cm::string_view, Action>>
// using KeywordActionMap = cm::flat_map<cm::string_view, KeywordAction>;
class KeywordActionMap
: public std::vector<std::pair<cm::string_view, KeywordAction>>
{
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;
};
class ActionMap
{
public:
KeywordActionMap Keywords;
};
class Base
{
public:
@@ -39,12 +47,12 @@ public:
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));
assert(inserted);