Jump to content
Arnaud Bouchez

New Command Line Parser in mORMot 2

Recommended Posts

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.
But not enough when you want to go any further.

 

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):

var
  verbose: boolean;
  threads: integer;
...
with Executable.Command do                                                        
begin                                                                             
  ExeDescription := 'An executable to test mORMot Execute.Command';               
  verbose := Option(['v', 'verbose'], 'generate verbose output');                 
  Get(['t', 'threads'], threads, '#number of threads to run', 5);                 
  ConsoleWrite(FullDescription);                                                  
end;                                                                              

This code will fill verbose and threads local variables from the command line (with some optional default value), and output on Linux:

An executable to test mORMot Execute.Command

Usage: mormot2tests [options] [params]

Options:
  -v, --verbose       generate verbose output

Params:
  -t, --threads <number> (default 5)
                      number of threads to run

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 available at

https://blog.synopse.info/?post/2023/04/19/New-Command-Line-Parser-in-mORMot-2

 

  • Thanks 1

Share this post


Link to post

Yellow,

 

Seems very nice that it'll also support sane syntax (for me) 🙂

"raw /servers=2 /threads=8 /nopin"

Instead of just  "/servers 2"

If also is OK?

/path="C:\Program Files\mORMotHyperServer\"

I really don't like kind of detached command line params. like "/password badpw ..."  way easier for me to read /password="badpw" etc.

 

-Tee-

 

Share this post


Link to post

Both syntax are of course supported.

 

This is explained in the blog article:

Quote

Note that both -t 10 and -t=10 syntax are accepted.

What is your exact concern?
Is it that you want the quotes to be supported too?

Such quotes are not cross-platform I guess. The parser don't read quotes, because they are in fact parsed at OS level.
IMHO the correct way is to write either
 

/path "C:\Program Files\mORMotHyperServer\"
or
"/path=C:\Program Files\mORMotHyperServer\"

But I did not test this.

Any feedback is welcome.

Edited by Arnaud Bouchez

Share this post


Link to post
4 hours ago, Tommi Prami said:

/path="C:\Program Files\mORMotHyperServer\"

This is non-standard. I guess the program will receive par1=/path="C:\Program and par2=Files\mORMotHyperServer\"

Seems to work actually

Edited by Fr0sT.Brutal

Share this post


Link to post
11 minutes ago, Arnaud Bouchez said:

Both syntax are of course supported.

 

This is explained in the blog article:

 

Tried to state that it is nice that both syntaxes are supported.

Share this post


Link to post
9 minutes ago, Fr0sT.Brutal said:

This is non-standard. I guess the program will receive par1=/path="C:\Program and par2=Files\mORMotHyperServer\"

I do not quite folllow.

Why that would be non standard. LEt me make complete example

myapp.exe /path="C:\Program Files\mORMotHyperServer\"

Why then the command line switch would/could not contain string: C:\Program Files\mORMotHyperServer\

(without quotes)

Forcing to wrap whole thing would be counter intuitive for me. (I think that syntax is supported by some apps).

Edited by Tommi Prami

Share this post


Link to post
18 minutes ago, Tommi Prami said:

Why then the command line switch would/could not contain string: C:\Program Files\mORMotHyperServer\

(without quotes)

Funny. At least Win7 seems to handle that correctly (launching [project1 /par="aa vv"] results in single argument [/par=aa vv]). However I'm not sure if this is stable and expected behavior.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×