Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-01

Freepascal CLI library

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Darius Blaszyk

I'm working on a CLI application that allows the user to do some configuration. During the process the user is presented with different questions that need to be answered, before the configuration is written to file. For convenience, the user is presented with default options as well. Something like this:

----------
Log files can be written to monitor the performance and debug the application

Please select whether you would like to write logs to file (y/n) [y]:

----------

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?

TIA!

Rgds, Darius

Reply by email

Unknown author
to Darius Blaszyk
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.*

Reply by email

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

No I do not look for commandline handling. Rather a CLI interaction library. Which allows to interact with the application during runtime. Similar to how sphinx-quickstart works for instance.

Rgds Darius

Reply by email

Unknown author
to Darius Blaszyk
Darius Blaszyk wrote 6 lines
> not specific to CLI apps but are you, perhaps, thinking of CheckOptions and HasOption? No I do not look for commandline handling. Rather a CLI interaction library. Which allows to interact with the application during runtime. Similar to how sphinx-quickstart works for instance.

oh, sorry... i misunderstood... i'm not aware of anything like that, really... i think most folks have a general YesNo routine that returns some value which they then use to determine which way the code flows... other commands might be validated by a CheckCMD routine which may then pass valid options off to a ServiceCMD routine which would then deal with getting the command handled...

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.*

Reply by email

Bart
to Unknown author
really... i think most folks have a general YesNo routine that returns some value which they then use to determine which way the code flows...

I used to have a general Prompt(Question: String; Allowed: TCharSet; Default: Char; CaseSensitive: Boolean): Char; function for that in the TP era.
(It used crt unit though. And TCharSet = set of Char)

Bart

Reply by email

Unknown author
to Bart
Bart wrote 9 lines
On Sun, Jan 14, 2018 at 6:00 PM, wrote: > really... i think most folks have a general YesNo routine that returns some > value which they then use to determine which way the code flows... I used to have a general Prompt(Question: String; Allowed: TCharSet; Default: Char; CaseSensitive: Boolean): Char; function for that in the TP era. (It used crt unit though. And TCharSet = set of Char)

that's another option and it can be altered to work in today's code, too...

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.*

Reply by email