2012-12-07 14:27:10 +01:00
|
|
|
#include "parameters_parser.hh"
|
|
|
|
|
2017-03-15 19:25:59 +01:00
|
|
|
#include "flags.hh"
|
|
|
|
|
2012-12-07 14:27:10 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-02-11 23:23:44 +01:00
|
|
|
String generate_switches_doc(const SwitchMap& switches)
|
2014-02-11 23:16:17 +01:00
|
|
|
{
|
|
|
|
String res;
|
2018-02-28 05:17:51 +01:00
|
|
|
if (switches.empty())
|
|
|
|
return res;
|
|
|
|
|
|
|
|
auto switch_len = [](auto& sw) { return sw.key.column_length() + (sw.value.takes_arg ? 5 : 0); };
|
|
|
|
auto switches_len = switches | transform(switch_len);
|
|
|
|
const ColumnCount maxlen = *std::max_element(switches_len.begin(), switches_len.end());
|
|
|
|
|
2018-02-25 01:56:33 +01:00
|
|
|
for (auto& sw : switches) {
|
2018-10-28 13:34:19 +01:00
|
|
|
res += format("-{} {}{}{}\n",
|
2018-02-25 01:56:33 +01:00
|
|
|
sw.key,
|
2016-09-06 00:34:03 +02:00
|
|
|
sw.value.takes_arg ? "<arg>" : "",
|
2018-02-28 05:17:51 +01:00
|
|
|
String{' ', maxlen - switch_len(sw) + 1},
|
2016-09-06 00:34:03 +02:00
|
|
|
sw.value.description);
|
2018-02-25 01:56:33 +01:00
|
|
|
}
|
2014-02-11 23:16:17 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:36:06 +02:00
|
|
|
ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& desc)
|
2013-02-27 20:51:44 +01:00
|
|
|
: m_params(params),
|
2014-02-08 02:02:58 +01:00
|
|
|
m_desc(desc)
|
2012-12-07 14:27:10 +01:00
|
|
|
{
|
2018-06-29 00:36:06 +02:00
|
|
|
const bool switches_only_at_start = desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart;
|
|
|
|
const bool ignore_unknown_switches = desc.flags & ParameterDesc::Flags::IgnoreUnknownSwitches;
|
2014-03-20 20:50:42 +01:00
|
|
|
bool only_pos = desc.flags & ParameterDesc::Flags::SwitchesAsPositional;
|
2018-06-29 00:36:06 +02:00
|
|
|
|
2017-02-15 19:55:11 +01:00
|
|
|
Vector<bool> switch_seen(desc.switches.size(), false);
|
2012-12-07 14:27:10 +01:00
|
|
|
for (size_t i = 0; i < params.size(); ++i)
|
|
|
|
{
|
2018-06-29 00:36:06 +02:00
|
|
|
if (not only_pos and not ignore_unknown_switches and params[i] == "--")
|
2013-02-27 20:51:44 +01:00
|
|
|
only_pos = true;
|
2016-12-27 23:01:11 +01:00
|
|
|
else if (not only_pos and not params[i].empty() and params[i][0_byte] == '-')
|
2012-12-07 14:27:10 +01:00
|
|
|
{
|
2014-02-11 23:23:44 +01:00
|
|
|
auto it = m_desc.switches.find(params[i].substr(1_byte));
|
|
|
|
if (it == m_desc.switches.end())
|
2018-06-29 00:36:06 +02:00
|
|
|
{
|
|
|
|
if (ignore_unknown_switches)
|
|
|
|
{
|
|
|
|
m_positional_indices.push_back(i);
|
|
|
|
if (switches_only_at_start)
|
|
|
|
only_pos = true;
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-07 14:27:10 +01:00
|
|
|
throw unknown_option(params[i]);
|
2018-06-29 00:36:06 +02:00
|
|
|
}
|
2012-12-07 14:27:10 +01:00
|
|
|
|
2017-02-15 19:55:11 +01:00
|
|
|
auto switch_index = it - m_desc.switches.begin();
|
|
|
|
if (switch_seen[switch_index])
|
|
|
|
throw runtime_error{format("switch '-{}' specified more than once", it->key)};
|
|
|
|
switch_seen[switch_index] = true;
|
|
|
|
|
2017-07-23 23:21:51 +02:00
|
|
|
if (it->value.takes_arg and ++i == params.size())
|
|
|
|
throw missing_option_value(it->key);
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
2016-12-25 02:42:31 +01:00
|
|
|
else // positional
|
2013-04-02 14:22:55 +02:00
|
|
|
{
|
2018-06-29 00:36:06 +02:00
|
|
|
if (switches_only_at_start)
|
2013-04-02 14:22:55 +02:00
|
|
|
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();
|
2014-02-08 02:02:58 +01:00
|
|
|
if (count > desc.max_positionals or count < desc.min_positionals)
|
2013-03-27 14:27:12 +01:00
|
|
|
throw wrong_argument_count();
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
|
|
|
|
2015-03-14 20:16:46 +01:00
|
|
|
Optional<StringView> ParametersParser::get_switch(StringView name) const
|
2012-12-07 14:27:10 +01:00
|
|
|
{
|
2014-02-11 23:23:44 +01:00
|
|
|
auto it = m_desc.switches.find(name);
|
|
|
|
kak_assert(it != m_desc.switches.end());
|
2012-12-07 14:27:10 +01:00
|
|
|
for (size_t i = 0; i < m_params.size(); ++i)
|
|
|
|
{
|
2015-03-14 20:16:46 +01:00
|
|
|
const auto& param = m_params[i];
|
2018-02-26 05:20:19 +01:00
|
|
|
if (param.substr(0_byte, 1_byte) == "-" and param.substr(1_byte) == name)
|
2015-09-16 20:57:57 +02:00
|
|
|
return it->value.takes_arg ? m_params[i+1] : StringView{};
|
2012-12-07 14:27:10 +01:00
|
|
|
|
2015-03-14 20:16:46 +01:00
|
|
|
if (param == "--")
|
2012-12-07 14:27:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-03-14 20:16:46 +01:00
|
|
|
return {};
|
2012-12-07 14:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|