mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Qcommandlineparser (#199)
* Replaced getoptplusplus with QCommandLineParser. Fixes #39 * enabling C++11 if possible * enabling C++11 if possible * fixed gcc compilation issues * fixed linux builds and improved os x build * trying to fix dispmanx * trying to fix dispmanx * simplified travis build script * fixed argumentparser default values * rewrote validator system and made sure default arguments are processed correctly * rewrote validator system and made sure default arguments are processed correctly * fixed bool vs. regular options * oops... removing debug code * reverted screenshot api change
This commit is contained in:
committed by
redPanther
parent
c13f2e20ec
commit
61db9f43b8
90
libsrc/commandline/Parser.cpp
Normal file
90
libsrc/commandline/Parser.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include <QtDebug>
|
||||
#include <QtGui>
|
||||
#include "commandline/Parser.h"
|
||||
|
||||
using namespace commandline;
|
||||
|
||||
bool Parser::parse(const QStringList &arguments)
|
||||
{
|
||||
if (!_parser.parse(arguments)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Parser::process(const QStringList &arguments)
|
||||
{
|
||||
_parser.process(arguments);
|
||||
if (!parse(arguments)) {
|
||||
|
||||
fprintf(stdout, "%s", qPrintable(tr("Error: %1").arg(_errorText)));
|
||||
showHelp(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::process(const QCoreApplication &app)
|
||||
{
|
||||
Q_UNUSED(app);
|
||||
process(QCoreApplication::arguments());
|
||||
}
|
||||
|
||||
QString Parser::errorText() const
|
||||
{
|
||||
if (_errorText.size()) {
|
||||
return _errorText;
|
||||
}
|
||||
else {
|
||||
return _parser.errorText();
|
||||
}
|
||||
}
|
||||
|
||||
bool Parser::addOption(Option &option)
|
||||
{
|
||||
return addOption(&option);
|
||||
}
|
||||
|
||||
bool Parser::addOption(Option * const option)
|
||||
{
|
||||
_options[option->name()] = option;
|
||||
return _parser.addOption(*option);
|
||||
}
|
||||
QStringList Parser::_getNames(const char shortOption, const QString longOption)
|
||||
{
|
||||
QStringList names;
|
||||
if (shortOption != 0x0) {
|
||||
names << QString(shortOption);
|
||||
}
|
||||
if (longOption.size()) {
|
||||
names << longOption;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
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 */
|
||||
if (default_.size()) {
|
||||
if(!formattedDescription.contains("%1")){
|
||||
formattedDescription += " [default: %1]";
|
||||
}
|
||||
formattedDescription = formattedDescription.arg(default_);
|
||||
}
|
||||
return formattedDescription;
|
||||
}
|
||||
|
Reference in New Issue
Block a user