Skip to content

Commit

Permalink
Added demo for TJPCmdLineParser
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdp committed Jun 19, 2022
1 parent b80fa7f commit 9458e46
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 0 deletions.
138 changes: 138 additions & 0 deletions demos/CmdLineParser/Lazarus/src/CmdLineParserDemo.lpi
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<Title Value="CmdLineParserDemo"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes>
<Item Name="Debug Win32" Default="True"/>
<Item Name="Release Win32">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="..\CmdLineParserDemo32"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="..\lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
<TargetCPU Value="i386"/>
<TargetOS Value="win32"/>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="False"/>
<DebugInfoType Value="dsDwarf3"/>
<UseExternalDbgSyms Value="True"/>
</Debugging>
</Linking>
</CompilerOptions>
</Item>
<Item Name="Release Win64">
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="..\CmdLineParserDemo64"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="..\lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
<SmartLinkUnit Value="True"/>
<TargetCPU Value="x86_64"/>
<TargetOS Value="win64"/>
<Optimizations>
<OptimizationLevel Value="3"/>
</Optimizations>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="False"/>
</Debugging>
<LinkSmart Value="True"/>
</Linking>
</CompilerOptions>
</Item>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<RequiredPackages>
<Item>
<PackageName Value="jplib"/>
</Item>
</RequiredPackages>
<Units>
<Unit>
<Filename Value="CmdLineParserDemo.lpr"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="..\CmdLineParserDemo32_debug"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="..\lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<IncludeAssertionCode Value="True"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
<StackChecks Value="True"/>
</Checks>
<VerifyObjMethodCallValidity Value="True"/>
<TargetCPU Value="i386"/>
<TargetOS Value="win32"/>
</CodeGeneration>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
<UseHeaptrc Value="True"/>
<TrashVariables Value="True"/>
<UseExternalDbgSyms Value="True"/>
</Debugging>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions>
<Item>
<Name Value="EAbort"/>
</Item>
<Item>
<Name Value="ECodetoolError"/>
</Item>
<Item>
<Name Value="EFOpenError"/>
</Item>
</Exceptions>
</Debugging>
</CONFIG>
141 changes: 141 additions & 0 deletions demos/CmdLineParser/Lazarus/src/CmdLineParserDemo.lpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
program CmdLineParserDemo;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
JPL.CmdLineParser;


const
ENDL = sLineBreak;
APP_VERSION_STR = '1.0';

var
Cmd: TJPCmdLineParser;


procedure RegisterOptions;
var
Category: string;
begin

{$IFDEF MSWINDOWS} Cmd.CommandLineParsingMode := cpmCustom; {$ELSE} Cmd.CommandLineParsingMode := cpmDelphi; {$ENDIF}
Cmd.UsageFormat := cufWget;


Category := 'main';

// Short and long option. Value required.
Cmd.RegisterOption('i', 'in-file', cvtRequired, False, False, 'Input file (value required).', 'FILE', Category);

// Short and long option. Value optional.
Cmd.RegisterOption('o', 'out-file', cvtOptional, False, False, 'Output file (value optional).', 'FILE', Category);

// Short and long option. No value.
Cmd.RegisterOption('a', 'Option-A', cvtNone, False, False, 'This is an --Option-A description.', '', Category);

// Short option.
Cmd.RegisterShortOption('b', cvtNone, False, False, 'This is an option -b description.', '', Category);

// Long option.
Cmd.RegisterLongOption('Option-C', cvtNone, False, False, 'This is an --Option-C description.', '', Category);


Category := 'info';
Cmd.RegisterOption('h', 'help', cvtNone, False, False, 'Show help and exit.', '', Category);
Cmd.RegisterOption('V', 'version', cvtNone, False, False, 'Show application version and exit.', '', Category);

// Hidden option.
Cmd.RegisterShortOption('?', cvtNone, False, True, '', '', Category);

end;

procedure DisplayUsage;
var
s: string;
begin
s :=
'JPL.CmdLineParser demo application' + ENDL + ENDL +
'Main options: ' + ENDL + Cmd.OptionsUsageStr(' ', 'main', 120, ' ', 30) + ENDL +
'Information: ' + ENDL + Cmd.OptionsUsageStr(' ', 'info', 120, ' ', 30);
Writeln(s);
end;

procedure ProcessOptions;
begin
if (Cmd.IsOptionExists('h')) or (Cmd.IsOptionExists('?')) then
begin
DisplayUsage;
Exit;
end;

if Cmd.IsOptionExists('V') then
begin
Writeln('Version: ', APP_VERSION_STR);
Exit;
end;

if Cmd.IsOptionExists('i') then Writeln('Input file: ', Cmd.GetOptionValue('i'))
else Writeln ('Input file: NOT SPECIFIED');

if Cmd.IsOptionExists('o') then Writeln('Output file: ', Cmd.GetOptionValue('o'))
else Writeln('Output file: NOT SPECIFIED');

if Cmd.IsOptionExists('a') then Writeln('Option -a: is set') else Writeln('Option -a: NOT SET');
if Cmd.IsShortOptionExists('b') then Writeln('Option -b: is set') else Writeln('Option -b: NOT SET');
if Cmd.IsLongOptionExists('Option-C') then Writeln('Option --Option-C: is set') else Writeln('Option --Option-C: NOT SET');
end;



begin

{$IFDEF FPC}
{$IF DECLARED(UseHeapTrace)}
GlobalSkipIfNoLeaks := True; // supported as of debugger version 3.2.0
{$ENDIF}
{$ENDIF}

try

Cmd := TJPCmdLineParser.Create;
try

RegisterOptions;

if ParamCount = 0 then
begin
DisplayUsage;
Exit;
end;

Cmd.Parse;

if Cmd.ErrorCount > 0 then
begin
Writeln('An error occured while parsing command-line options: ', Cmd.ErrorsStr);
ExitCode := 1;
Exit;
end;

ProcessOptions;


// Do something ...


finally
Cmd.Free;
end;

except
on E: Exception do
Writeln(E.Message);
end;

end.

Loading

0 comments on commit 9458e46

Please sign in to comment.