improved argument parser
This commit is contained in:
parent
f4c2568765
commit
aa3f3b5906
|
@ -1,5 +1,6 @@
|
||||||
#include "argumentparser.h"
|
#include "argumentparser.h"
|
||||||
#include "failure.h"
|
#include "failure.h"
|
||||||
|
|
||||||
#include "../conversion/stringconversion.h"
|
#include "../conversion/stringconversion.h"
|
||||||
#include "../misc/random.h"
|
#include "../misc/random.h"
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ Argument::Argument(const std::string &name, const std::string abbreviation, cons
|
||||||
m_required(false),
|
m_required(false),
|
||||||
m_combinable(false),
|
m_combinable(false),
|
||||||
m_implicit(false),
|
m_implicit(false),
|
||||||
|
m_denotesOperation(false),
|
||||||
m_requiredValueCount(0),
|
m_requiredValueCount(0),
|
||||||
m_default(false),
|
m_default(false),
|
||||||
m_present(false),
|
m_present(false),
|
||||||
|
@ -451,7 +453,21 @@ void ArgumentParser::parseArgs(int argc, char *argv[])
|
||||||
} else {
|
} else {
|
||||||
readValue:
|
readValue:
|
||||||
if(!currentArg) {
|
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) {
|
for(Argument *arg : m_mainArgs) {
|
||||||
if(!arg->isPresent() && arg->isImplicit()) {
|
if(!arg->isPresent() && arg->isImplicit()) {
|
||||||
// set present flag of argument
|
// set present flag of argument
|
||||||
|
|
|
@ -63,6 +63,8 @@ public:
|
||||||
void setCombinable(bool value);
|
void setCombinable(bool value);
|
||||||
bool isImplicit() const;
|
bool isImplicit() const;
|
||||||
void setImplicit(bool value);
|
void setImplicit(bool value);
|
||||||
|
bool denotesOperation() const;
|
||||||
|
void setDenotesOperation(bool denotesOperation);
|
||||||
void setCallback(CallbackFunction callback);
|
void setCallback(CallbackFunction callback);
|
||||||
void printInfo(std::ostream &os, unsigned char indentionLevel = 0) const;
|
void printInfo(std::ostream &os, unsigned char indentionLevel = 0) const;
|
||||||
const ArgumentVector &secondaryArguments() const;
|
const ArgumentVector &secondaryArguments() const;
|
||||||
|
@ -81,6 +83,7 @@ private:
|
||||||
bool m_required;
|
bool m_required;
|
||||||
bool m_combinable;
|
bool m_combinable;
|
||||||
bool m_implicit;
|
bool m_implicit;
|
||||||
|
bool m_denotesOperation;
|
||||||
int m_requiredValueCount;
|
int m_requiredValueCount;
|
||||||
StringList m_valueNames;
|
StringList m_valueNames;
|
||||||
bool m_default;
|
bool m_default;
|
||||||
|
@ -415,6 +418,8 @@ inline void Argument::setCombinable(bool value)
|
||||||
/*!
|
/*!
|
||||||
* \brief Returns an indication whether the argument can be specified implicitely.
|
* \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()
|
* \sa setImplicit()
|
||||||
*/
|
*/
|
||||||
inline bool Argument::isImplicit() const
|
inline bool Argument::isImplicit() const
|
||||||
|
@ -432,6 +437,30 @@ inline void Argument::setImplicit(bool value)
|
||||||
m_implicit = 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
|
* \brief Sets a \a callback function which will be called by the parser if
|
||||||
* the argument could be found and no parsing errors occured.
|
* the argument could be found and no parsing errors occured.
|
||||||
|
|
Loading…
Reference in New Issue