From 0a1cb4b9b19933f099fa89194d528911c844e2bd Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 15 Feb 2017 18:55:11 +0000 Subject: [PATCH] Detect when switches are specified more than once Fixes #1219 --- src/parameters_parser.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/parameters_parser.cc b/src/parameters_parser.cc index 68a497c9..30154483 100644 --- a/src/parameters_parser.cc +++ b/src/parameters_parser.cc @@ -19,6 +19,7 @@ ParametersParser::ParametersParser(ParameterList params, m_desc(desc) { bool only_pos = desc.flags & ParameterDesc::Flags::SwitchesAsPositional; + Vector switch_seen(desc.switches.size(), false); for (size_t i = 0; i < params.size(); ++i) { if (not only_pos and params[i] == "--") @@ -29,6 +30,11 @@ ParametersParser::ParametersParser(ParameterList params, if (it == m_desc.switches.end()) 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) { ++i;