Add help to command line arguments

This commit is contained in:
Matthew Welch 2021-02-05 23:20:06 -08:00
parent 6e8e993aba
commit 141166392c

View File

@ -1,6 +1,6 @@
program downloader; program downloader;
uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar, CustApp; uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar, CustApp, Math;
var var
gameId: Integer; gameId: Integer;
@ -140,10 +140,55 @@ begin
end; end;
end; end;
procedure displayHelp;
const
optionFlags: TStringArray = (
'-h, --help',
'-g, --gameid <game id>',
'-u, --username <username>',
'-p, --password <password>');
optionDescriptions: TStringArray = (
'Display help.',
'Sets the game id. This is required.',
'Sets the username for authentication.',
'Sets the password for authentication.');
var
i, lenFlag, lenDesc, maxLenFlag, maxLenDesc, minLenFlag, minLenDesc: Integer;
begin
maxLenFlag := 0;
maxLenDesc := 0;
minLenFlag := 1000;
minLenDesc := 1000;
for i := 0 to Length(optionFlags)-1 do
begin
minLenFlag := Min(minLenFlag, Length(optionFlags[i]));
maxLenFlag := Max(maxLenFlag, Length(optionFlags[i]));
minLenDesc := Min(minLenDesc, Length(optionDescriptions[i]));
maxLenDesc := Max(maxLenDesc, Length(optionDescriptions[i]));
end;
WriteLn('Usage: downloader.exe [options]');
WriteLn;
WriteLn('Options:');
for i := 0 to Length(optionFLags)-1 do
begin
lenFlag := Length(optionFLags[i]);
lenDesc := Length(optionDescriptions[i]);
Write(optionFlags[i]:(lenFlag+2));
WriteLn(optionDescriptions[i]:(lenDesc+maxLenFlag-lenFlag+2))
end;
end;
begin begin
app := TCustomApplication.Create(nil); app := TCustomApplication.Create(nil);
errorMsg := app.checkOptions('g:u:p:', 'gameid: username: password:'); errorMsg := app.checkOptions('hg:u:p:', 'help gameid: username: password:');
if (errorMsg = '') then if (errorMsg <> '') or app.HasOption('h', 'help') then
begin
WriteLn(errorMsg);
displayHelp;
end
else
begin begin
if app.HasOption('g', 'gameid') then if app.HasOption('g', 'gameid') then
begin begin
@ -172,10 +217,6 @@ begin
begin begin
WriteLn('No game id was given'); WriteLn('No game id was given');
end; end;
end
else
begin
WriteLn(errorMsg);
end; end;
end. end.