pub trait CommandLineParserTraitConst {
    fn as_raw_CommandLineParser(&self) -> *const c_void;

    fn get_path_to_application(&self) -> Result<String> { ... }
    fn has(&self, name: &str) -> Result<bool> { ... }
    fn check(&self) -> Result<bool> { ... }
    fn print_message(&self) -> Result<()> { ... }
    fn print_errors(&self) -> Result<()> { ... }
}
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]

Required Methods

Provided Methods

Returns application path

This method returns the path to the executable from the command line (argv[0]).

For example, if the application has been started with such a command:

$ ./bin/my-executable

this method will return ./bin.

Check if field was provided in the command line

Parameters
  • name: argument name to check

Check for parsing errors

Returns false if error occurred while accessing the parameters (bad conversion, missing arguments, etc.). Call @ref printErrors to print error messages list.

Print help message

This method will print standard help message containing the about message and arguments description.

See also

about

Print list of errors occurred

See also

check

Implementors