2012-12-07 14:27:10 +01:00
|
|
|
#include "parameters_parser.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
ParametersParser::ParametersParser(const ParameterList& params,
|
2013-03-27 14:27:12 +01:00
|
|
|
std::unordered_map<String, bool> options,
|
2013-04-02 14:22:55 +02:00
|
|
|
Flags flags,
|
2013-03-27 14:27:12 +01:00
|
|
|
size_t min_positionals,
|
|
|
|
size_t max_positionals)
|
2013-02-27 20:51:44 +01:00
|
|
|
: m_params(params),
|
2012-12-07 14:27:10 +01:00
|
|
|
m_options(std::move(options))
|
|
|
|
{
|
2013-02-27 20:51:44 +01:00
|
|
|
bool only_pos = false;
|
2012-12-07 14:27:10 +01:00
|
|
|
for (size_t i = 0; i < params.size(); ++i)
|
|
|
|
{
|
2013-02-27 20:51:44 +01:00
|
|
|
if (params[i] == "--")
|
|
|
|
only_pos = true;
|
|
|
|
else if (not only_pos and params[i][0] == '-')
|
2012-12-07 14:27:10 +01:00
|
|
|
{
|
|
|
|
auto it = m_options.find(params[i].substr(1_byte));
|
|
|
|
if (it == m_options.end())
|
|
|
|
throw unknown_option(params[i]);
|
|
|
|
|
|
|
|
if (it->second)
|
|
|
|
{
|
2013-02-27 20:51:44 +01:00
|
|
|
++i;
|
|
|
|
if (i == params.size() or params[i][0] == '-')
|
2012-12-07 14:27:10 +01:00
|
|
|
throw missing_option_value(params[i]);
|
|
|
|
}
|
|
|
|
}
|
2013-02-27 20:51:44 +01:00
|
|
|
else
|
2013-04-02 14:22:55 +02:00
|
|
|
{
|
|
|
|
if (flags & Flags::OptionsOnlyAtStart)
|
|
|
|
only_pos = true;
|
2013-02-27 20:51:44 +01:00
|
|
|
m_positional_indices.push_back(i);
|
2013-04-02 14:22:55 +02:00
|
|
|
}
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
2013-03-27 14:27:12 +01:00
|
|
|
size_t count = m_positional_indices.size();
|
|
|
|
if (count > max_positionals or count < min_positionals)
|
|
|
|
throw wrong_argument_count();
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ParametersParser::has_option(const String& name) const
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(m_options.find(name) != m_options.end());
|
2012-12-07 14:27:10 +01:00
|
|
|
for (auto& param : m_params)
|
|
|
|
{
|
|
|
|
if (param[0] == '-' and param.substr(1_byte) == name)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (param == "--")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const String& ParametersParser::option_value(const String& name) const
|
|
|
|
{
|
2013-02-27 19:02:01 +01:00
|
|
|
#ifdef KAK_DEBUG
|
2012-12-07 14:27:10 +01:00
|
|
|
auto it = m_options.find(name);
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(it != m_options.end());
|
|
|
|
kak_assert(it->second == true);
|
2013-02-27 19:02:01 +01:00
|
|
|
#endif
|
2012-12-07 14:27:10 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < m_params.size(); ++i)
|
|
|
|
{
|
|
|
|
if (m_params[i][0] == '-' and m_params[i].substr(1_byte) == name)
|
|
|
|
return m_params[i+1];
|
|
|
|
|
|
|
|
if (m_params[i] == "--")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
static String empty;
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ParametersParser::positional_count() const
|
|
|
|
{
|
2013-02-27 20:51:44 +01:00
|
|
|
return m_positional_indices.size();
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const String& ParametersParser::operator[] (size_t index) const
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(index < positional_count());
|
2013-02-27 20:51:44 +01:00
|
|
|
return m_params[m_positional_indices[index]];
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ParametersParser::iterator ParametersParser::begin() const
|
|
|
|
{
|
2013-02-27 20:51:44 +01:00
|
|
|
return iterator(*this, 0);
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ParametersParser::iterator ParametersParser::end() const
|
|
|
|
{
|
2013-02-27 20:51:44 +01:00
|
|
|
return iterator(*this, m_positional_indices.size());
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|