From b6bfcbdc335cccc2d96c1be28272628bd19db307 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 9 Sep 2021 15:14:17 +0200 Subject: [PATCH] Util: Support for optional arguments in ArgParser --- src/util/argparser.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/util/argparser.cpp b/src/util/argparser.cpp index b90bb6a..c8fec8e 100644 --- a/src/util/argparser.cpp +++ b/src/util/argparser.cpp @@ -191,20 +191,29 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next) bool ArgParser::parseArgument(std::string_view argument) { - // No handler for argument - if (m_argumentIndex >= m_arguments.size()) { - return false; - } + bool result = true; - Argument& currentArgument = m_arguments.at(m_argumentIndex); - bool result = currentArgument.acceptValue(argument.data()); + for (;;) { + // Run out of argument handlers + if (m_argumentIndex >= m_arguments.size()) { + return false; + } - if (result) { - currentArgument.addedValues++; - } + Argument& currentArgument = m_arguments.at(m_argumentIndex); + result = currentArgument.acceptValue(argument.data()); + + if (result) { + currentArgument.addedValues++; + if (currentArgument.addedValues >= currentArgument.maxValues) { + m_argumentIndex++; + } + } + else if (currentArgument.minValues == 0) { + m_argumentIndex++; + continue; + } - if (currentArgument.addedValues >= currentArgument.maxValues) { - m_argumentIndex++; + break; } return result;