From 141166392c8d7b6fe9de291867c3ff9ee1bfd8c6 Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Fri, 5 Feb 2021 23:20:06 -0800 Subject: [PATCH] Add help to command line arguments --- downloader.pas | 55 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/downloader.pas b/downloader.pas index b72b7aa..23c4a99 100644 --- a/downloader.pas +++ b/downloader.pas @@ -1,6 +1,6 @@ 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 gameId: Integer; @@ -140,10 +140,55 @@ begin end; end; +procedure displayHelp; +const + optionFlags: TStringArray = ( + '-h, --help', + '-g, --gameid ', + '-u, --username ', + '-p, --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 app := TCustomApplication.Create(nil); - errorMsg := app.checkOptions('g:u:p:', 'gameid: username: password:'); - if (errorMsg = '') then + errorMsg := app.checkOptions('hg:u:p:', 'help gameid: username: password:'); + if (errorMsg <> '') or app.HasOption('h', 'help') then + begin + WriteLn(errorMsg); + displayHelp; + end + else begin if app.HasOption('g', 'gameid') then begin @@ -172,10 +217,6 @@ begin begin WriteLn('No game id was given'); end; - end - else - begin - WriteLn(errorMsg); end; end.