Add better support for command line arguments

Use -g or --gameid to set game id
Use -u or --username to set username
Use -p or --password to set password
This commit is contained in:
Matthew Welch 2021-02-05 20:13:48 -08:00
parent 67bf8f70f2
commit 6e8e993aba

View File

@ -1,13 +1,13 @@
program downloader; program downloader;
uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar; uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar, CustApp;
var var
authString: String;
gameId: Integer; gameId: Integer;
gameData: TJSONData; gameData: TJSONData;
client: TFPHTTPClient; client: TFPHTTPClient;
tarFile: String; authString, tarFile, errorMsg: String;
app: TCustomApplication;
procedure createFileDir(filePath: AnsiString); procedure createFileDir(filePath: AnsiString);
var var
@ -111,10 +111,24 @@ var
username, password: String; username, password: String;
begin begin
Write('Enter username: '); if app.HasOption('u', 'user') then
ReadLn(username); begin
Write('Enter password: '); username := app.GetOptionValue('u', 'username');
ReadLn(password); end
else
begin
Write('Enter username: ');
ReadLn(username);
end;
if app.HasOption('p', 'password') then
begin
password := app.GetOptionValue('p', 'password');
end
else
begin
Write('Enter password: ');
ReadLn(password);
end;
authString := username + ':' + password; authString := username + ':' + password;
client.AddHeader('Authorization', 'Basic '+base64.EncodeStringBase64(authString)); client.AddHeader('Authorization', 'Basic '+base64.EncodeStringBase64(authString));
client.AllowRedirect := true; client.AllowRedirect := true;
@ -127,32 +141,41 @@ begin
end; end;
begin begin
if ParamCount > 0 then app := TCustomApplication.Create(nil);
errorMsg := app.checkOptions('g:u:p:', 'gameid: username: password:');
if (errorMsg = '') then
begin begin
gameId := StrToInt(ParamStr(1)); if app.HasOption('g', 'gameid') then
client := TFPHTTPClient.Create(nil);
if authenticate(client) then
begin begin
if getGameData(client, gameId, gameData) then gameId := StrToInt(app.GetOptionValue('g', 'gameid'));
client := TFPHTTPClient.Create(nil);
if authenticate(client) then
begin begin
tarFile := downloadGameTar(client, gameId); if getGameData(client, gameId, gameData) then
extractTar(tarFile); begin
DeleteFile(tarFile); tarFile := downloadGameTar(client, gameId);
WriteLn('Download finished'); extractTar(tarFile);
DeleteFile(tarFile);
WriteLn('Download finished');
end
else
begin
WriteLn('Invalid game id given');
end;
end end
else else
begin begin
WriteLn('Invalid game id given'); WriteLn('Invalid username or password');
end; end;
end end
else else
begin begin
WriteLn('Invalid username or password'); WriteLn('No game id was given');
end; end;
end end
else else
begin begin
WriteLn('No game id was given'); WriteLn(errorMsg);
end; end;
end. end.