mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-12 01:09:47 -06:00
cmArgumentParser: Offer bindings for positional arguments
This commit is contained in:
@@ -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<iterator, bool>
|
||||
{
|
||||
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<Continue(cm::string_view)> f,
|
||||
ExpectAtLeast expect)
|
||||
{
|
||||
@@ -99,7 +118,7 @@ void Instance::Bind(std::vector<std::vector<std::string>>& 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);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "cmConfigure.h" // IWYU pragma: keep
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
@@ -81,6 +82,8 @@ struct ExpectAtLeast
|
||||
class Instance;
|
||||
using KeywordAction = std::function<void(Instance&)>;
|
||||
using KeywordNameAction = std::function<void(Instance&, cm::string_view)>;
|
||||
using PositionAction =
|
||||
std::function<void(Instance&, std::size_t, cm::string_view)>;
|
||||
|
||||
// using KeywordActionMap = cm::flat_map<cm::string_view, KeywordAction>;
|
||||
class KeywordActionMap
|
||||
@@ -92,12 +95,22 @@ public:
|
||||
const_iterator Find(cm::string_view name) const;
|
||||
};
|
||||
|
||||
// using PositionActionMap = cm::flat_map<cm::string_view, PositionAction>;
|
||||
class PositionActionMap
|
||||
: public std::vector<std::pair<std::size_t, PositionAction>>
|
||||
{
|
||||
public:
|
||||
std::pair<iterator, bool> 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<void>(inserted);
|
||||
}
|
||||
};
|
||||
|
||||
class Instance
|
||||
@@ -166,10 +187,10 @@ public:
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
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<Continue(cm::string_view)> KeywordValueFunc;
|
||||
|
||||
void Consume(cm::string_view arg);
|
||||
void Consume(std::size_t pos, cm::string_view arg);
|
||||
void FinishKeyword();
|
||||
|
||||
template <typename Result>
|
||||
@@ -273,6 +294,18 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
cmArgumentParser& Bind(std::size_t position,
|
||||
cm::optional<std::string> Result::*member)
|
||||
{
|
||||
this->Base::Bind(
|
||||
position,
|
||||
[member](Instance& instance, std::size_t, cm::string_view arg) {
|
||||
Result* result = static_cast<Result*>(instance.Result);
|
||||
result->*member = arg;
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
cmArgumentParser& BindParsedKeywords(
|
||||
std::vector<cm::string_view> Result::*member)
|
||||
{
|
||||
@@ -285,22 +318,23 @@ public:
|
||||
|
||||
template <typename Range>
|
||||
bool Parse(Result& result, Range const& args,
|
||||
std::vector<std::string>* unparsedArguments) const
|
||||
std::vector<std::string>* 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<bool>(*parseResultPtr) : true;
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
Result Parse(Range const& args,
|
||||
std::vector<std::string>* unparsedArguments) const
|
||||
Result Parse(Range const& args, std::vector<std::string>* 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<std::string>& ref)
|
||||
{
|
||||
this->Base::Bind(position,
|
||||
[&ref](Instance&, std::size_t, cm::string_view arg) {
|
||||
ref = std::string(arg);
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
cmArgumentParser& BindParsedKeywords(std::vector<cm::string_view>& ref)
|
||||
{
|
||||
this->Base::BindParsedKeyword(
|
||||
@@ -352,11 +395,12 @@ public:
|
||||
|
||||
template <typename Range>
|
||||
ParseResult Parse(Range const& args,
|
||||
std::vector<std::string>* unparsedArguments) const
|
||||
std::vector<std::string>* unparsedArguments,
|
||||
std::size_t pos = 0) const
|
||||
{
|
||||
ParseResult parseResult;
|
||||
Instance instance(this->Bindings, &parseResult, unparsedArguments);
|
||||
instance.Parse(args);
|
||||
instance.Parse(args, pos);
|
||||
return parseResult;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,8 @@ struct Result : public ArgumentParser::ParseResult
|
||||
cm::optional<std::vector<std::vector<std::string>>> Multi3;
|
||||
cm::optional<std::vector<std::vector<std::string>>> Multi4;
|
||||
|
||||
cm::optional<std::string> Pos1;
|
||||
|
||||
bool Func0_ = false;
|
||||
ArgumentParser::Continue Func0(cm::string_view)
|
||||
{
|
||||
@@ -93,6 +95,7 @@ std::initializer_list<cm::string_view> 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)
|
||||
|
||||
Reference in New Issue
Block a user