Workspace 6.21.5
|
Abstract base class for dealing with command line arguments. More...
#include <Workspace/Application/argumentsparser.h>
Public Member Functions | |
ArgumentsParser (ArgumentsParser &&args) | |
ArgumentsParser (const ArgumentsParser &args) | |
ArgumentsParser (const QStringList &expectedArguments=QStringList(), const QString &usageHeader="") | |
virtual | ~ArgumentsParser () |
QString | argument (int index) const |
bool | flagExists (const QString &name) const |
int | flagOccurrence (const QString &name) const |
QString | flagParameter (const QString &name, int paramIndex, int occurrence=0) const |
QStringList | flagParameters (const QString &name, int occurrence=0) const |
const QStringList & | getArguments () const |
int | numArguments () const |
ArgumentsParser & | operator= (ArgumentsParser args) |
bool | parse (const QStringList &arguments, int &exitCode) |
virtual QString | usage () const |
Static Public Member Functions | |
static bool | parseBoolean (const QString &s) |
static bool | validBoolean (const QString &s) |
Protected Member Functions | |
void | addSupportedFlag (const QString &name, const QString &description, int numParameters=0, const QStringList ¶mNames=QStringList(), bool developOnly=false) |
void | logError (const QString &msg) |
void | purgeArgument (int index) |
void | purgeFlag (const QString &name, int occurrence=0) |
virtual bool | validate (int &exitCode) |
Derived classes should use addSupportedFlag() during construction to specify the flags and flag parameter counts they expect.
When a user calls parse() for a given set of arguments any argument prefixed with "--" is checked against the supported flags and the appropriate number of parameters are stored. The derived class is expected to expose these flags through it's own new methods that can call the protected flagExists() and flagParameters() methods on the base class. If while reading in the expected number of parameters for a supported flag we encounter a new flag ('–' prefix) we stop reading parameters even if more are expected and interpret this value as a new flag.
Derived classes must also implement validate() which will be called by parse() after it has parsed all the arguments - allowing the derived class to confirm that the parsed arguments are valid. During validate() derived classes can call logError() and return false if problems are detected.
If any arguments are encountered during parse() which have not been specified by addSupportedFlag(), they are made available directly to the user via numArguments() and argument(index).
See WorkspaceArgumentParser for an example derived class.
ArgumentsParser | ( | const QStringList & | expectedArguments = QStringList() , |
const QString & | usageHeader = "" |
||
) |
expectedArguments | A list of names for the expected arguments of this application. |
usageHeader | A header for the usage message. If this is an empty string, the constructor will attempt to create a default header based on the application name and version as obtained from QCoreApplication. |
The constructor always adds the help
flag. If the application version has also been set by a call to QCoreApplication::setApplicationVersion(), then a version
flag will also be automatically added by this constructor. Note that the version must be set before the arguments parser object is created or else the version
flag will not be added automatically.
ArgumentsParser | ( | const ArgumentsParser & | args | ) |
ArgumentsParser | ( | ArgumentsParser && | args | ) |
|
virtualdefault |
|
protected |
Derived classes should call this function during construction to specify all the flags they support along with the number of parameters expected for each of then.
QString argument | ( | int | index | ) | const |
index | Specifies which non-flag argument to retrieve. Arguments are indexed in the order they were specified on the command line, with the first non-flag argument having an index of zero. |
bool flagExists | ( | const QString & | name | ) | const |
int flagOccurrence | ( | const QString & | name | ) | const |
QString flagParameter | ( | const QString & | name, |
int | paramIndex, | ||
int | occurrence = 0 |
||
) | const |
QStringList flagParameters | ( | const QString & | name, |
int | occurrence = 0 |
||
) | const |
const QStringList & getArguments | ( | ) | const |
|
protected |
This is a convenience only. It simply prepends msg with an appropriate prefix denoting the text as an error (eg "ERROR: " for English languages) and also appends a new line character. The whole lot is then passed to LogManager::logText() to send it to the main log, which is typically just stdout.
int numArguments | ( | ) | const |
ArgumentsParser & operator= | ( | ArgumentsParser | args | ) |
bool parse | ( | const QStringList & | arguments, |
int & | exitCode | ||
) |
arguments | The command line arguments supplied to the application. This would normally be the same as those passed to the QCoreApplication constructor or simply be the result of calling QCoreApplication::arguments(). |
exitCode | Subclasses can set this to a non-zero value in their validate() function to indicate some specific validation error. When parse() returns, if exitCode is non-zero, its value should be used as the exit code of the application. |
This function parses the command line in arguments and populates the internal data structures for the supported flags, etc. It must be called before any of the query methods. Unless one of the standard options –version
or –help
is present, parse() will call validate() as the last thing it does. If one of the standard options is present, then exitCode() will have the value of zero upon return and the function will return false to indicate that the application should terminate.
–version
or –help
is present. In order to distinguish between an error and one of these such arguments, the exitCode can be inspected. If exitCode is zero, the caller can assume there was no error in the arguments. Subclasses should ensure they obey this rule.
|
static |
|
protected |
Derived classed can call this function to remove the given non-flag argument.
|
protected |
Derived class can call this function to remove the given flags.
|
virtual |
|
protectedvirtual |
Reimplemented in WorkflowAnalyseBatchArgumentsParser, OOPScheduleeArgumentsParser, and WorkspaceArgumentsParser.
|
static |