2016-08-28 15:10:43 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
#include <QtGui>
|
|
|
|
#include "commandline/Parser.h"
|
|
|
|
|
|
|
|
using namespace commandline;
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-02-11 22:52:47 +01:00
|
|
|
Q_FOREACH(Option * option, _options)
|
|
|
|
{
|
|
|
|
QString value = this->value(*option);
|
|
|
|
if (!option->validate(*this, value)) {
|
|
|
|
const QString error = option->getError();
|
|
|
|
if (error.size()) {
|
|
|
|
_errorText = tr("%1 is not a valid option for %2\n%3").arg(value, option->name(), error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_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))
|
|
|
|
{
|
2016-08-28 15:10:43 +02:00
|
|
|
fprintf(stdout, "%s", 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
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::process(const QCoreApplication &app)
|
|
|
|
{
|
2017-02-11 22:52:47 +01:00
|
|
|
Q_UNUSED(app);
|
|
|
|
process(QCoreApplication::arguments());
|
2016-08-28 15:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Parser::errorText() const
|
|
|
|
{
|
2017-02-11 22:52:47 +01:00
|
|
|
return (_errorText.size()) ? _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
|
|
|
|
2016-08-28 15:10:43 +02:00
|
|
|
QStringList Parser::_getNames(const char shortOption, const QString longOption)
|
|
|
|
{
|
|
|
|
QStringList names;
|
2017-02-11 22:52:47 +01:00
|
|
|
if (shortOption != 0x0)
|
|
|
|
{
|
2016-08-28 15:10:43 +02:00
|
|
|
names << QString(shortOption);
|
|
|
|
}
|
2017-02-11 22:52:47 +01:00
|
|
|
if (longOption.size())
|
|
|
|
{
|
2016-08-28 15:10:43 +02:00
|
|
|
names << longOption;
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2017-02-11 22:52:47 +01:00
|
|
|
|
2016-08-28 15:10:43 +02:00
|
|
|
QString Parser::_getDescription(const QString description, const QString default_)
|
|
|
|
{
|
|
|
|
/* Add the translations if available */
|
|
|
|
QString formattedDescription(tr(qPrintable(description)));
|
|
|
|
|
|
|
|
/* Fill in the default if needed */
|
2017-02-11 22:52:47 +01:00
|
|
|
if (default_.size())
|
|
|
|
{
|
|
|
|
if(!formattedDescription.contains("%1"))
|
|
|
|
{
|
2016-08-28 15:10:43 +02:00
|
|
|
formattedDescription += " [default: %1]";
|
|
|
|
}
|
|
|
|
formattedDescription = formattedDescription.arg(default_);
|
|
|
|
}
|
|
|
|
return formattedDescription;
|
|
|
|
}
|