2012-12-07 17:27:10 +04:00
|
|
|
#include "parameters_parser.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-02-12 02:23:44 +04:00
|
|
|
String generate_switches_doc(const SwitchMap& switches)
|
2014-02-12 02:16:17 +04:00
|
|
|
{
|
|
|
|
String res;
|
2014-02-12 02:23:44 +04:00
|
|
|
for (auto& sw : switches)
|
|
|
|
res += " -" + sw.first + (sw.second.takes_arg ? " <arg>: " : ": ") + sw.second.description + "\n";
|
2014-02-12 02:16:17 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-02-08 05:02:58 +04:00
|
|
|
ParametersParser::ParametersParser(ParameterList params,
|
|
|
|
const ParameterDesc& desc)
|
2013-02-27 23:51:44 +04:00
|
|
|
: m_params(params),
|
2014-02-08 05:02:58 +04:00
|
|
|
m_desc(desc)
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
2014-03-20 23:50:42 +04:00
|
|
|
bool only_pos = desc.flags & ParameterDesc::Flags::SwitchesAsPositional;
|
2012-12-07 17:27:10 +04:00
|
|
|
for (size_t i = 0; i < params.size(); ++i)
|
|
|
|
{
|
2014-08-27 01:10:54 +04:00
|
|
|
if (not only_pos and params[i] == "--")
|
2013-02-27 23:51:44 +04:00
|
|
|
only_pos = true;
|
2015-03-10 22:33:46 +03:00
|
|
|
else if (not only_pos and params[i][0_byte] == '-')
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
2014-02-12 02:23:44 +04:00
|
|
|
auto it = m_desc.switches.find(params[i].substr(1_byte));
|
|
|
|
if (it == m_desc.switches.end())
|
2012-12-07 17:27:10 +04:00
|
|
|
throw unknown_option(params[i]);
|
|
|
|
|
2014-02-12 02:16:17 +04:00
|
|
|
if (it->second.takes_arg)
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
2013-02-27 23:51:44 +04:00
|
|
|
++i;
|
2015-03-10 22:33:46 +03:00
|
|
|
if (i == params.size() or params[i][0_byte] == '-')
|
2014-03-25 12:21:16 +04:00
|
|
|
throw missing_option_value(it->first);
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
}
|
2013-02-27 23:51:44 +04:00
|
|
|
else
|
2013-04-02 16:22:55 +04:00
|
|
|
{
|
2014-02-12 02:23:44 +04:00
|
|
|
if (desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart)
|
2013-04-02 16:22:55 +04:00
|
|
|
only_pos = true;
|
2013-02-27 23:51:44 +04:00
|
|
|
m_positional_indices.push_back(i);
|
2013-04-02 16:22:55 +04:00
|
|
|
}
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
2013-03-27 17:27:12 +04:00
|
|
|
size_t count = m_positional_indices.size();
|
2014-02-08 05:02:58 +04:00
|
|
|
if (count > desc.max_positionals or count < desc.min_positionals)
|
2013-03-27 17:27:12 +04:00
|
|
|
throw wrong_argument_count();
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
|
2015-03-14 22:16:46 +03:00
|
|
|
Optional<StringView> ParametersParser::get_switch(StringView name) const
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
2014-02-12 02:23:44 +04:00
|
|
|
auto it = m_desc.switches.find(name);
|
|
|
|
kak_assert(it != m_desc.switches.end());
|
2012-12-07 17:27:10 +04:00
|
|
|
for (size_t i = 0; i < m_params.size(); ++i)
|
|
|
|
{
|
2015-03-14 22:16:46 +03:00
|
|
|
const auto& param = m_params[i];
|
|
|
|
if (param[0_byte] == '-' and param.substr(1_byte) == name)
|
|
|
|
return it->second.takes_arg ? m_params[i+1] : StringView{};
|
2012-12-07 17:27:10 +04:00
|
|
|
|
2015-03-14 22:16:46 +03:00
|
|
|
if (param == "--")
|
2012-12-07 17:27:10 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-03-14 22:16:46 +03:00
|
|
|
return {};
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|