From aa3f3b5906219a3b8c226218bb224a967d345b33 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 8 May 2015 23:20:47 +0200 Subject: [PATCH] improved argument parser --- application/argumentparser.cpp | 18 +++++++++++++++++- application/argumentparser.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/application/argumentparser.cpp b/application/argumentparser.cpp index 3eebe77..5201663 100644 --- a/application/argumentparser.cpp +++ b/application/argumentparser.cpp @@ -1,5 +1,6 @@ #include "argumentparser.h" #include "failure.h" + #include "../conversion/stringconversion.h" #include "../misc/random.h" @@ -38,6 +39,7 @@ Argument::Argument(const std::string &name, const std::string abbreviation, cons m_required(false), m_combinable(false), m_implicit(false), + m_denotesOperation(false), m_requiredValueCount(0), m_default(false), m_present(false), @@ -451,7 +453,21 @@ void ArgumentParser::parseArgs(int argc, char *argv[]) } else { readValue: if(!currentArg) { - // we have not parsed an argument before -> check if there's an implicit argument definition + // we have not parsed an argument before + // -> check if an argument which denotes the operation is specified + if(i == argv + 1) { + for(Argument *arg : m_mainArgs) { + if(!arg->isPresent() && arg->denotesOperation() + && (arg->name() == givenArg || arg->abbreviation() == givenArg)) { + currentArg = arg; + break; + } + } + if(currentArg) { + continue; + } + } + // -> check if there's an implicit argument definition for(Argument *arg : m_mainArgs) { if(!arg->isPresent() && arg->isImplicit()) { // set present flag of argument diff --git a/application/argumentparser.h b/application/argumentparser.h index 19d0217..69700c6 100644 --- a/application/argumentparser.h +++ b/application/argumentparser.h @@ -63,6 +63,8 @@ public: void setCombinable(bool value); bool isImplicit() const; void setImplicit(bool value); + bool denotesOperation() const; + void setDenotesOperation(bool denotesOperation); void setCallback(CallbackFunction callback); void printInfo(std::ostream &os, unsigned char indentionLevel = 0) const; const ArgumentVector &secondaryArguments() const; @@ -81,6 +83,7 @@ private: bool m_required; bool m_combinable; bool m_implicit; + bool m_denotesOperation; int m_requiredValueCount; StringList m_valueNames; bool m_default; @@ -415,6 +418,8 @@ inline void Argument::setCombinable(bool value) /*! * \brief Returns an indication whether the argument can be specified implicitely. * + * An implicit main argument is assumed to be present even if only its value is present. + * * \sa setImplicit() */ inline bool Argument::isImplicit() const @@ -432,6 +437,30 @@ inline void Argument::setImplicit(bool value) m_implicit = value; } +/*! + * \brief Returns whether the argument denotes the operation. + * + * An argument which denotes the operation might be specified + * withouth "--" or "-" prefix as first main argument. + * + * The default value is false. + * + * \sa setDenotesOperation() + */ +inline bool Argument::denotesOperation() const +{ + return m_denotesOperation; +} + +/*! + * \brief Sets whether the argument denotes the operation. + * \sa denotesOperation() + */ +inline void Argument::setDenotesOperation(bool denotesOperation) +{ + m_denotesOperation = denotesOperation; +} + /*! * \brief Sets a \a callback function which will be called by the parser if * the argument could be found and no parsing errors occured.