add hidden options to our CLI parser to ignore Qt's parameters
Fixes #1511
This commit is contained in:
@@ -34,11 +34,11 @@ CommandLineParser::~CommandLineParser()
|
||||
}
|
||||
}
|
||||
|
||||
const CommandLineParser::Option *CommandLineParser::AddOption(const QStringList &strings, const QString &description, bool takes_arg, const QString &arg_placeholder)
|
||||
const CommandLineParser::Option *CommandLineParser::AddOption(const QStringList &strings, const QString &description, bool takes_arg, const QString &arg_placeholder, bool hidden)
|
||||
{
|
||||
Option* o = new Option();
|
||||
|
||||
options_.append({strings, description, o, takes_arg, arg_placeholder});
|
||||
options_.append({strings, description, o, takes_arg, arg_placeholder, hidden});
|
||||
|
||||
return o;
|
||||
}
|
||||
@@ -140,6 +140,10 @@ void CommandLineParser::PrintHelp(const char* filename)
|
||||
|
||||
printf("Usage: %s [options] %s\n\n", basename, positional_args.toUtf8().constData());
|
||||
foreach (const KnownOption& o, options_) {
|
||||
if (o.hidden) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QString all_args;
|
||||
|
||||
for (int i=0; i<o.args.size(); i++) {
|
||||
|
||||
@@ -26,6 +26,16 @@
|
||||
|
||||
#include "common/define.h"
|
||||
|
||||
/**
|
||||
* @brief Command-line argument parser
|
||||
*
|
||||
* You may be wondering why we don't use QCommandLineParser instead of a custom implmentation like
|
||||
* this. The reason why is because QCommandLineParser requires a QApplication object of some kind
|
||||
* to already have been created before it can parse anything, but we need to be able to control
|
||||
* whether a QApplication (GUI-mode) or a QCoreApplication (CLI-mode) is created which is set by
|
||||
* the user as a command line argument. Therefore we needed a custom implementation that could
|
||||
* parse arguments without the need for a Q(Core)Application to be present already.
|
||||
*/
|
||||
class CommandLineParser
|
||||
{
|
||||
public:
|
||||
@@ -77,7 +87,7 @@ public:
|
||||
|
||||
CommandLineParser() = default;
|
||||
|
||||
const Option* AddOption(const QStringList& strings, const QString& description, bool takes_arg = false, const QString& arg_placeholder = QString());
|
||||
const Option* AddOption(const QStringList& strings, const QString& description, bool takes_arg = false, const QString& arg_placeholder = QString(), bool hidden = false);
|
||||
|
||||
const PositionalArgument* AddPositionalArgument(const QString& name, const QString& description, bool required = false);
|
||||
|
||||
@@ -92,6 +102,7 @@ private:
|
||||
Option* option;
|
||||
bool takes_arg;
|
||||
QString arg_placeholder;
|
||||
bool hidden;
|
||||
};
|
||||
|
||||
struct KnownPositionalArgument {
|
||||
|
||||
+25
-6
@@ -76,32 +76,51 @@ int main(int argc, char *argv[])
|
||||
|
||||
CommandLineParser parser;
|
||||
|
||||
const CommandLineParser::Option* help_option =
|
||||
// Our options
|
||||
auto help_option =
|
||||
parser.AddOption({QStringLiteral("h"), QStringLiteral("-help")},
|
||||
QCoreApplication::translate("main", "Show this help text"));
|
||||
|
||||
const CommandLineParser::Option* version_option =
|
||||
auto version_option =
|
||||
parser.AddOption({QStringLiteral("v"), QStringLiteral("-version")},
|
||||
QCoreApplication::translate("main", "Show application version"));
|
||||
|
||||
const CommandLineParser::Option* fullscreen_option =
|
||||
auto fullscreen_option =
|
||||
parser.AddOption({QStringLiteral("f"), QStringLiteral("-fullscreen")},
|
||||
QCoreApplication::translate("main", "Start in full-screen mode"));
|
||||
|
||||
const CommandLineParser::Option* export_option =
|
||||
auto export_option =
|
||||
parser.AddOption({QStringLiteral("x"), QStringLiteral("-export")},
|
||||
QCoreApplication::translate("main", "Export only (No GUI)"));
|
||||
|
||||
const CommandLineParser::Option* ts_option =
|
||||
auto ts_option =
|
||||
parser.AddOption({QStringLiteral("-ts")},
|
||||
QCoreApplication::translate("main", "Override language with file"),
|
||||
true,
|
||||
QCoreApplication::translate("main", "qm-file"));
|
||||
|
||||
const CommandLineParser::PositionalArgument* project_argument =
|
||||
auto project_argument =
|
||||
parser.AddPositionalArgument(QStringLiteral("project"),
|
||||
QCoreApplication::translate("main", "Project to open on startup"));
|
||||
|
||||
// Qt options re-implemented (add to this as necessary)
|
||||
//
|
||||
// Because we don't use QCommandLineParser, we must filter out Qt's arguments ourselves. Here,
|
||||
// we create them so they're recognized, but never use and also hide them in the "help" text.
|
||||
parser.AddOption({QStringLiteral("platform")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("platformpluginpath")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("platformtheme")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("plugin")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("qmljsdebugger")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("qwindowgeometry")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("qwindowicon")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("qwindowtitle")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("reverse")}, QString(), false, QString(), true);
|
||||
parser.AddOption({QStringLiteral("session")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("style")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("stylesheet")}, QString(), true, QString(), true);
|
||||
parser.AddOption({QStringLiteral("widgetcount")}, QString(), false, QString(), true);
|
||||
|
||||
parser.Process(argc, argv);
|
||||
|
||||
if (help_option->IsSet()) {
|
||||
|
||||
Reference in New Issue
Block a user