fix for < LocutusOfBorg1> https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760776
authorsheepluva
Mon, 08 Sep 2014 18:12:13 +0200
changeset 10405 469ce70ba163
parent 10403 bc9433b33947
child 10407 012bb4b63117
fix for < LocutusOfBorg1> https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760776
QTfrontend/main.cpp
--- a/QTfrontend/main.cpp	Sun Sep 07 15:11:08 2014 -0400
+++ b/QTfrontend/main.cpp	Mon Sep 08 18:12:13 2014 +0200
@@ -123,6 +123,24 @@
     }
 }
 
+QString getUsage()
+{
+    return QString(
+"%1: hedgewars [%2...]\n"
+"\n"
+"%3:\n"
+"  --help              %4\n"
+"  --config-dir=PATH   %5\n"
+"  --data-dir=PATH     %6\n"
+"\n"
+).arg(HWApplication::tr("Usage", "command-line"))
+.arg(HWApplication::tr("OPTION", "command-line"))
+.arg(HWApplication::tr("Options", "command-line"))
+.arg(HWApplication::tr("Display this help", "command-line"))
+.arg(HWApplication::tr("Custom path for configuration data and user data", "command-line"))
+.arg(HWApplication::tr("Custom path to the game data folder", "command-line"));
+}
+
 int main(int argc, char *argv[])
 {
     // Since we're calling this first, closeResources() will be the last thing called after main() returns.
@@ -133,8 +151,87 @@
 #endif
 
     HWApplication app(argc, argv);
+    app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
 
+    // file engine and splash. to be initialized later
+    engine = NULL;
     QLabel *splash = NULL;
+
+    // parse arguments
+
+    QStringList arguments = app.arguments();
+    QMap<QString, QString> parsedArgs;
+    {
+        QList<QString>::iterator i = arguments.begin();
+        while(i != arguments.end())
+        {
+            QString arg = *i;
+
+
+            QRegExp opt("--(\\S+)=(.+)");
+            if(opt.exactMatch(arg))
+            {
+                parsedArgs[opt.cap(1)] = opt.cap(2);
+                i = arguments.erase(i);
+            }
+            else
+            {
+                if(arg.startsWith("--")) {
+                    if(arg == "--help")
+                    {
+                        printf("%s", getUsage().toUtf8().constData());
+                        return 0;
+                    }
+                    // argument is something wrong
+                    fprintf(stderr, "%s\n\n%s",
+                        HWApplication::tr("Malformed option argument: %1", "command-line").arg(arg).toUtf8().constData(),
+                        getUsage().toUtf8().constData());
+                    return 1;
+                }
+
+                // if not starting with --, then always skip
+                // (because we can't determine if executable path/call or not - on windows)
+                ++i;
+            }
+        }
+    }
+
+    if(parsedArgs.contains("data-dir"))
+    {
+        QFileInfo f(parsedArgs["data-dir"]);
+        parsedArgs.remove("data-dir");
+        if(!f.exists())
+        {
+            qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath();
+        }
+        *cDataDir = f.absoluteFilePath();
+        custom_data = true;
+    }
+
+    if(parsedArgs.contains("config-dir"))
+    {
+        QFileInfo f(parsedArgs["config-dir"]);
+        parsedArgs.remove("config-dir");
+        cfgdir->setPath(f.absoluteFilePath());
+        custom_config = true;
+    }
+    else
+    {
+        cfgdir->setPath(QDir::homePath());
+        custom_config = false;
+    }
+
+    if (!parsedArgs.isEmpty()) {
+        foreach (const QString & key, parsedArgs.keys())
+        {
+            fprintf(stderr, "%s\n", HWApplication::tr("Unknown option argument: %1", "command-line").arg(QString("--") + key).toUtf8().constData());
+        }
+        fprintf(stderr, "\n%s", getUsage().toUtf8().constData());
+        return 1;
+    }
+
+    // end of parameter parsing
+
 #if defined Q_OS_WIN
     QPixmap pixmap(":res/splash.png");
     splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
@@ -146,55 +243,6 @@
     splash->setPixmap(pixmap);
     splash->show();
 #endif
-
-    engine = new FileEngineHandler(argv[0]);
-
-    app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
-
-    QStringList arguments = app.arguments();
-    QMap<QString, QString> parsedArgs;
-    {
-        QList<QString>::iterator i = arguments.begin();
-        while(i != arguments.end())
-        {
-            QString arg = *i;
-
-            QRegExp opt("--(\\S+)=(.+)");
-            if(opt.exactMatch(arg))
-            {
-                parsedArgs[opt.cap(1)] = opt.cap(2);
-                i = arguments.erase(i);
-            }
-            else
-            {
-                ++i;
-            }
-        }
-    }
-
-    if(parsedArgs.contains("data-dir"))
-    {
-        QFileInfo f(parsedArgs["data-dir"]);
-        if(!f.exists())
-        {
-            qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath();
-        }
-        *cDataDir = f.absoluteFilePath();
-        custom_data = true;
-    }
-
-    if(parsedArgs.contains("config-dir"))
-    {
-        QFileInfo f(parsedArgs["config-dir"]);
-        cfgdir->setPath(f.absoluteFilePath());
-        custom_config = true;
-    }
-    else
-    {
-        cfgdir->setPath(QDir::homePath());
-        custom_config = false;
-    }
-
     app.setStyle(new QPlastiqueStyle());
 
     QDateTime now = QDateTime::currentDateTime();
@@ -255,6 +303,7 @@
     }
 
     // setup PhysFS
+    engine = new FileEngineHandler(argv[0]);
     engine->mount(datadir->absolutePath());
     engine->mount(cfgdir->absolutePath() + "/Data");
     engine->mount(cfgdir->absolutePath());