Do not keep a reference to ParametersDesc inside ParameterParser

This should fix #2900
This commit is contained in:
Maxime Coste 2019-05-10 23:46:16 +10:00
parent f96547719d
commit 834f6916da
2 changed files with 12 additions and 19 deletions

View File

@ -26,8 +26,7 @@ String generate_switches_doc(const SwitchMap& switches)
} }
ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& desc) ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& desc)
: m_params(params), : m_params(params)
m_desc(desc)
{ {
const bool switches_only_at_start = desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart; const bool switches_only_at_start = desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart;
const bool ignore_unknown_switches = desc.flags & ParameterDesc::Flags::IgnoreUnknownSwitches; const bool ignore_unknown_switches = desc.flags & ParameterDesc::Flags::IgnoreUnknownSwitches;
@ -40,8 +39,9 @@ ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& de
only_pos = true; only_pos = true;
else if (not only_pos and not params[i].empty() and params[i][0_byte] == '-') else if (not only_pos and not params[i].empty() and params[i][0_byte] == '-')
{ {
auto it = m_desc.switches.find(params[i].substr(1_byte)); StringView switch_name = params[i].substr(1_byte);
if (it == m_desc.switches.end()) auto it = desc.switches.find(switch_name);
if (it == desc.switches.end())
{ {
if (ignore_unknown_switches) if (ignore_unknown_switches)
{ {
@ -53,13 +53,15 @@ ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& de
throw unknown_option(params[i]); throw unknown_option(params[i]);
} }
auto switch_index = it - m_desc.switches.begin(); auto switch_index = it - desc.switches.begin();
if (switch_seen[switch_index]) if (switch_seen[switch_index])
throw runtime_error{format("switch '-{}' specified more than once", it->key)}; throw runtime_error{format("switch '-{}' specified more than once", it->key)};
switch_seen[switch_index] = true; switch_seen[switch_index] = true;
if (it->value.takes_arg and ++i == params.size()) if (it->value.takes_arg and ++i == params.size())
throw missing_option_value(it->key); throw missing_option_value(it->key);
m_switches[switch_name.str()] = it->value.takes_arg ? params[i] : StringView{};
} }
else // positional else // positional
{ {
@ -75,18 +77,9 @@ ParametersParser::ParametersParser(ParameterList params, const ParameterDesc& de
Optional<StringView> ParametersParser::get_switch(StringView name) const Optional<StringView> ParametersParser::get_switch(StringView name) const
{ {
auto it = m_desc.switches.find(name); auto it = m_switches.find(name);
kak_assert(it != m_desc.switches.end()); return it == m_switches.end() ? Optional<StringView>{}
for (size_t i = 0; i < m_params.size(); ++i) : Optional<StringView>{it->value};
{
const auto& param = m_params[i];
if (param.substr(0_byte, 1_byte) == "-" and param.substr(1_byte) == name)
return it->value.takes_arg ? m_params[i+1] : StringView{};
if (param == "--")
break;
}
return {};
} }
} }

View File

@ -119,7 +119,7 @@ struct ParametersParser
ConstArrayView<String> positionals_from(size_t first) const ConstArrayView<String> positionals_from(size_t first) const
{ {
kak_assert(m_desc.flags & (ParameterDesc::Flags::SwitchesOnlyAtStart | ParameterDesc::Flags::SwitchesAsPositional)); // kak_assert(m_desc.flags & (ParameterDesc::Flags::SwitchesOnlyAtStart | ParameterDesc::Flags::SwitchesAsPositional));
return m_params.subrange(first < m_positional_indices.size() ? m_positional_indices[first] : -1); return m_params.subrange(first < m_positional_indices.size() ? m_positional_indices[first] : -1);
} }
@ -129,7 +129,7 @@ struct ParametersParser
private: private:
ParameterList m_params; ParameterList m_params;
Vector<size_t, MemoryDomain::Commands> m_positional_indices; Vector<size_t, MemoryDomain::Commands> m_positional_indices;
const ParameterDesc& m_desc; HashMap<String, StringView> m_switches;
}; };
} }