2016-09-17 00:40:29 +02:00
|
|
|
#pragma once
|
2016-08-28 15:10:43 +02:00
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include "ColorOption.h"
|
|
|
|
#include "ColorsOption.h"
|
|
|
|
#include "DoubleOption.h"
|
|
|
|
#include "ImageOption.h"
|
|
|
|
#include "IntOption.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "RegularExpressionOption.h"
|
|
|
|
#include "SwitchOption.h"
|
|
|
|
#include "ValidatorOption.h"
|
|
|
|
#include "BooleanOption.h"
|
|
|
|
|
|
|
|
namespace commandline
|
|
|
|
{
|
|
|
|
|
|
|
|
class Parser : public QObject
|
|
|
|
{
|
|
|
|
protected:
|
2016-09-17 00:40:29 +02:00
|
|
|
QHash<QString, Option *> _options;
|
|
|
|
QString _errorText;
|
|
|
|
/* No public inheritance because we need to modify a few methods */
|
|
|
|
QCommandLineParser _parser;
|
2016-08-28 15:10:43 +02:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
QStringList _getNames(const char shortOption, const QString& longOption);
|
|
|
|
QString _getDescription(const QString& description, const QString& default_=QString());
|
2016-08-28 15:10:43 +02:00
|
|
|
|
|
|
|
public:
|
2020-07-12 09:22:05 +02:00
|
|
|
~Parser() override;
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
bool parse(const QStringList &arguments);
|
|
|
|
void process(const QStringList &arguments);
|
|
|
|
void process(const QCoreApplication &app);
|
|
|
|
QString errorText() const;
|
2016-08-28 15:10:43 +02:00
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
template<class OptionT, class ... Args>
|
|
|
|
OptionT &add(
|
|
|
|
const char shortOption,
|
|
|
|
const QString longOption,
|
2016-08-28 15:10:43 +02:00
|
|
|
const QString description,
|
|
|
|
const QString default_,
|
|
|
|
Args ... args)
|
2016-09-17 00:40:29 +02:00
|
|
|
{
|
|
|
|
OptionT * option = new OptionT(
|
|
|
|
_getNames(shortOption, longOption),
|
|
|
|
_getDescription(description, default_),
|
|
|
|
longOption,
|
|
|
|
default_,
|
|
|
|
args...);
|
|
|
|
addOption(option);
|
|
|
|
return *option;
|
|
|
|
}
|
2016-08-28 15:10:43 +02:00
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
/* gcc does not support default arguments for variadic templates which
|
|
|
|
* makes this method necessary */
|
|
|
|
template<class OptionT>
|
|
|
|
OptionT &add(
|
|
|
|
const char shortOption,
|
2016-08-28 15:10:43 +02:00
|
|
|
const QString longOption,
|
|
|
|
const QString description,
|
|
|
|
const QString default_ = QString())
|
2016-09-17 00:40:29 +02:00
|
|
|
{
|
|
|
|
OptionT * option = new OptionT(
|
|
|
|
_getNames(shortOption, longOption),
|
|
|
|
_getDescription(description, default_),
|
|
|
|
longOption,
|
|
|
|
default_);
|
|
|
|
addOption(option);
|
|
|
|
return *option;
|
|
|
|
}
|
2016-08-28 15:10:43 +02:00
|
|
|
|
2020-07-22 18:15:39 +02:00
|
|
|
template<class OptionT>
|
|
|
|
OptionT &addHidden(
|
|
|
|
const char shortOption,
|
|
|
|
const QString longOption,
|
|
|
|
const QString description,
|
|
|
|
const QString default_ = QString())
|
|
|
|
{
|
|
|
|
OptionT * option = new OptionT(
|
|
|
|
_getNames(shortOption, longOption),
|
|
|
|
_getDescription(description, default_),
|
|
|
|
longOption,
|
|
|
|
default_);
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
|
|
|
|
option->setFlags(QCommandLineOption::HiddenFromHelp);
|
|
|
|
#else
|
|
|
|
option->setHidden(true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
addOption(option);
|
|
|
|
return *option;
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
Parser(const QString& description = QString())
|
2016-09-17 00:40:29 +02:00
|
|
|
{
|
2020-08-08 23:12:43 +02:00
|
|
|
if(description.size())
|
|
|
|
setApplicationDescription(description);
|
2020-11-14 17:58:56 +01:00
|
|
|
}
|
2020-08-08 23:12:43 +02:00
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QCommandLineOption addHelpOption()
|
|
|
|
{
|
|
|
|
return _parser.addHelpOption();
|
2020-11-14 17:58:56 +01:00
|
|
|
}
|
2020-08-08 23:12:43 +02:00
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
bool addOption(Option &option);
|
|
|
|
bool addOption(Option *option);
|
|
|
|
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax = QString())
|
|
|
|
{
|
|
|
|
_parser.addPositionalArgument(name, description, syntax);
|
2020-11-14 17:58:56 +01:00
|
|
|
}
|
2020-08-08 23:12:43 +02:00
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QCommandLineOption addVersionOption()
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
2016-09-17 00:40:29 +02:00
|
|
|
return _parser.addVersionOption();
|
2020-11-14 17:58:56 +01:00
|
|
|
}
|
2020-08-08 23:12:43 +02:00
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QString applicationDescription() const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.applicationDescription();
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
void clearPositionalArguments()
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
_parser.clearPositionalArguments();
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QString helpText() const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.helpText();
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
bool isSet(const QString &name) const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.isSet(name);
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
bool isSet(const Option &option) const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.isSet(option);
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
bool isSet(const Option *option) const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.isSet(*option);
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QStringList optionNames() const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.optionNames();
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QStringList positionalArguments() const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.positionalArguments();
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
void setApplicationDescription(const QString &description)
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
_parser.setApplicationDescription(description);
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
void setSingleDashWordOptionMode(QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode)
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
_parser.setSingleDashWordOptionMode(singleDashWordOptionMode);
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
[[ noreturn ]] void showHelp(int exitCode = 0)
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
_parser.showHelp(exitCode);
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QStringList unknownOptionNames() const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.unknownOptionNames();
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QString value(const QString &optionName) const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.value(optionName);
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QString value(const Option &option) const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.value(option);
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:40:29 +02:00
|
|
|
QStringList values(const QString &optionName) const
|
2020-08-08 23:12:43 +02:00
|
|
|
{
|
|
|
|
return _parser.values(optionName);
|
|
|
|
}
|
2016-08-28 15:10:43 +02:00
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QStringList values(const Option &option) const
|
|
|
|
{
|
|
|
|
return _parser.values(option);
|
|
|
|
}
|
2016-08-28 15:10:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|