From f92b75a46324ec7f5821382465e20fc6d408459f Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 6 Sep 2021 14:54:23 +0200 Subject: [PATCH] Util+Test: Add setting to stop parsing on first non-option --- src/util/argparser.cpp | 10 +++++-- src/util/argparser.h | 13 ++++----- test/testutilargparser.cpp | 59 +++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/util/argparser.cpp b/src/util/argparser.cpp index ee935bf..77367dc 100644 --- a/src/util/argparser.cpp +++ b/src/util/argparser.cpp @@ -24,7 +24,7 @@ void ArgParser::printOptionError(char name, Error error) void ArgParser::printOptionError(const char* name, Error error, bool longName) { - if (!m_errorMessages) { + if (!m_errorReporting) { return; } @@ -206,14 +206,15 @@ bool ArgParser::parse(int argc, const char* argv[]) { bool result = true; + // Skip program name argument + m_optionIndex = 1; + // By default parse all '-' prefixed parameters as options m_nonOptionMode = false; // Get program name m_name = argv[0] + std::string_view(argv[0]).find_last_of('/') + 1; - printf("name: %s\n", m_name); - std::string_view argument; std::string_view next; for (; m_optionIndex < argc; ++m_optionIndex) { @@ -250,6 +251,9 @@ bool ArgParser::parse(int argc, const char* argv[]) } // Argument else { + if (m_stopParsingOnFirstNonOption) { + m_nonOptionMode = true; + } printf("-> argu: '%s'", argument.data()); } } diff --git a/src/util/argparser.h b/src/util/argparser.h index 3ffd655..080309c 100644 --- a/src/util/argparser.h +++ b/src/util/argparser.h @@ -54,10 +54,9 @@ public: void addOption(double& value, char shortName, const char* longName, const char* usageString, const char* manString, const char* argumentName = "", Required requiresArgument = Required::No); void addOption(std::vector& value, char shortName, const char* longName, const char* usageString, const char* manString, const char* argumentName = "", Required requiresArgument = Required::No); - void setOptionIndex(int index) { m_optionIndex = index; } + void setErrorReporting(bool state) { m_errorReporting = state; } void setExitOnFirstError(bool state) { m_exitOnFirstError = state; } - void setErrorMessages(bool state) { m_errorMessages = state; } - void setNonOptionAfterFirst(bool state) { m_nonOptionAfterFirst = state; } + void setStopParsingOnFirstNonOption(bool state) { m_stopParsingOnFirstNonOption = state; } private: void printOptionError(char name, Error error); @@ -65,12 +64,12 @@ private: bool parseShortOption(std::string_view option, std::string_view next); bool parseLongOption(std::string_view option, std::string_view next); + bool m_errorReporting { true }; + bool m_exitOnFirstError { true }; + bool m_stopParsingOnFirstNonOption { false }; + int m_optionIndex { 1 }; bool m_nonOptionMode { false }; - bool m_exitOnFirstError { true }; - bool m_errorMessages { true }; - // TODO: Implement this, maybe combine with error messages flag, enum class? or bitfield - bool m_nonOptionAfterFirst { false }; const char* m_name; std::vector