Detect when switches are specified more than once

Fixes #1219
This commit is contained in:
Maxime Coste 2017-02-15 18:55:11 +00:00
parent 9e0f085b86
commit 0a1cb4b9b1

View File

@ -19,6 +19,7 @@ ParametersParser::ParametersParser(ParameterList params,
m_desc(desc) m_desc(desc)
{ {
bool only_pos = desc.flags & ParameterDesc::Flags::SwitchesAsPositional; bool only_pos = desc.flags & ParameterDesc::Flags::SwitchesAsPositional;
Vector<bool> switch_seen(desc.switches.size(), false);
for (size_t i = 0; i < params.size(); ++i) for (size_t i = 0; i < params.size(); ++i)
{ {
if (not only_pos and params[i] == "--") if (not only_pos and params[i] == "--")
@ -29,6 +30,11 @@ ParametersParser::ParametersParser(ParameterList params,
if (it == m_desc.switches.end()) if (it == m_desc.switches.end())
throw unknown_option(params[i]); throw unknown_option(params[i]);
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;
if (it->value.takes_arg) if (it->value.takes_arg)
{ {
++i; ++i;