ParameterParser: refactoring, simplify
This commit is contained in:
parent
09901d455e
commit
43bc8314fb
|
@ -5,12 +5,15 @@ namespace Kakoune
|
||||||
|
|
||||||
ParametersParser::ParametersParser(const ParameterList& params,
|
ParametersParser::ParametersParser(const ParameterList& params,
|
||||||
std::unordered_map<String, bool> options)
|
std::unordered_map<String, bool> options)
|
||||||
: m_params(params), m_positional(params.size(), true),
|
: m_params(params),
|
||||||
m_options(std::move(options))
|
m_options(std::move(options))
|
||||||
{
|
{
|
||||||
|
bool only_pos = false;
|
||||||
for (size_t i = 0; i < params.size(); ++i)
|
for (size_t i = 0; i < params.size(); ++i)
|
||||||
{
|
{
|
||||||
if (params[i][0] == '-')
|
if (params[i] == "--")
|
||||||
|
only_pos = true;
|
||||||
|
else if (not only_pos and params[i][0] == '-')
|
||||||
{
|
{
|
||||||
auto it = m_options.find(params[i].substr(1_byte));
|
auto it = m_options.find(params[i].substr(1_byte));
|
||||||
if (it == m_options.end())
|
if (it == m_options.end())
|
||||||
|
@ -18,17 +21,13 @@ ParametersParser::ParametersParser(const ParameterList& params,
|
||||||
|
|
||||||
if (it->second)
|
if (it->second)
|
||||||
{
|
{
|
||||||
if (i + 1 == params.size() or params[i+1][0] == '-')
|
++i;
|
||||||
|
if (i == params.size() or params[i][0] == '-')
|
||||||
throw missing_option_value(params[i]);
|
throw missing_option_value(params[i]);
|
||||||
|
|
||||||
m_positional[i+1] = false;
|
|
||||||
}
|
}
|
||||||
m_positional[i] = false;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// all options following -- are positional
|
m_positional_indices.push_back(i);
|
||||||
if (params[i] == "--")
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,38 +67,23 @@ const String& ParametersParser::option_value(const String& name) const
|
||||||
|
|
||||||
size_t ParametersParser::positional_count() const
|
size_t ParametersParser::positional_count() const
|
||||||
{
|
{
|
||||||
size_t res = 0;
|
return m_positional_indices.size();
|
||||||
for (bool positional : m_positional)
|
|
||||||
{
|
|
||||||
if (positional)
|
|
||||||
++res;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const String& ParametersParser::operator[] (size_t index) const
|
const String& ParametersParser::operator[] (size_t index) const
|
||||||
{
|
{
|
||||||
assert(index < positional_count());
|
assert(index < positional_count());
|
||||||
iterator it = begin();
|
return m_params[m_positional_indices[index]];
|
||||||
while (index)
|
|
||||||
{
|
|
||||||
++it;
|
|
||||||
--index;
|
|
||||||
}
|
|
||||||
return *it;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ParametersParser::iterator ParametersParser::begin() const
|
ParametersParser::iterator ParametersParser::begin() const
|
||||||
{
|
{
|
||||||
int index = 0;
|
return iterator(*this, 0);
|
||||||
while (index < m_positional.size() and not m_positional[index])
|
|
||||||
++index;
|
|
||||||
return iterator(*this, index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ParametersParser::iterator ParametersParser::end() const
|
ParametersParser::iterator ParametersParser::end() const
|
||||||
{
|
{
|
||||||
return iterator(*this, m_params.size());
|
return iterator(*this, m_positional_indices.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,31 +61,26 @@ struct ParametersParser
|
||||||
|
|
||||||
const String& operator*() const
|
const String& operator*() const
|
||||||
{
|
{
|
||||||
assert(m_parser.m_positional[m_index]);
|
return m_parser.m_params[m_parser.m_positional_indices[m_index]];
|
||||||
return m_parser.m_params[m_index];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const String* operator->() const
|
const String* operator->() const
|
||||||
{
|
{
|
||||||
assert(m_parser.m_positional[m_index]);
|
return &m_parser.m_params[m_parser.m_positional_indices[m_index]];
|
||||||
return &m_parser.m_params[m_index];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator& operator++()
|
iterator& operator++() { ++m_index; return *this; }
|
||||||
{
|
|
||||||
while (m_index < m_parser.m_positional.size() and
|
|
||||||
not m_parser.m_positional[++m_index]) {}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const iterator& other) const
|
bool operator==(const iterator& other) const
|
||||||
{
|
{
|
||||||
return &m_parser == &other.m_parser and m_index == other.m_index;
|
assert(&m_parser == &other.m_parser);
|
||||||
|
return m_index == other.m_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const iterator& other) const
|
bool operator!=(const iterator& other) const
|
||||||
{
|
{
|
||||||
return &m_parser != &other.m_parser or m_index != other.m_index;
|
assert(&m_parser == &other.m_parser);
|
||||||
|
return m_index != other.m_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const iterator& other) const
|
bool operator<(const iterator& other) const
|
||||||
|
@ -108,7 +103,7 @@ struct ParametersParser
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ParameterList m_params;
|
ParameterList m_params;
|
||||||
std::vector<bool> m_positional;
|
std::vector<size_t> m_positional_indices;
|
||||||
std::unordered_map<String, bool> m_options;
|
std::unordered_map<String, bool> m_options;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user