[−][src]Trait opencv::core::CommandLineParserTrait
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
fn as_raw_CommandLineParser(&self) -> *const c_void
[src]
fn as_raw_mut_CommandLineParser(&mut self) -> *mut c_void
[src]
Provided methods
fn get_path_to_application(&self) -> Result<String>
[src]
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
.
fn has(&self, name: &str) -> Result<bool>
[src]
fn check(&self) -> Result<bool>
[src]
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.
fn about(&mut self, message: &str) -> Result<()>
[src]
Set the about message
The about message will be shown when @ref printMessage is called, right before arguments table.
fn print_message(&self) -> Result<()>
[src]
Print help message
This method will print standard help message containing the about message and arguments description.
See also
about