[fpc-pascal] Freepascal CLI library

wkitty42 at windstream.net wkitty42 at windstream.net
Sun Jan 14 00:04:25 CET 2018


On 01/13/2018 04:53 PM, Darius Blaszyk wrote:
> Is there a library available that can handle this kind of input from the
> command line? Though not difficult to write something from scratch (I already
> started on it), I feel like this has to be available already. Anyone has a
> tip for me?

not specific to CLI apps but are you, perhaps, thinking of CheckOptions and 
HasOption?

i have a CLI app that is basically a CustApp... CheckOptions allows to check 
command line options for short and long variations...

   eg: -h or --help

then options may or may not have parameters... if they have a parameter, they 
must always have a parameter...

so basically, there's tMyApplication.DoRun... inside there, i check for command 
line options via CheckOptions like the following snippet... it checks for 
options such as

   appname -?
   appname -h
   appname --help

the above are all for displaying command line help...

   appname -v
   appname --version

these two display the program version information...

   appname --diag

this is the only option for diagnostic information...

   appname -i <filemask>
   appname --input <filemask>

these two are for input filemask... you probably don't want to worry about the 
AppName part... i had to do that for something special in this program... i thin 
kto try to have the log file default to the application name unless otherwise 
specified with another command line option... i've trimmed things in this quick 
snippet...


// TMyApplication
procedure TMyApplication.DoRun;
var
   ErrorMsg: String;
   foo: string;
begin
   // quick check parameters
   ErrorMsg:=CheckOptions('?hvi:','help version diag input:');
   if ErrorMsg<>'' then begin
     writeln('Parameter Error: ',ErrorMsg);
     Terminate;
     Exit;
   end;

//  AppName := 
StringReplace(ExtractFileName(ExeName),ExtractFileExt(ExeName),'',[]);
   foo := paramstr(0);
   AppName := StringReplace(ExtractFileName(foo),ExtractFileExt(foo),'',[]);
   // parse parameters
   if HasOption('?') or HasOption('h','help') then begin
     WriteHelp;
     Terminate;
     Exit;
   end;
   if HasOption('v','version') then begin
     WriteVersion;
     Terminate;
     Exit;
   end;

   ModeDIAG         := HasOption('diag');              // diagnostic mode, show 
config and quit
   if HasOption('i','input') then                      // input files mask
     InFile := GetOptionValue('i','input')
   else
     InFile := '*.txt';
[... blah blah blah ...]



-- 
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list unless*
        *a signed and pre-paid contract is in effect with us.*



More information about the fpc-pascal mailing list