Recent

Author Topic: New Command Line Parser in mORMot 2  (Read 1092 times)

abouchez

  • Full Member
  • ***
  • Posts: 124
    • Synopse
New Command Line Parser in mORMot 2
« on: April 19, 2023, 01:47:53 pm »
For most projects, we want to be able to pass some custom values when starting it.
We have ParamStr and ParamCount global functions, enough to retrieve the basic information.
On Lazarus, we got some TCustomApplication methods... one step further...
But still not enough when you want to have simple and maintainable code.

We just committed a new command line parser to our Open Source mORMot 2 framework, which works on both Delphi and FPC, follows both Windows not POSIX/Linux conventions, and has much more features (like automated generation of the help message), in an innovative and easy workflow.

The most simple code may be the following (extracted from the documentation):
Code: Pascal  [Select][+][-]
  1. var
  2.   verbose: boolean;
  3.   threads: integer;
  4. ...
  5. with Executable.Command do                                                        
  6. begin                                                                            
  7.   ExeDescription := 'An executable to test mORMot Execute.Command';              
  8.   verbose := Option(['v', 'verbose'], 'generate verbose output');                
  9.   Get(['t', 'threads'], threads, '#number of threads to run', 5);                
  10.   ConsoleWrite(FullDescription);                                                  
  11. end;                                                                              
  12.  
This code will fill verbose and threads local variables from the command line (with some optional default value), and output on Linux:

Code: Text  [Select][+][-]
  1. An executable to test mORMot Execute.Command
  2. Usage: mormot2tests [options] [params]
  3.  
  4. Options:
  5.   -v, --verbose       generate verbose output
  6.  
  7. Params:
  8.   -t, --threads <number> (default 5)
  9.                       number of threads to run
  10.  
So, not only you can parse the command line and retrieve values, but you can also add some description text, and let generate an accurate help message when needed.

More information and source samples are available at
https://blog.synopse.info/?post/2023/04/19/New-Command-Line-Parser-in-mORMot-2
 ;D

Okoba

  • Hero Member
  • *****
  • Posts: 612
Re: New Command Line Parser in mORMot 2
« Reply #1 on: April 25, 2023, 10:19:48 am »
That's a handy tool and surely I will use it.
Thank you ab.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12032
  • FPC developer.
Re: New Command Line Parser in mORMot 2
« Reply #2 on: April 25, 2023, 12:02:48 pm »
There is also getopts. Doesn't do the help though (good examples are e.g. chmls )

abouchez

  • Full Member
  • ***
  • Posts: 124
    • Synopse
Re: New Command Line Parser in mORMot 2
« Reply #3 on: April 25, 2023, 03:56:44 pm »
There is also getopts. Doesn't do the help though (good examples are e.g. chmls )
I did not saw this getopts unit. It is indeed used by most compiler tools.  8)
More complete than the others, but still lacking direct support of non-switch parameters (so there is the cryptic buildarglist() procedure in chmls.dpr), and no help generation.
It is much more verbose that our new unit, especially for the long options support.
And we wanted something working on both FPC and Delphi. ;)

 

TinyPortal © 2005-2018