2016-08-28 15:10:43 +02:00
|
|
|
#include "commandline/Parser.h"
|
|
|
|
|
|
|
|
using namespace commandline;
|
|
|
|
|
2020-07-12 09:22:05 +02:00
|
|
|
Parser::~Parser()
|
|
|
|
{
|
|
|
|
qDeleteAll(_options);
|
|
|
|
}
|
|
|
|
|
2016-08-28 15:10:43 +02:00
|
|
|
bool Parser::parse(const QStringList &arguments)
|
|
|
|
{
|
2017-02-11 22:52:47 +01:00
|
|
|
if (!_parser.parse(arguments))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-28 15:10:43 +02:00
|
|
|
|
2020-07-12 09:19:59 +02:00
|
|
|
for(Option * option : _options)
|
2017-02-11 22:52:47 +01:00
|
|
|
{
|
|
|
|
QString value = this->value(*option);
|
|
|
|
if (!option->validate(*this, value)) {
|
|
|
|
const QString error = option->getError();
|
2020-11-14 17:58:56 +01:00
|
|
|
if (!error.isEmpty()) {
|
2021-11-29 18:51:03 +01:00
|
|
|
_errorText = tr("\"%1\" is not a valid option for %2, %3").arg(value, option->name(), error);
|
2017-02-11 22:52:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-29 18:51:03 +01:00
|
|
|
_errorText = tr("\"%1\" is not a valid option for %2").arg(value, option->name());
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
2017-02-11 22:52:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::process(const QStringList &arguments)
|
|
|
|
{
|
2017-02-11 22:52:47 +01:00
|
|
|
_parser.process(arguments);
|
|
|
|
if (!parse(arguments))
|
|
|
|
{
|
2021-11-29 18:51:03 +01:00
|
|
|
fprintf(stdout, "%s\n\n", qPrintable(tr("Error: %1").arg(_errorText)));
|
2017-02-11 22:52:47 +01:00
|
|
|
showHelp(EXIT_FAILURE);
|
|
|
|
}
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
void Parser::process(const QCoreApplication& /*app*/)
|
2016-08-28 15:10:43 +02:00
|
|
|
{
|
2017-02-11 22:52:47 +01:00
|
|
|
process(QCoreApplication::arguments());
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Parser::errorText() const
|
|
|
|
{
|
2020-11-14 17:58:56 +01:00
|
|
|
return (!_errorText.isEmpty()) ? _errorText : _parser.errorText();
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Parser::addOption(Option &option)
|
|
|
|
{
|
2017-02-11 22:52:47 +01:00
|
|
|
return addOption(&option);
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Parser::addOption(Option * const option)
|
|
|
|
{
|
2017-02-11 22:52:47 +01:00
|
|
|
_options[option->name()] = option;
|
|
|
|
return _parser.addOption(*option);
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
2017-02-11 22:52:47 +01:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
QStringList Parser::_getNames(const char shortOption, const QString& longOption)
|
2016-08-28 15:10:43 +02:00
|
|
|
{
|
|
|
|
QStringList names;
|
2017-02-11 22:52:47 +01:00
|
|
|
if (shortOption != 0x0)
|
|
|
|
{
|
2016-08-28 15:10:43 +02:00
|
|
|
names << QString(shortOption);
|
|
|
|
}
|
2020-11-14 17:58:56 +01:00
|
|
|
if (longOption.size() != 0)
|
2017-02-11 22:52:47 +01:00
|
|
|
{
|
2016-08-28 15:10:43 +02:00
|
|
|
names << longOption;
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2017-02-11 22:52:47 +01:00
|
|
|
|
2020-11-14 17:58:56 +01:00
|
|
|
QString Parser::_getDescription(const QString& description, const QString& default_)
|
2016-08-28 15:10:43 +02:00
|
|
|
{
|
|
|
|
/* Add the translations if available */
|
|
|
|
QString formattedDescription(tr(qPrintable(description)));
|
|
|
|
|
|
|
|
/* Fill in the default if needed */
|
2020-11-14 17:58:56 +01:00
|
|
|
if (!default_.isEmpty())
|
2017-02-11 22:52:47 +01:00
|
|
|
{
|
|
|
|
if(!formattedDescription.contains("%1"))
|
|
|
|
{
|
2016-08-28 15:10:43 +02:00
|
|
|
formattedDescription += " [default: %1]";
|
|
|
|
}
|
|
|
|
formattedDescription = formattedDescription.arg(default_);
|
|
|
|
}
|
|
|
|
return formattedDescription;
|
|
|
|
}
|