hedgewars/ArgParsers.inc
branchflibqtfrontend
changeset 8304 620560c89284
parent 8278 9cdfcb5b7ddd
parent 8302 a7934cd12469
child 8363 0b4ac686fc44
equal deleted inserted replaced
8282:1f81bcce3ee6 8304:620560c89284
    73 
    73 
    74 procedure DisplayUsage;
    74 procedure DisplayUsage;
    75 begin
    75 begin
    76     WriteLn(stdout, 'Usage:');
    76     WriteLn(stdout, 'Usage:');
    77     WriteLn(stdout, '');
    77     WriteLn(stdout, '');
    78     WriteLn(stdout, '  hwengine [path to user hedgewars folder] <path to global data folder> <path to replay file> [options]');
    78     WriteLn(stdout, '  hwengine <path to global data folder> <path to replay file> [options]');
    79     WriteLn(stdout, '');
    79     WriteLn(stdout, '');
    80     WriteLn(stdout, 'where [options] are any of the following:');
    80     WriteLn(stdout, 'where [options] are any of the following:');
    81     WriteLn(stdout, ' --locale [path to language file]');
    81     WriteLn(stdout, ' --user-dir [path to user data folder]');
    82     WriteLn(stdout, ' --width  [screen width in pixels]');
    82     WriteLn(stdout, ' --locale [name of language file]');
       
    83     WriteLn(stdout, ' --width [screen width in pixels]');
    83     WriteLn(stdout, ' --height [screen height in pixels]');
    84     WriteLn(stdout, ' --height [screen height in pixels]');
    84     WriteLn(stdout, ' --depth  [color depth]');
       
    85     WriteLn(stdout, ' --volume [sound level]');
    85     WriteLn(stdout, ' --volume [sound level]');
    86     WriteLn(stdout, ' --time   [number of seconds]');
    86     WriteLn(stdout, ' --frame-interval [milliseconds]');
       
    87     WriteLn(stdout, ' --raw-quality [flags]');
       
    88     WriteLn(stdout, ' --low-quality');
    87     WriteLn(stdout, ' --nomusic');
    89     WriteLn(stdout, ' --nomusic');
    88     WriteLn(stdout, ' --nosound');
    90     WriteLn(stdout, ' --nosound');
    89     WriteLn(stdout, ' --fullscreen');
    91     WriteLn(stdout, ' --fullscreen');
    90     WriteLn(stdout, ' --showfps');
    92     WriteLn(stdout, ' --showfps');
    91     WriteLn(stdout, ' --altdmg');
    93     WriteLn(stdout, ' --altdmg');
    92     WriteLn(stdout, ' --lowquality');
       
    93     WriteLn(stdout, ' --stats-only');
    94     WriteLn(stdout, ' --stats-only');
    94     WriteLn(stdout, ' --help');
    95     WriteLn(stdout, ' --help');
    95     WriteLn(stdout, '');
    96     WriteLn(stdout, '');
    96     WriteLn(stdout, 'Deprecated options:');
    97     WriteLn(stdout, 'For more detailed help and examples go to:');
    97     WriteLn(stdout, ' --set-video [screen width] [screen height] [color dept]');
       
    98     WriteLn(stdout, ' --set-audio [volume] [enable music] [enable sounds]');
       
    99     WriteLn(stdout, ' --set-other [language file] [full screen] [show FPS]');
       
   100     WriteLn(stdout, ' --set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
       
   101     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]');
       
   102     WriteLn(stdout, '');
       
   103     WriteLn(stdout, 'For a more detailed help and examples go to:');
       
   104     WriteLn(stdout, 'http://code.google.com/p/hedgewars/wiki/CommandLineOptions');
    98     WriteLn(stdout, 'http://code.google.com/p/hedgewars/wiki/CommandLineOptions');
   105 end;
    99 end;
   106 
   100 
   107 function getLongIntParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): LongInt;
   101 function getLongIntParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): LongInt;
   108 var tmpInt, c: LongInt;
   102 var tmpInt, c: LongInt;
   113     if wrongParameter then
   107     if wrongParameter then
   114         WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a number, you passed "'+str+'"');
   108         WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a number, you passed "'+str+'"');
   115     getLongIntParameter:= tmpInt;
   109     getLongIntParameter:= tmpInt;
   116 end;
   110 end;
   117 
   111 
   118 function getStringParameter(str:String; var paramIndex:LongInt): String;
   112 function getStringParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): String;
   119 begin
   113 begin
   120     paramIndex:= paramIndex + 1;
   114     paramIndex:= paramIndex + 1;
       
   115     wrongParameter:= (str='') or (Copy(str,1,2) = '--');
       
   116     if wrongParameter then
       
   117          WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a string, you passed "'+str+'"');
   121     getStringParameter:= str;
   118     getStringParameter:= str;
   122 end;
   119 end;
       
   120 
   123 
   121 
   124 procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt); Forward;
   122 procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt); Forward;
   125 
   123 
   126 function parseParameter(cmd:String; arg:String; var paramIndex:LongInt): Boolean;
   124 function parseParameter(cmd:String; arg:String; var paramIndex:LongInt): Boolean;
   127 const videoArray: Array [1..3] of String = ('--width','--height','--depth');
   125 const videoArray: Array [1..3] of String = ('--width','--height','--depth');
   128 const audioArray: Array [1..3] of String = ('--volume','--nomusic','--nosound');
   126 const audioArray: Array [1..3] of String = ('--volume','--nomusic','--nosound');
   129 const otherArray: Array [1..3] of String = ('--locale','--fullscreen','--showfps');
   127 const otherArray: Array [1..3] of String = ('--locale','--fullscreen','--showfps');
   130 const mediaArray: Array [1..8] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen');
   128 const mediaArray: Array [1..8] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen');
   131 const allArray: Array [1..12] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--time','--lowquality');
   129 const allArray: Array [1..12] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--time','--lowquality');
   132 const reallyAll: array[0..19] of shortstring = (
   130 const reallyAll: array[0..21] of shortstring = (
   133             '--locale', '--width', '--height', '--depth', '--time'
   131                 '--user-dir', '--locale', '--width', '--height', '--frame-interval', '--volume','--nomusic', '--nosound',
   134             , '--volume', '--nomusic', '--nosound', '--fullscreen', '--showfps'
   132                 '--fullscreen', '--showfps', '--altdmg', '--low-quality', '--raw-quality',
   135             , '--altdmg', '--lowquality', '--set-video', '--set-audio', '--set-other'
   133   {deprecated}  '--depth', '--set-video', '--set-audio', '--set-other', '--set-multimedia', '--set-everything',
   136             , '--set-multimedia', '--set-everything', '--stats-only', '--gci', '--help');
   134                 '--stats-only', '--gci', '--help');
   137 var cmdIndex: byte;
   135 var cmdIndex: byte;
   138 begin
   136 begin
   139     parseParameter:= false;
   137     parseParameter:= false;
   140     cmdIndex:= 0;
   138     cmdIndex:= 0;
       
   139 
       
   140     //NOTE: Any update to the list of parameters must be reflected in the case statement below, the reallyAll array above,
       
   141     //      the the DisplayUsage() procedure, the HWForm::getDemoArguments() function, and the online wiki
       
   142 
   141     while (cmdIndex <= High(reallyAll)) and (cmd <> reallyAll[cmdIndex]) do inc(cmdIndex);
   143     while (cmdIndex <= High(reallyAll)) and (cmd <> reallyAll[cmdIndex]) do inc(cmdIndex);
   142     case cmdIndex of
   144     case cmdIndex of
   143         {--locale}   0 : cLocaleFName   := getStringParameter (arg, paramIndex);
   145         {--user-dir}        0 : UserPathPrefix := getStringParameter (arg, paramIndex, parseParameter);
   144         {--width}    1 : cScreenWidth   := getLongIntParameter(arg, paramIndex, parseParameter);
   146         {--locale}          1 : cLocaleFName   := getStringParameter (arg, paramIndex, parseParameter);
   145         {--height}   2 : cScreenHeight  := getLongIntParameter(arg, paramIndex, parseParameter);
   147         {--width}           2 : cScreenWidth   := getLongIntParameter(arg, paramIndex, parseParameter);
   146         {--depth}    3 : cBits          := getLongIntParameter(arg, paramIndex, parseParameter);
   148         {--height}          3 : cScreenHeight  := getLongIntParameter(arg, paramIndex, parseParameter);
   147         {--time}     4 : cTimerInterval := getLongIntParameter(arg, paramIndex, parseParameter);
   149         {--frame-interval}  4 : cTimerInterval := getLongIntParameter(arg, paramIndex, parseParameter);
   148         {--volume}   5 : SetVolume       ( getLongIntParameter(arg, paramIndex, parseParameter) );
   150         {--volume}          5 : SetVolume       ( getLongIntParameter(arg, paramIndex, parseParameter) );
   149         {--nomusic}  6 : SetMusic        ( false );
   151         {--nomusic}         6 : SetMusic        ( false );
   150         {--nosound}  7 : SetSound        ( false );
   152         {--nosound}         7 : SetSound        ( false );
   151         {--fullscreen}   8 : cFullScreen    := true;
   153         {--fullscreen}      8 : cFullScreen    := true;
   152         {--showfps}      9 : cShowFPS       := true;
   154         {--showfps}         9 : cShowFPS       := true;
   153         {--altdmg}      10 : cAltDamage     := true;
   155         {--altdmg}         10 : cAltDamage     := true;
   154         {--lowquality}  11 : cReducedQuality:= ($FFFFFFFF * getLongIntParameter(arg, paramIndex, parseParameter)) xor rqLowRes; //HACK!
   156         {--low-quality}    11 : cReducedQuality:= $FFFFFFFF xor rqLowRes;
   155         {--set-video}   12 : parseClassicParameter(videoArray,3,paramIndex);
   157         {--raw-quality}    12 : cReducedQuality:= getLongIntParameter(arg, paramIndex, parseParameter);
   156         {--set-audio}   13 : parseClassicParameter(audioArray,3,paramIndex);
   158         {deprecated options}
   157         {--set-other}   14 : parseClassicParameter(otherArray,3,paramIndex);
   159         {--depth}          13 : cBits          := getLongIntParameter(arg, paramIndex, parseParameter);
   158         {--set-multimedia}  15 : parseClassicParameter(mediaArray,8,paramIndex);
   160         {--set-video}      14 : parseClassicParameter(videoArray,3,paramIndex);
   159         {--set-everything}  16 : parseClassicParameter(allArray,12,paramIndex);
   161         {--set-audio}      15 : parseClassicParameter(audioArray,3,paramIndex);
   160         {--stats-only}  17 : begin
   162         {--set-other}      16 : parseClassicParameter(otherArray,3,paramIndex);
   161                              cOnlyStats:= true;
   163         {--set-multimedia} 17 : parseClassicParameter(mediaArray,8,paramIndex);
   162                              SetSound(false);
   164         {--set-everything} 18 : parseClassicParameter(allArray,12,paramIndex);
   163                              SetMusic(false);
   165         {anything else}
   164                              cReducedQuality:= $FFFFFFFF xor rqLowRes;
   166         {--stats-only}     19 : begin
   165                              end;
   167                                 cOnlyStats:= true;
   166         {--gci}  18 : begin            //     We had to make up all this saved space some how...     \\
   168                                 cReducedQuality:= $FFFFFFFF xor rqLowRes;
   167                       WriteLn(stdout, '                                                                ');
   169                                 SetSound(false);
   168                       WriteLn(stdout, '      /\\\\\\\\\\\\        /\\\\\\\\\  /\\\\\\\\\\\             ');
   170                                 end;
   169                       WriteLn(stdout, '     /\\\//////////      /\\\////////  \/////\\\///             ');
   171         {--gci}            20 : begin            //     We had to make up all this saved space some how...     \\
   170                       WriteLn(stdout, '     /\\\               /\\\/               \/\\\               ');
   172                                 WriteLn(stdout, '                                                                ');
   171                       WriteLn(stdout, '     \/\\\    /\\\\\\\  /\\\                 \/\\\              ');
   173                                 WriteLn(stdout, '      /\\\\\\\\\\\\        /\\\\\\\\\  /\\\\\\\\\\\             ');
   172                       WriteLn(stdout, '      \/\\\   \/////\\\ \/\\\                 \/\\\             ');
   174                                 WriteLn(stdout, '     /\\\//////////      /\\\////////  \/////\\\///             ');
   173                       WriteLn(stdout, '       \/\\\       \/\\\ \//\\\                \/\\\            ');
   175                                 WriteLn(stdout, '     /\\\               /\\\/               \/\\\               ');
   174                       WriteLn(stdout, '        \/\\\       \/\\\  \///\\\              \/\\\           ');
   176                                 WriteLn(stdout, '     \/\\\    /\\\\\\\  /\\\                 \/\\\              ');
   175                       WriteLn(stdout, '         \//\\\\\\\\\\\\/     \////\\\\\\\\\  /\\\\\\\\\\\      ');
   177                                 WriteLn(stdout, '      \/\\\   \/////\\\ \/\\\                 \/\\\             ');
   176                       WriteLn(stdout, '          \////////////           \/////////  \///////////      ');
   178                                 WriteLn(stdout, '       \/\\\       \/\\\ \//\\\                \/\\\            ');
   177                       WriteLn(stdout, '                                                                ');
   179                                 WriteLn(stdout, '        \/\\\       \/\\\  \///\\\              \/\\\           ');
   178                       WriteLn(stdout, ' Command Line Parser Implementation by a Google Code-In Student ');
   180                                 WriteLn(stdout, '         \/\\\\\\\\\\\\\/     \////\\\\\\\\\  /\\\\\\\\\\\      ');
   179                       WriteLn(stdout, '             ASCII Art easter egg idea by @sheepluva            ');
   181                                 WriteLn(stdout, '          \/////////////          \/////////  \///////////      ');
   180                       WriteLn(stdout, '                                                                ');
   182                                 WriteLn(stdout, '                                                                ');
   181                       end;
   183                                 WriteLn(stdout, ' Command Line Parser Implementation by a Google Code-In Student ');
   182         {--help}  19 : begin
   184                                 WriteLn(stdout, '             ASCII Art easter egg idea by @sheepluva            ');
   183                        DisplayUsage();
   185                                 WriteLn(stdout, '                                                                ');
   184                        GameType:= gmtSyntax;
   186                                 end;
   185                        end;
   187         {--help}           21 : begin
       
   188                                 DisplayUsage();
       
   189                                 GameType:= gmtSyntax;
       
   190                                 end;
   186     else
   191     else
   187         begin
   192         begin
   188         WriteLn(stderr, 'ERROR: '+cmd+' is not a valid argument');
   193         WriteLn(stderr, 'ERROR: '+cmd+' is not a valid argument');
   189         parseParameter:= true;
   194         parseParameter:= true;
   190         end
   195         end
   204         isBool:= (cmdArray[index] = '--nomusic')
   209         isBool:= (cmdArray[index] = '--nomusic')
   205             or (cmdArray[index] = '--nosound')
   210             or (cmdArray[index] = '--nosound')
   206             or (cmdArray[index] = '--fullscreen')
   211             or (cmdArray[index] = '--fullscreen')
   207             or (cmdArray[index] = '--showfps')
   212             or (cmdArray[index] = '--showfps')
   208             or (cmdArray[index] = '--altdmg')
   213             or (cmdArray[index] = '--altdmg')
   209             or (cmdArray[index] = '--lowquality');
   214             or (cmdArray[index] = '--low-quality');
   210         if (not isBool) or ((ParamStr(paramIndex)='1') and (cmdArray[index]<>'--nomusic') and (cmdArray[index]<>'--nosound')) then
   215         if (not isBool) or ((ParamStr(paramIndex)='1') and (cmdArray[index]<>'--nomusic') and (cmdArray[index]<>'--nosound')) then
   211             parseParameter(cmdArray[index], ParamStr(paramIndex), tmpInt);
   216             parseParameter(cmdArray[index], ParamStr(paramIndex), tmpInt);
   212         index:= index+1;
   217         index:= index+1;
   213         end;
   218         end;
   214 end;
   219 end;