Qt: More flexible argv parsing

This commit is contained in:
Vicki Pfau 2022-04-21 21:31:13 -07:00
parent ce7e53d53d
commit b10a5b7f02
4 changed files with 38 additions and 20 deletions

View File

@ -13,6 +13,11 @@
#include <mgba/feature/commandline.h>
static const mOption s_frontendOptions[] = {
{ "ecard", true, '\0' },
{ 0 }
};
using namespace QGBA;
ConfigOption::ConfigOption(const QString& name, QObject* parent)
@ -129,24 +134,27 @@ ConfigController::ConfigController(QObject* parent)
mSubParserGraphicsInit(&m_subparsers[0], &m_graphicsOpts);
m_subparsers[1].usage = "Frontend options:\n"
" --ecard FILENAME Scan an e-Reader card in the first loaded game\n"
" Can be paassed multiple times for multiple cards";
" --ecard FILE Scan an e-Reader card in the first loaded game\n"
" Can be paassed multiple times for multiple cards\n"
m_subparsers[1].parse = nullptr;
m_subparsers[1].parseLong = [](struct mSubParser* parser, const char* option, const char* arg) {
if (option == QLatin1String("ecard")) {
QStringList* eCards = static_cast<QStringList*>(parser->opts);
eCards->append(QString::fromUtf8(arg));
ConfigController* self = static_cast<ConfigController*>(parser->opts);
QString optionName(QString::fromUtf8(option));
if (optionName == QLatin1String("ecard")) {
QStringList ecards;
if (self->m_argvOptions.contains(optionName)) {
ecards = self->m_argvOptions[optionName].toStringList();
}
ecards.append(QString::fromUtf8(arg));
self->m_argvOptions[optionName] = ecards;
return true;
}
return false;
};
m_subparsers[1].apply = nullptr;
m_subparsers[1].extraOptions = nullptr;
m_subparsers[1].longOptions = (const mOption[]) {
{ "ecard", true, '\0' },
{ 0 }
};
m_subparsers[1].opts = &m_eCards;
m_subparsers[1].longOptions = s_frontendOptions;
m_subparsers[1].opts = this;
}
ConfigController::~ConfigController() {
@ -223,6 +231,14 @@ QVariant ConfigController::getQtOption(const QString& key, const QString& group)
return value;
}
QVariant ConfigController::getArgvOption(const QString& key) const {
return m_argvOptions.value(key);
}
QVariant ConfigController::takeArgvOption(const QString& key) {
return m_argvOptions.take(key);
}
void ConfigController::saveOverride(const Override& override) {
override.save(overrides());
write();
@ -334,10 +350,6 @@ void ConfigController::usage(const char* arg0) const {
::usage(arg0, nullptr, nullptr, m_subparsers.data(), m_subparsers.size());
}
QStringList ConfigController::takeECardList() {
return QStringList(std::move(m_eCards));
}
bool ConfigController::isPortable() {
return mCoreConfigIsPortable();
}

View File

@ -81,6 +81,9 @@ public:
QVariant getQtOption(const QString& key, const QString& group = QString()) const;
QVariant getArgvOption(const QString& key) const;
QVariant takeArgvOption(const QString& key);
QList<QString> getMRU() const;
void setMRU(const QList<QString>& mru);
@ -96,8 +99,6 @@ public:
const mGraphicsOpts* graphicsOpts() const { return &m_graphicsOpts; }
void usage(const char* arg0) const;
QStringList takeECardList();
static const QString& configDir();
static bool isPortable();
@ -113,6 +114,8 @@ public slots:
void write();
private:
void addArgvOption(const QString& key, const QVariant& value);
Configuration* defaults() { return &m_config.defaultsTable; }
mCoreConfig m_config;
@ -121,8 +124,8 @@ private:
mGraphicsOpts m_graphicsOpts{};
std::array<mSubParser, 2> m_subparsers;
bool m_parsed = false;
QStringList m_eCards;
QHash<QString, QVariant> m_argvOptions;
QHash<QString, ConfigOption*> m_optionSet;
std::unique_ptr<QSettings> m_settings;
static QString s_configDir;

View File

@ -912,7 +912,7 @@ void CoreController::scanCard(const QString& path) {
break;
}
QString filepath(QString::fromUtf8(line));
if (filepath[0] == QChar('#')) {
if (filepath.isEmpty() || filepath[0] == QChar('#')) {
continue;
}
if (QFileInfo(filepath).isRelative()) {

View File

@ -931,7 +931,10 @@ void Window::gameStarted() {
#ifdef M_CORE_GBA
if (m_controller->platform() == mPLATFORM_GBA) {
m_controller->scanCards(m_config->takeECardList());
QVariant eCardList = m_config->takeArgvOption(QString("ecard"));
if (eCardList.canConvert(QMetaType::QStringList)) {
m_controller->scanCards(eCardList.toStringList());
}
}
#endif