From 1c450d43a2045ad7712ba876aae9a6072fa259fb Mon Sep 17 00:00:00 2001 From: Martchus Date: Tue, 26 Sep 2017 16:46:17 +0200 Subject: [PATCH] Add method to find specified operation --- application/argumentparser.cpp | 29 +++++++++++++++++++++++++++++ application/argumentparser.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/application/argumentparser.cpp b/application/argumentparser.cpp index 13cc325..f176c9f 100644 --- a/application/argumentparser.cpp +++ b/application/argumentparser.cpp @@ -527,6 +527,20 @@ Argument *Argument::wouldConflictWithArgument() const return nullptr; } +/*! + * \brief Returns the first operation argument specified by the user or nullptr if no operation has been specified. + * \remarks Only direct sub arguments of this argument are considered. + */ +Argument *Argument::specifiedOperation() const +{ + for (Argument *arg : m_subArgs) { + if (arg->denotesOperation() && arg->isPresent()) { + return arg; + } + } + return nullptr; +} + /*! * \brief Resets this argument and all sub arguments recursively. * \sa Argument::reset() @@ -743,6 +757,21 @@ void ArgumentParser::resetArgs() m_actualArgc = 0; } +/*! + * \brief Returns the first operation argument specified by the user or nullptr if no operation has been specified. + * \remarks Only main arguments are considered. See Argument::specifiedOperation() to check sub arguments of a specific + * argument. + */ +Argument *ArgumentParser::specifiedOperation() const +{ + for (Argument *arg : m_mainArgs) { + if (arg->denotesOperation() && arg->isPresent()) { + return arg; + } + } + return nullptr; +} + /*! * \brief Checks whether at least one uncombinable main argument is present. */ diff --git a/application/argumentparser.h b/application/argumentparser.h index 3f45636..39b262d 100644 --- a/application/argumentparser.h +++ b/application/argumentparser.h @@ -199,6 +199,7 @@ public: void setPreDefinedCompletionValues(const char *preDefinedCompletionValues); Argument *conflictsWithArgument() const; Argument *wouldConflictWithArgument() const; + Argument *specifiedOperation() const; void reset(); void resetRecursively(); @@ -250,6 +251,7 @@ public: void setUnknownArgumentBehavior(UnknownArgumentBehavior behavior); Argument *defaultArgument() const; void setDefaultArgument(Argument *argument); + Argument *specifiedOperation() const; void checkConstraints(); void invokeCallbacks(); bool isUncombinableMainArgPresent() const;