During the last weeks it happened often that I had to write some command line apps. The reason for this is that more and more things are running automatically in tools like FinalBuilder (I will write an article about this later).
One thing that makes the command line app life easier is the encapsulation of the switches in a record (Current Delphi versions allow records with methods).
type
TSwitches = record
strict private const
cHelpSwitch = '?';
cBackupCountSwitch = 'c';
public
class function BackupCount: Integer; static;
class function Help: Boolean; static;
class procedure PrintHelp; static;
end;
...
class function TSwitches.BackupCount: Integer;
var
sCount: string;
begin
FindCmdLineSwitch(cBackupCountSwitch, sCount);
TryStrToInt(sCount, Result);
end;
class function TSwitches.Help: Boolean;
var
sHelp: string;
begin
Result := (ParamCount = 0) or (FindCmdLineSwitch(cHelpSwitch, sHelp));
end;
class procedure TSwitches.PrintHelp;
begin
Writeln(Format(SUsageSample, [TDBSwitches.cBackupFolderSwitch, TDBSwitches.cBackupCountSwitch]));
Writeln;
Writeln(SOptions);
Writeln;
Writeln(Format(SHelpSwitch, [TDBSwitches.cHelpSwitch]));
Writeln(Format(SBackupCountSwitch, [TDBSwitches.cBackupCountSwitch]));
end;
This record can be used directly in the .dpr file.
begin
if TDBSwitches.Help then
TDBSwitches.PrintHelp
else
TDBWork.DeleteBackup(TDBSwitches.BackupFolder, TDBSwitches.BackupCount);
end.



