hedgewars/ArgParsers.pas
changeset 10015 4feced261c68
parent 9998 736015b847e3
parent 9954 bf51bc7e2808
child 10078 8572d1f8b2f0
equal deleted inserted replaced
10014:56d2f2d5aad8 10015:4feced261c68
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18  
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit ArgParsers;
    21 unit ArgParsers;
    22 interface
    22 interface
    23 
    23 
   109         WriteLn(stderr, 'ERROR: use of --port is not allowed');
   109         WriteLn(stderr, 'ERROR: use of --port is not allowed');
   110         wrongParameter := true;
   110         wrongParameter := true;
   111         end
   111         end
   112 end;
   112 end;
   113 
   113 
   114 function parseNick(nick: String): String;
   114 function parseNick(nick: shortstring): shortstring;
   115 begin
   115 begin
   116     if isInternal then
   116     if isInternal then
   117         parseNick:= DecodeBase64(nick)
   117         parseNick:= DecodeBase64(nick)
   118     else
   118     else
   119         parseNick:= nick;
   119         parseNick:= nick;
   156     cVideoQuality:= StrToInt(ParamStr(paramIndex));      inc(paramIndex);
   156     cVideoQuality:= StrToInt(ParamStr(paramIndex));      inc(paramIndex);
   157     cAudioCodec:= ParamStr(paramIndex);                  inc(paramIndex);
   157     cAudioCodec:= ParamStr(paramIndex);                  inc(paramIndex);
   158 {$ENDIF}
   158 {$ENDIF}
   159 end;
   159 end;
   160 
   160 
   161 function getLongIntParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): LongInt;
   161 function getLongIntParameter(str:shortstring; var paramIndex:LongInt; var wrongParameter:Boolean): LongInt;
   162 var tmpInt, c: LongInt;
   162 var tmpInt, c: LongInt;
   163 begin
   163 begin
   164     inc(paramIndex);
   164     inc(paramIndex);
       
   165 {$IFDEF PAS2C}
       
   166     val(str, tmpInt);
       
   167 {$ELSE}
   165     val(str, tmpInt, c);
   168     val(str, tmpInt, c);
   166     wrongParameter:= c <> 0;
   169     wrongParameter:= c <> 0;
   167     if wrongParameter then
   170     if wrongParameter then
   168         WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a number, you passed "'+str+'"');
   171         WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a number, you passed "'+str+'"');
       
   172 {$ENDIF}
   169     getLongIntParameter:= tmpInt;
   173     getLongIntParameter:= tmpInt;
   170 end;
   174 end;
   171 
   175 
   172 function getStringParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): String;
   176 function getstringParameter(str:shortstring; var paramIndex:LongInt; var wrongParameter:Boolean): shortstring;
   173 begin
   177 begin
   174     inc(paramIndex);
   178     inc(paramIndex);
   175     wrongParameter:= (str='') or (Copy(str,1,2) = '--');
   179     wrongParameter:= (str='') or (Copy(str,1,2) = '--');
   176     if wrongParameter then
   180     if wrongParameter then
   177          WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a string, you passed "'+str+'"');
   181          WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a string, you passed "'+str+'"');
   178     getStringParameter:= str;
   182     getstringParameter:= str;
   179 end;
   183 end;
   180 
   184 
   181 
   185 procedure parseClassicParameter(cmdArray: array of string; size:LongInt; var paramIndex:LongInt); forward;
   182 procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt); Forward;
   186 
   183 
   187 function parseParameter(cmd:string; arg:string; var paramIndex:LongInt): Boolean;
   184 function parseParameter(cmd:String; arg:String; var paramIndex:LongInt): Boolean;
   188 const videoArray: Array [1..5] of string = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth');
   185 const videoArray: Array [1..5] of String = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth');
   189       audioArray: Array [1..3] of string = ('--volume','--nomusic','--nosound');
   186       audioArray: Array [1..3] of String = ('--volume','--nomusic','--nosound');
   190       otherArray: Array [1..3] of string = ('--locale','--fullscreen','--showfps');
   187       otherArray: Array [1..3] of String = ('--locale','--fullscreen','--showfps');
   191       mediaArray: Array [1..10] of string = ('--fullscreen-width', '--fullscreen-height', '--width', '--height', '--depth', '--volume','--nomusic','--nosound','--locale','--fullscreen');
   188       mediaArray: Array [1..10] of String = ('--fullscreen-width', '--fullscreen-height', '--width', '--height', '--depth', '--volume','--nomusic','--nosound','--locale','--fullscreen');
   192       allArray: Array [1..18] of string = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--frame-interval','--low-quality','--no-teamtag','--no-hogtag','--no-healthtag','--translucent-tags');
   189       allArray: Array [1..18] of String = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--frame-interval','--low-quality','--no-teamtag','--no-hogtag','--no-healthtag','--translucent-tags');
       
   190       reallyAll: array[0..35] of shortstring = (
   193       reallyAll: array[0..35] of shortstring = (
   191                 '--prefix', '--user-prefix', '--locale', '--fullscreen-width', '--fullscreen-height', '--width',
   194                 '--prefix', '--user-prefix', '--locale', '--fullscreen-width', '--fullscreen-height', '--width',
   192                 '--height', '--frame-interval', '--volume','--nomusic', '--nosound',
   195                 '--height', '--frame-interval', '--volume','--nomusic', '--nosound',
   193                 '--fullscreen', '--showfps', '--altdmg', '--low-quality', '--raw-quality', '--stereo', '--nick',
   196                 '--fullscreen', '--showfps', '--altdmg', '--low-quality', '--raw-quality', '--stereo', '--nick',
   194   {deprecated}  '--depth', '--set-video', '--set-audio', '--set-other', '--set-multimedia', '--set-everything',
   197   {deprecated}  '--depth', '--set-video', '--set-audio', '--set-other', '--set-multimedia', '--set-everything',
   202     //NOTE: Any update to the list of parameters must be reflected in the case statement below, the reallyAll array above,
   205     //NOTE: Any update to the list of parameters must be reflected in the case statement below, the reallyAll array above,
   203     //      the the DisplayUsage() procedure, the HWForm::getDemoArguments() function, and the online wiki
   206     //      the the DisplayUsage() procedure, the HWForm::getDemoArguments() function, and the online wiki
   204 
   207 
   205     while (cmdIndex <= High(reallyAll)) and (cmd <> reallyAll[cmdIndex]) do inc(cmdIndex);
   208     while (cmdIndex <= High(reallyAll)) and (cmd <> reallyAll[cmdIndex]) do inc(cmdIndex);
   206     case cmdIndex of
   209     case cmdIndex of
   207         {--prefix}               0 : PathPrefix        := getStringParameter (arg, paramIndex, parseParameter);
   210         {--prefix}               0 : PathPrefix        := getstringParameter (arg, paramIndex, parseParameter);
   208         {--user-prefix}          1 : UserPathPrefix    := getStringParameter (arg, paramIndex, parseParameter);
   211         {--user-prefix}          1 : UserPathPrefix    := getstringParameter (arg, paramIndex, parseParameter);
   209         {--locale}               2 : cLocaleFName      := getStringParameter (arg, paramIndex, parseParameter);
   212         {--locale}               2 : cLocaleFName      := getstringParameter (arg, paramIndex, parseParameter);
   210         {--fullscreen-width}     3 : cFullscreenWidth  := max(getLongIntParameter(arg, paramIndex, parseParameter), cMinScreenWidth);
   213         {--fullscreen-width}     3 : cFullscreenWidth  := max(getLongIntParameter(arg, paramIndex, parseParameter), cMinScreenWidth);
   211         {--fullscreen-height}    4 : cFullscreenHeight := max(getLongIntParameter(arg, paramIndex, parseParameter), cMinScreenHeight);
   214         {--fullscreen-height}    4 : cFullscreenHeight := max(getLongIntParameter(arg, paramIndex, parseParameter), cMinScreenHeight);
   212         {--width}                5 : cWindowedWidth    := max(2 * (getLongIntParameter(arg, paramIndex, parseParameter) div 2), cMinScreenWidth);
   215         {--width}                5 : cWindowedWidth    := max(2 * (getLongIntParameter(arg, paramIndex, parseParameter) div 2), cMinScreenWidth);
   213         {--height}               6 : cWindowedHeight   := max(2 * (getLongIntParameter(arg, paramIndex, parseParameter) div 2), cMinScreenHeight);
   216         {--height}               6 : cWindowedHeight   := max(2 * (getLongIntParameter(arg, paramIndex, parseParameter) div 2), cMinScreenHeight);
   214         {--frame-interval}       7 : cTimerInterval    := getLongIntParameter(arg, paramIndex, parseParameter);
   217         {--frame-interval}       7 : cTimerInterval    := getLongIntParameter(arg, paramIndex, parseParameter);
   219         {--showfps}             12 : cShowFPS          := true;
   222         {--showfps}             12 : cShowFPS          := true;
   220         {--altdmg}              13 : cAltDamage        := true;
   223         {--altdmg}              13 : cAltDamage        := true;
   221         {--low-quality}         14 : cReducedQuality   := $FFFFFFFF xor rqLowRes;
   224         {--low-quality}         14 : cReducedQuality   := $FFFFFFFF xor rqLowRes;
   222         {--raw-quality}         15 : cReducedQuality   := getLongIntParameter(arg, paramIndex, parseParameter);
   225         {--raw-quality}         15 : cReducedQuality   := getLongIntParameter(arg, paramIndex, parseParameter);
   223         {--stereo}              16 : setStereoMode      ( getLongIntParameter(arg, paramIndex, parseParameter) );
   226         {--stereo}              16 : setStereoMode      ( getLongIntParameter(arg, paramIndex, parseParameter) );
   224         {--nick}                17 : UserNick          := parseNick( getStringParameter(arg, paramIndex, parseParameter) );
   227         {--nick}                17 : UserNick          := parseNick( getstringParameter(arg, paramIndex, parseParameter) );
   225         {deprecated options}
   228         {deprecated options}
   226         {--depth}               18 : setDepth(paramIndex);
   229         {--depth}               18 : setDepth(paramIndex);
   227         {--set-video}           19 : parseClassicParameter(videoArray,5,paramIndex);
   230         {--set-video}           19 : parseClassicParameter(videoArray,5,paramIndex);
   228         {--set-audio}           20 : parseClassicParameter(audioArray,3,paramIndex);
   231         {--set-audio}           20 : parseClassicParameter(audioArray,3,paramIndex);
   229         {--set-other}           21 : parseClassicParameter(otherArray,3,paramIndex);
   232         {--set-other}           21 : parseClassicParameter(otherArray,3,paramIndex);
   240         {--help}                30 : DisplayUsage();
   243         {--help}                30 : DisplayUsage();
   241         {--no-teamtag}          31 : cTagsMask := cTagsMask and not htTeamName;
   244         {--no-teamtag}          31 : cTagsMask := cTagsMask and not htTeamName;
   242         {--no-hogtag}           32 : cTagsMask := cTagsMask and not htName;
   245         {--no-hogtag}           32 : cTagsMask := cTagsMask and not htName;
   243         {--no-healthtag}        33 : cTagsMask := cTagsMask and not htHealth;
   246         {--no-healthtag}        33 : cTagsMask := cTagsMask and not htHealth;
   244         {--translucent-tags}    34 : cTagsMask := cTagsMask or htTransparent;
   247         {--translucent-tags}    34 : cTagsMask := cTagsMask or htTransparent;
   245         {--lua-test}            35 : begin cTestLua := true; cScriptName := getStringParameter(arg, paramIndex, parseParameter); WriteLn(stdout, 'Lua test file specified: ' + cScriptName);end;
   248         {--lua-test}            35 : begin cTestLua := true; cScriptName := getstringParameter(arg, paramIndex, parseParameter); WriteLn(stdout, 'Lua test file specified: ' + cScriptName);end;
   246     else
   249     else
   247         begin
   250         begin
   248         //Assume the first "non parameter" is the replay file, anything else is invalid
   251         //Assume the first "non parameter" is the replay file, anything else is invalid
   249         if (recordFileName = '') and (Copy(cmd,1,2) <> '--') then
   252         if (recordFileName = '') and (Copy(cmd,1,2) <> '--') then
   250             recordFileName := cmd
   253             recordFileName := cmd
   255             end;
   258             end;
   256         end;
   259         end;
   257     end;
   260     end;
   258 end;
   261 end;
   259 
   262 
   260 procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt);
   263 procedure parseClassicParameter(cmdArray: array of string; size:LongInt; var paramIndex:LongInt);
   261 var index, tmpInt: LongInt;
   264 var index, tmpInt: LongInt;
   262     isBool, isValid: Boolean;
   265     isBool, isValid: Boolean;
   263     cmd, arg, newSyntax: String;
   266     cmd, arg, newSyntax: string;
   264 begin
   267 begin
   265     WriteLn(stdout, 'WARNING: you are using a deprecated command, which could be removed in a future version!');
   268     WriteLn(stdout, 'WARNING: you are using a deprecated command, which could be removed in a future version!');
   266     WriteLn(stdout, '         Consider updating to the latest syntax, which is much more flexible!');
   269     WriteLn(stdout, '         Consider updating to the latest syntax, which is much more flexible!');
   267     WriteLn(stdout, '         Run `hwegine --help` to learn it!');
   270     WriteLn(stdout, '         Run `hwegine --help` to learn it!');
   268     WriteLn(stdout, '');
   271     WriteLn(stdout, '');
   285             isValid:= not isValid;
   288             isValid:= not isValid;
   286 
   289 
   287         if isValid then
   290         if isValid then
   288             begin
   291             begin
   289             parseParameter(cmd, arg, tmpInt);
   292             parseParameter(cmd, arg, tmpInt);
   290             newSyntax := newSyntax + cmd + ' ';
   293             newSyntax:= newSyntax + cmd + ' ';
   291             if not isBool then
   294             if not isBool then
   292                 newSyntax := newSyntax + arg + ' ';
   295                 newSyntax:= newSyntax + arg + ' ';
   293             end;
   296             end;
   294         inc(index);
   297         inc(index);
   295         end;
   298         end;
   296 
   299 
   297     WriteLn(stdout, 'Attempted to automatically convert to the new syntax:');
   300     WriteLn(stdout, 'Attempted to automatically convert to the new syntax:');
   338 {$IFNDEF HWLIBRARY}
   341 {$IFNDEF HWLIBRARY}
   339 procedure GetParams;
   342 procedure GetParams;
   340 begin
   343 begin
   341     isInternal:= (ParamStr(1) = '--internal');
   344     isInternal:= (ParamStr(1) = '--internal');
   342 
   345 
   343     UserPathPrefix := '.';
   346     UserPathPrefix := _S'.';
   344     PathPrefix     := cDefaultPathPrefix;
   347     PathPrefix     := cDefaultPathPrefix;
   345     recordFileName := '';
   348     recordFileName := '';
   346     parseCommandLine();
   349     parseCommandLine();
   347 
   350 
   348     if (isInternal) and (ParamCount<=1) then
   351     if (isInternal) and (ParamCount<=1) then