Browse Source

improved argument parser

experiment/meta_header
Martchus 7 years ago
parent
commit
aa3f3b5906
  1. 18
      application/argumentparser.cpp
  2. 29
      application/argumentparser.h

18
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

29
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.

Loading…
Cancel
Save