improve engine cmd line parsing and its documentation
authorRowanD
Sun, 16 Dec 2012 01:25:11 +0100
changeset 8302 a7934cd12469
parent 8301 8bf36d368291
child 8303 6331bceac95c
improve engine cmd line parsing and its documentation
QTfrontend/hwform.cpp
hedgewars/ArgParsers.inc
hedgewars/hwengine.pas
--- a/QTfrontend/hwform.cpp	Sat Dec 15 23:00:07 2012 +0100
+++ b/QTfrontend/hwform.cpp	Sun Dec 16 01:25:11 2012 +0100
@@ -1879,19 +1879,25 @@
 // used for --set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]
 QString HWForm::getDemoArguments()
 {
+#ifdef Q_WS_WIN
+    QString userdir = cfgdir->absolutePath().replace("/","\\");
+#else
+    QString userdir = cfgdir->absolutePath();
+#endif
+
     QRect resolution = config->vid_Resolution();
-    return QString(QString::number(resolution.width()) + " "
-                   + QString::number(resolution.height()) + " "
-                   + QString::number(config->bitDepth()) + " " // bpp
-                   + QString::number(config->volume()) + " " // sound volume
-                   + (config->isMusicEnabled() ? "1" : "0") + " "
-                   + (config->isSoundEnabled() ? "1" : "0") + " "
-                   + config->language() + ".txt "
-                   + (config->vid_Fullscreen() ? "1" : "0") + " "
-                   + (config->isShowFPSEnabled() ? "1" : "0") + " "
-                   + (config->isAltDamageEnabled() ? "1" : "0") + " "
-                   + QString::number(config->timerInterval()) + " "
-                   + QString::number(config->translateQuality()));
+    return QString("--user-dir " + userdir
+                   + " --width " + QString::number(resolution.width())
+                   + " --height " + QString::number(resolution.height())
+                   + " --volume " + QString::number(config->volume())
+                   + (config->isMusicEnabled() ? "" : " --nomusic")
+                   + (config->isSoundEnabled() ? "" : " --nosound")
+                   + " --locale " + config->language() + ".txt"
+                   + (config->vid_Fullscreen() ? " --fullscreen" : "")
+                   + (config->isShowFPSEnabled() ? " --showfps" : "")
+                   + (config->isAltDamageEnabled() ? " --altdmg" : "")
+                   + " --frame-interval " + QString::number(config->timerInterval())
+                   + " --raw-quality " + QString::number(config->translateQuality()));
 }
 
 void HWForm::AssociateFiles()
@@ -1906,8 +1912,8 @@
     registry_hkcr.setValue("Hedgewars.Save/Default", tr("Hedgewars Save File", "File Types"));
     registry_hkcr.setValue("Hedgewars.Demo/DefaultIcon/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwdfile.ico\",0");
     registry_hkcr.setValue("Hedgewars.Save/DefaultIcon/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwsfile.ico\",0");
-    registry_hkcr.setValue("Hedgewars.Demo/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + cfgdir->absolutePath().replace("/","\\") + "\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" --set-everything "+arguments);
-    registry_hkcr.setValue("Hedgewars.Save/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + cfgdir->absolutePath().replace("/","\\") + "\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" --set-everything "+arguments);
+    registry_hkcr.setValue("Hedgewars.Demo/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" "+arguments);
+    registry_hkcr.setValue("Hedgewars.Save/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" "+arguments);
 #elif defined __APPLE__
     // only useful when other apps have taken precedence over our file extensions and you want to reset it
     system("defaults write com.apple.LaunchServices LSHandlers -array-add '<dict><key>LSHandlerContentTag</key><string>hwd</string><key>LSHandlerContentTagClass</key><string>public.filename-extension</string><key>LSHandlerRoleAll</key><string>org.hedgewars.desktop</string></dict>'");
@@ -1928,7 +1934,7 @@
     if (success) success = system("xdg-mime default hwengine.desktop application/x-hedgewars-demo")==0;
     if (success) success = system("xdg-mime default hwengine.desktop application/x-hedgewars-save")==0;
     // hack to add user's settings to hwengine. might be better at this point to read in the file, append it, and write it out to its new home.  This assumes no spaces in the data dir path
-    if (success) success = system(("sed -i 's/^\\(Exec=.*\\) \\([^ ]* %f\\)/\\1 "+cfgdir->absolutePath().replace(" ","\\\\ ").replace("/","\\/")+" \\2 --set-everything "+arguments+"/' "+QDir::home().absolutePath()+"/.local/share/applications/hwengine.desktop").toLocal8Bit().constData())==0;
+    if (success) success = system(("sed -i 's/^\\(Exec=.*\\) \\([^ ]* %f\\)/\\1 \\2 "+arguments+"/' "+QDir::home().absolutePath()+"/.local/share/applications/hwengine.desktop").toLocal8Bit().constData())==0;
 #endif
     if (success)
     {
--- a/hedgewars/ArgParsers.inc	Sat Dec 15 23:00:07 2012 +0100
+++ b/hedgewars/ArgParsers.inc	Sun Dec 16 01:25:11 2012 +0100
@@ -75,32 +75,26 @@
 begin
     WriteLn(stdout, 'Usage:');
     WriteLn(stdout, '');
-    WriteLn(stdout, '  hwengine [path to user hedgewars folder] <path to global data folder> <path to replay file> [options]');
+    WriteLn(stdout, '  hwengine <path to global data folder> <path to replay file> [options]');
     WriteLn(stdout, '');
     WriteLn(stdout, 'where [options] are any of the following:');
-    WriteLn(stdout, ' --locale [path to language file]');
-    WriteLn(stdout, ' --width  [screen width in pixels]');
+    WriteLn(stdout, ' --user-dir [path to user data folder]');
+    WriteLn(stdout, ' --locale [name of language file]');
+    WriteLn(stdout, ' --width [screen width in pixels]');
     WriteLn(stdout, ' --height [screen height in pixels]');
-    WriteLn(stdout, ' --depth  [color depth]');
     WriteLn(stdout, ' --volume [sound level]');
-    WriteLn(stdout, ' --time   [number of seconds]');
+    WriteLn(stdout, ' --frame-interval [milliseconds]');
+    WriteLn(stdout, ' --raw-quality [flags]');
+    WriteLn(stdout, ' --low-quality');
     WriteLn(stdout, ' --nomusic');
     WriteLn(stdout, ' --nosound');
     WriteLn(stdout, ' --fullscreen');
     WriteLn(stdout, ' --showfps');
     WriteLn(stdout, ' --altdmg');
-    WriteLn(stdout, ' --lowquality');
     WriteLn(stdout, ' --stats-only');
     WriteLn(stdout, ' --help');
     WriteLn(stdout, '');
-    WriteLn(stdout, 'Deprecated options:');
-    WriteLn(stdout, ' --set-video [screen width] [screen height] [color dept]');
-    WriteLn(stdout, ' --set-audio [volume] [enable music] [enable sounds]');
-    WriteLn(stdout, ' --set-other [language file] [full screen] [show FPS]');
-    WriteLn(stdout, ' --set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
-    WriteLn(stdout, ' --set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]');
-    WriteLn(stdout, '');
-    WriteLn(stdout, 'For a more detailed help and examples go to:');
+    WriteLn(stdout, 'For more detailed help and examples go to:');
     WriteLn(stdout, 'http://code.google.com/p/hedgewars/wiki/CommandLineOptions');
 end;
 
@@ -115,12 +109,16 @@
     getLongIntParameter:= tmpInt;
 end;
 
-function getStringParameter(str:String; var paramIndex:LongInt): String;
+function getStringParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): String;
 begin
     paramIndex:= paramIndex + 1;
+    wrongParameter:= (str='') or (Copy(str,1,2) = '--');
+    if wrongParameter then
+         WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a string, you passed "'+str+'"');
     getStringParameter:= str;
 end;
 
+
 procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt); Forward;
 
 function parseParameter(cmd:String; arg:String; var paramIndex:LongInt): Boolean;
@@ -129,60 +127,67 @@
 const otherArray: Array [1..3] of String = ('--locale','--fullscreen','--showfps');
 const mediaArray: Array [1..8] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen');
 const allArray: Array [1..12] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--time','--lowquality');
-const reallyAll: array[0..19] of shortstring = (
-            '--locale', '--width', '--height', '--depth', '--time'
-            , '--volume', '--nomusic', '--nosound', '--fullscreen', '--showfps'
-            , '--altdmg', '--lowquality', '--set-video', '--set-audio', '--set-other'
-            , '--set-multimedia', '--set-everything', '--stats-only', '--gci', '--help');
+const reallyAll: array[0..21] of shortstring = (
+                '--user-dir', '--locale', '--width', '--height', '--frame-interval', '--volume','--nomusic', '--nosound',
+                '--fullscreen', '--showfps', '--altdmg', '--low-quality', '--raw-quality',
+  {deprecated}  '--depth', '--set-video', '--set-audio', '--set-other', '--set-multimedia', '--set-everything',
+                '--stats-only', '--gci', '--help');
 var cmdIndex: byte;
 begin
     parseParameter:= false;
     cmdIndex:= 0;
+
+    //NOTE: Any update to the list of parameters must be reflected in the case statement below, the reallyAll array above,
+    //      the the DisplayUsage() procedure, the HWForm::getDemoArguments() function, and the online wiki
+
     while (cmdIndex <= High(reallyAll)) and (cmd <> reallyAll[cmdIndex]) do inc(cmdIndex);
     case cmdIndex of
-        {--locale}   0 : cLocaleFName   := getStringParameter (arg, paramIndex);
-        {--width}    1 : cScreenWidth   := getLongIntParameter(arg, paramIndex, parseParameter);
-        {--height}   2 : cScreenHeight  := getLongIntParameter(arg, paramIndex, parseParameter);
-        {--depth}    3 : cBits          := getLongIntParameter(arg, paramIndex, parseParameter);
-        {--time}     4 : cTimerInterval := getLongIntParameter(arg, paramIndex, parseParameter);
-        {--volume}   5 : SetVolume       ( getLongIntParameter(arg, paramIndex, parseParameter) );
-        {--nomusic}  6 : SetMusic        ( false );
-        {--nosound}  7 : SetSound        ( false );
-        {--fullscreen}   8 : cFullScreen    := true;
-        {--showfps}      9 : cShowFPS       := true;
-        {--altdmg}      10 : cAltDamage     := true;
-        {--lowquality}  11 : cReducedQuality:= ($FFFFFFFF * getLongIntParameter(arg, paramIndex, parseParameter)) xor rqLowRes; //HACK!
-        {--set-video}   12 : parseClassicParameter(videoArray,3,paramIndex);
-        {--set-audio}   13 : parseClassicParameter(audioArray,3,paramIndex);
-        {--set-other}   14 : parseClassicParameter(otherArray,3,paramIndex);
-        {--set-multimedia}  15 : parseClassicParameter(mediaArray,8,paramIndex);
-        {--set-everything}  16 : parseClassicParameter(allArray,12,paramIndex);
-        {--stats-only}  17 : begin
-                             cOnlyStats:= true;
-                             SetSound(false);
-                             SetMusic(false);
-                             cReducedQuality:= $FFFFFFFF xor rqLowRes;
-                             end;
-        {--gci}  18 : begin            //     We had to make up all this saved space some how...     \\
-                      WriteLn(stdout, '                                                                ');
-                      WriteLn(stdout, '      /\\\\\\\\\\\\        /\\\\\\\\\  /\\\\\\\\\\\             ');
-                      WriteLn(stdout, '     /\\\//////////      /\\\////////  \/////\\\///             ');
-                      WriteLn(stdout, '     /\\\               /\\\/               \/\\\               ');
-                      WriteLn(stdout, '     \/\\\    /\\\\\\\  /\\\                 \/\\\              ');
-                      WriteLn(stdout, '      \/\\\   \/////\\\ \/\\\                 \/\\\             ');
-                      WriteLn(stdout, '       \/\\\       \/\\\ \//\\\                \/\\\            ');
-                      WriteLn(stdout, '        \/\\\       \/\\\  \///\\\              \/\\\           ');
-                      WriteLn(stdout, '         \//\\\\\\\\\\\\/     \////\\\\\\\\\  /\\\\\\\\\\\      ');
-                      WriteLn(stdout, '          \////////////           \/////////  \///////////      ');
-                      WriteLn(stdout, '                                                                ');
-                      WriteLn(stdout, ' Command Line Parser Implementation by a Google Code-In Student ');
-                      WriteLn(stdout, '             ASCII Art easter egg idea by @sheepluva            ');
-                      WriteLn(stdout, '                                                                ');
-                      end;
-        {--help}  19 : begin
-                       DisplayUsage();
-                       GameType:= gmtSyntax;
-                       end;
+        {--user-dir}        0 : UserPathPrefix := getStringParameter (arg, paramIndex, parseParameter);
+        {--locale}          1 : cLocaleFName   := getStringParameter (arg, paramIndex, parseParameter);
+        {--width}           2 : cScreenWidth   := getLongIntParameter(arg, paramIndex, parseParameter);
+        {--height}          3 : cScreenHeight  := getLongIntParameter(arg, paramIndex, parseParameter);
+        {--frame-interval}  4 : cTimerInterval := getLongIntParameter(arg, paramIndex, parseParameter);
+        {--volume}          5 : SetVolume       ( getLongIntParameter(arg, paramIndex, parseParameter) );
+        {--nomusic}         6 : SetMusic        ( false );
+        {--nosound}         7 : SetSound        ( false );
+        {--fullscreen}      8 : cFullScreen    := true;
+        {--showfps}         9 : cShowFPS       := true;
+        {--altdmg}         10 : cAltDamage     := true;
+        {--low-quality}    11 : cReducedQuality:= $FFFFFFFF xor rqLowRes;
+        {--raw-quality}    12 : cReducedQuality:= getLongIntParameter(arg, paramIndex, parseParameter);
+        {deprecated options}
+        {--depth}          13 : cBits          := getLongIntParameter(arg, paramIndex, parseParameter);
+        {--set-video}      14 : parseClassicParameter(videoArray,3,paramIndex);
+        {--set-audio}      15 : parseClassicParameter(audioArray,3,paramIndex);
+        {--set-other}      16 : parseClassicParameter(otherArray,3,paramIndex);
+        {--set-multimedia} 17 : parseClassicParameter(mediaArray,8,paramIndex);
+        {--set-everything} 18 : parseClassicParameter(allArray,12,paramIndex);
+        {anything else}
+        {--stats-only}     19 : begin
+                                cOnlyStats:= true;
+                                cReducedQuality:= $FFFFFFFF xor rqLowRes;
+                                SetSound(false);
+                                end;
+        {--gci}            20 : begin            //     We had to make up all this saved space some how...     \\
+                                WriteLn(stdout, '                                                                ');
+                                WriteLn(stdout, '      /\\\\\\\\\\\\        /\\\\\\\\\  /\\\\\\\\\\\             ');
+                                WriteLn(stdout, '     /\\\//////////      /\\\////////  \/////\\\///             ');
+                                WriteLn(stdout, '     /\\\               /\\\/               \/\\\               ');
+                                WriteLn(stdout, '     \/\\\    /\\\\\\\  /\\\                 \/\\\              ');
+                                WriteLn(stdout, '      \/\\\   \/////\\\ \/\\\                 \/\\\             ');
+                                WriteLn(stdout, '       \/\\\       \/\\\ \//\\\                \/\\\            ');
+                                WriteLn(stdout, '        \/\\\       \/\\\  \///\\\              \/\\\           ');
+                                WriteLn(stdout, '         \/\\\\\\\\\\\\\/     \////\\\\\\\\\  /\\\\\\\\\\\      ');
+                                WriteLn(stdout, '          \/////////////          \/////////  \///////////      ');
+                                WriteLn(stdout, '                                                                ');
+                                WriteLn(stdout, ' Command Line Parser Implementation by a Google Code-In Student ');
+                                WriteLn(stdout, '             ASCII Art easter egg idea by @sheepluva            ');
+                                WriteLn(stdout, '                                                                ');
+                                end;
+        {--help}           21 : begin
+                                DisplayUsage();
+                                GameType:= gmtSyntax;
+                                end;
     else
         begin
         WriteLn(stderr, 'ERROR: '+cmd+' is not a valid argument');
@@ -206,7 +211,7 @@
             or (cmdArray[index] = '--fullscreen')
             or (cmdArray[index] = '--showfps')
             or (cmdArray[index] = '--altdmg')
-            or (cmdArray[index] = '--lowquality');
+            or (cmdArray[index] = '--low-quality');
         if (not isBool) or ((ParamStr(paramIndex)='1') and (cmdArray[index]<>'--nomusic') and (cmdArray[index]<>'--nosound')) then
             parseParameter(cmdArray[index], ParamStr(paramIndex), tmpInt);
         index:= index+1;
--- a/hedgewars/hwengine.pas	Sat Dec 15 23:00:07 2012 +0100
+++ b/hedgewars/hwengine.pas	Sun Dec 16 01:25:11 2012 +0100
@@ -590,21 +590,11 @@
         begin
         if (ParamCount >= 2) then
             begin
-            UserPathPrefix := ParamStr(1);
-            PathPrefix     := ParamStr(2);
-            end;
-        if (ParamCount >= 3) then
-            recordFilename := ParamStr(3);
-        if (ParamCount = 2) or
-           ((ParamCount >= 3) and (Copy(recordFileName,1,2) = '--')) then
-            begin
             UserPathPrefix := '.';
             PathPrefix     := ParamStr(1);
             recordFileName := ParamStr(2);
             startIndex     := 3;
-            end
-        else
-            startIndex := 4;
+            end;
 
         playReplayFileWithParameters(startIndex);
         end;