From b282c7d8153e23e4f31ca262854332f3500559bb Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 26 Jan 2020 13:32:10 -0800 Subject: [PATCH] Fix parsing CLI flags that accept values when passed before 'action' argument --- app/cli/commandlineparser.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/app/cli/commandlineparser.cpp b/app/cli/commandlineparser.cpp index 6faf15ea..2047323b 100644 --- a/app/cli/commandlineparser.cpp +++ b/app/cli/commandlineparser.cpp @@ -172,20 +172,31 @@ GlobalCommandLineParser::ParseResult GlobalCommandLineParser::parse(const QStrin parser.addPositionalArgument("action", "Action to execute", ""); parser.parse(args); auto posArgs = parser.positionalArguments(); - QString action = posArgs.isEmpty() ? QString() : posArgs.first().toLower(); - if (action == "") { + if (posArgs.isEmpty()) { // This method will not return and terminates the process if --version // or --help is specified parser.handleHelpAndVersionOptions(); parser.handleUnknownOptions(); return NormalStartRequested; - } else if (action == "quit") { - return QuitRequested; - } else if (action == "stream") { - return StreamRequested; - } else { - parser.showError(QString("Invalid action: %1").arg(action)); + } + else { + // If users supply arguments that accept values prior to the "quit" + // or "stream" positional arguments, we will not be able to correctly + // parse the value out of the input because this QCommandLineParser + // doesn't know about all of the options that "quit" and "stream" + // commands can accept. To work around this issue, we just look + // for "quit" or "stream" positional arguments anywhere. + for (int i = 0; i < posArgs.size(); i++) { + QString action = posArgs.at(i).toLower(); + if (action == "quit") { + return QuitRequested; + } else if (action == "stream") { + return StreamRequested; + } + } + + parser.showError(QString("Invalid action")); } }