* Added help output

* Added command line parsing
This commit is contained in:
Andreas Schneider 2015-09-29 15:20:36 +02:00
parent 5da2776f5d
commit c412d64727
4 changed files with 186 additions and 17 deletions

View File

@ -54,7 +54,8 @@ type
procedure CmdHighlight(AData: String);
procedure ProcessCall(AURL: String);
//Helper
procedure ListProfiles;
procedure WriteHelp;
procedure ListTemplates;
procedure WriteContent;
public
constructor Create; overload;
@ -64,7 +65,7 @@ type
implementation
uses
strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite;
strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite, vinfo;
type
TContentType = (ctOther, ctJSON, ctXML);
@ -90,17 +91,24 @@ var
data: TextFile;
commandMode: Boolean;
begin
templateFile := FTemplateDir + ParamStr(1) + '.rest';
if ParamCount <> 1 then
if HasOption('l', 'list') then
begin
Writeln('Usage: ', ExtractFileName(ParamStr(0)), ' <profile or file>');
Writeln;
ListProfiles;
Halt(0);
ListTemplates;
Terminate;
Exit;
end;
if FileExists(ParamStr(1)) then
if HasOption('h', 'help') or (ParamCount < 1) or
(ParamStr(ParamCount)[1] = '-') then
begin
WriteHelp;
Terminate;
Exit;
end;
templateFile := FTemplateDir + ParamStr(ParamCount) + '.rest';
if FileExists(ParamStr(ParamCount)) then
begin
AssignFile(data, ParamStr(1));
FTemplateName := ExtractFileName(ParamStr(1));
@ -110,11 +118,12 @@ begin
if FileExists(templateFile) then
begin
AssignFile(data, templateFile);
FTemplateName := ParamStr(1);
FTemplateName := ParamStr(ParamCount);
end else
begin
writeln('Template not found!');
Halt(1);
writeln('Template ', ParamStr(ParamCount), ' not found!');
Terminate;
Exit;
end;
Reset(data);
@ -345,11 +354,30 @@ begin
request.Free;
end;
procedure TRestemplateApplication.ListProfiles;
procedure TRestemplateApplication.WriteHelp;
begin
NormVideo;
TextColor(Green);
Writeln('restemplate ', VersionInfo.GetProductVersionString);
Writeln('Copyright (c) ', VersionInfo.GetCopyright(False));
NormVideo;
Writeln;
Writeln('Usage: ', ExtractFileName(ExeName), ' [options] <template>');
Writeln;
Writeln('Tempate can be either a filename or a known template.');
Writeln;
Writeln('Options:');
Writeln(' -l --list');
Writeln(' Print a list of known templates.');
Writeln(' -h --help');
Writeln(' Print this help screen.');
end;
procedure TRestemplateApplication.ListTemplates;
var
sr: TSearchRec;
begin
Writeln('Known profiles:');
Writeln('Known Templates:');
if FindFirst(FTemplateDir + '*.rest', faAnyFile, sr) = 0 then
begin
repeat

View File

@ -57,7 +57,7 @@
<FormatVersion Value="1"/>
</local>
</RunParams>
<Units Count="4">
<Units Count="5">
<Unit0>
<Filename Value="restemplate.pas"/>
<IsPartOfProject Value="True"/>
@ -74,6 +74,10 @@
<Filename Value="URestemplateApp.pas"/>
<IsPartOfProject Value="True"/>
</Unit3>
<Unit4>
<Filename Value="vinfo.pas"/>
<IsPartOfProject Value="True"/>
</Unit4>
</Units>
</ProjectOptions>
<CompilerOptions>

View File

@ -22,7 +22,7 @@ program restemplate;
{$mode objfpc}{$H+}
uses
URestemplateApp, UFilter, UCRTHelper;
URestemplateApp, UFilter, UCRTHelper, vinfo;
{$R *.res}

137
vinfo.pas Normal file
View File

@ -0,0 +1,137 @@
unit vinfo;
{$mode objfpc}
interface
uses
Classes, SysUtils, resource, versiontypes, versionresource;
type
TVersionPrecision = 1..4;
{ TVersionInfo }
TVersionInfo = class
private
FVersResource: TVersionResource;
function GetFixedInfo: TVersionFixedInfo;
function GetStringFileInfo: TVersionStringFileInfo;
function GetVarFileInfo: TVersionVarFileInfo;
public
constructor Create;
destructor Destroy; override;
procedure Load(Instance: THandle);
property FixedInfo: TVersionFixedInfo read GetFixedInfo;
property StringFileInfo: TVersionStringFileInfo read GetStringFileInfo;
property VarFileInfo: TVersionVarFileInfo read GetVarFileInfo;
//Helper functions
function GetProductVersionString(AMinPrecision: TVersionPrecision = 2): String;
function GetCopyright(ALong: Boolean = false): String;
procedure PrintStringFileInfo;
end;
var
VersionInfo: TVersionInfo;
implementation
{ TVersionInfo }
function TVersionInfo.GetFixedInfo: TVersionFixedInfo;
begin
Result := FVersResource.FixedInfo;
end;
function TVersionInfo.GetStringFileInfo: TVersionStringFileInfo;
begin
Result := FVersResource.StringFileInfo;
end;
function TVersionInfo.GetVarFileInfo: TVersionVarFileInfo;
begin
Result := FVersResource.VarFileInfo;
end;
constructor TVersionInfo.Create;
begin
inherited Create;
FVersResource := TVersionResource.Create;
end;
destructor TVersionInfo.Destroy;
begin
FVersResource.Free;
inherited Destroy;
end;
procedure TVersionInfo.Load(Instance: THandle);
var
Stream: TResourceStream;
begin
Stream := TResourceStream.CreateFromID(Instance, 1, PChar(RT_VERSION));
try
FVersResource.SetCustomRawDataStream(Stream);
// access some property to load from the stream
FVersResource.FixedInfo;
// clear the stream
FVersResource.SetCustomRawDataStream(nil);
finally
Stream.Free;
end;
end;
function TVersionInfo.GetProductVersionString(AMinPrecision: TVersionPrecision = 2): String;
var
productVersion: TFileProductVersion;
lastVersion, i: Integer;
begin
productVersion := FixedInfo.ProductVersion;
lastVersion := 3;
while (lastVersion >= AMinPrecision) and (productVersion[lastVersion] = 0) do
dec(lastVersion);
Result := '';
for i := 0 to lastVersion do
begin
Result := Result + IntToStr(productVersion[i]);
if i < lastVersion then
Result := Result + '.';
end;
end;
function TVersionInfo.GetCopyright(ALong: Boolean = False): String;
begin
Result := StringFileInfo[0]['LegalCopyright'];
if ALong then
Result := StringReplace(Result, '(c) ', 'Copyright ', []);
end;
procedure TVersionInfo.PrintStringFileInfo;
var
verStringInfo: TVersionStringFileInfo;
verStringTable: TVersionStringTable;
i, j: Integer;
begin
verStringInfo := GetStringFileInfo;
for i := 0 to verStringInfo.Count - 1 do
begin
writeln('Version String Info ', i + 1);
verStringTable := verStringInfo.Items[i];
for j := 0 to verStringTable.Count - 1 do
begin
writeln(' ', verStringTable.Keys[j], ' = ', verStringTable.ValuesByIndex[j]);
end;
end;
end;
initialization
VersionInfo := TVersionInfo.Create;
VersionInfo.Load(HINSTANCE);
finalization
VersionInfo.Free;
end.