pub struct CommandLineParser { /* private fields */ }
Expand description

Designed for command line parsing

The sample below demonstrates how to use CommandLineParser:

   CommandLineParser parser(argc, argv, keys);
   parser.about("Application name v1.0.0");
 
   if (parser.has("help"))
   {
       parser.printMessage();
       return 0;
   }
 
   int N = parser.get<int>("N");
   double fps = parser.get<double>("fps");
   String path = parser.get<String>("path");
 
   use_time_stamp = parser.has("timestamp");
 
   String img1 = parser.get<String>(0);
   String img2 = parser.get<String>(1);
 
   int repeat = parser.get<int>(2);
 
   if (!parser.check())
   {
       parser.printErrors();
       return 0;
   }

Keys syntax

The keys parameter is a string containing several blocks, each one is enclosed in curly braces and describes one argument. Each argument contains three parts separated by the | symbol:

-# argument names is a space-separated list of option synonyms (to mark argument as positional, prefix it with the @ symbol) -# default value will be used if the argument was not provided (can be empty) -# help message (can be empty)

For example:

   const String keys =
       "{help h usage ? |      | print this message   }"
       "{@image1        |      | image1 for compare   }"
       "{@image2        |<none>| image2 for compare   }"
       "{@repeat        |1     | number               }"
       "{path           |.     | path to file         }"
       "{fps            | -1.0 | fps for output video }"
       "{N count        |100   | count of objects     }"
       "{ts timestamp   |      | use time stamp       }"
       ;
}

Note that there are no default values for help and timestamp so we can check their presence using the has() method. Arguments with default values are considered to be always present. Use the get() method in these cases to check their actual value instead.

String keys like get<String>("@image1") return the empty string "" by default - even with an empty default value. Use the special <none> default value to enforce that the returned string must not be empty. (like in get<String>("@image2"))

Usage

For the described keys:

   $ ./app -N=200 1.png 2.jpg 19 -ts
 
   $ ./app -fps=aaa
   ERRORS:
   Parameter "fps": can not convert: [aaa] to [double]

Implementations

Constructor

Initializes command line parser object

Parameters
  • argc: number of command line arguments (from main())
  • argv: array of command line arguments (from main())
  • keys: string describing acceptable command line parameters (see class description for syntax)

Copy constructor

Trait Implementations

Wrap the specified raw pointer Read more
Return an the underlying raw pointer while consuming this wrapper. Read more
Return the underlying raw pointer. Read more
Return the underlying mutable raw pointer Read more
Set the about message Read more
Returns application path Read more
Check if field was provided in the command line Read more
Check for parsing errors Read more
Print help message Read more
Print list of errors occurred Read more
Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.