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:

   # Good call (3 positional parameters: image1, image2 and repeat; N is 200, ts is true)
   $ ./app -N=200 1.png 2.jpg 19 -ts
 
   # Bad call
   $ ./app -fps=aaa
   ERRORS:
   Parameter "fps": can not convert: [aaa] to [double]

Implementations§

source§

impl CommandLineParser

source

pub fn new(argc: i32, argv: &[&str], keys: &str) -> Result<CommandLineParser>

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

pub fn copy(parser: &CommandLineParser) -> Result<CommandLineParser>

Copy constructor

Trait Implementations§

source§

impl Boxed for CommandLineParser

source§

unsafe fn from_raw(ptr: *mut c_void) -> Self

Wrap the specified raw pointer Read more
source§

fn into_raw(self) -> *mut c_void

Return an the underlying raw pointer while consuming this wrapper. Read more
source§

fn as_raw(&self) -> *const c_void

Return the underlying raw pointer. Read more
source§

fn as_raw_mut(&mut self) -> *mut c_void

Return the underlying mutable raw pointer Read more
source§

impl CommandLineParserTrait for CommandLineParser

source§

fn as_raw_mut_CommandLineParser(&mut self) -> *mut c_void

source§

fn set(&mut self, parser: &CommandLineParser) -> Result<()>

Assignment operator
source§

fn about(&mut self, message: &str) -> Result<()>

Set the about message Read more
source§

impl CommandLineParserTraitConst for CommandLineParser

source§

fn as_raw_CommandLineParser(&self) -> *const c_void

source§

fn get_path_to_application(&self) -> Result<String>

Returns application path Read more
source§

fn get_bool(&self, name: &str, space_delete: bool) -> Result<bool>

Access arguments by name Read more
source§

fn get_bool_def(&self, name: &str) -> Result<bool>

Access arguments by name Read more
source§

fn get_i32(&self, name: &str, space_delete: bool) -> Result<i32>

Access arguments by name Read more
source§

fn get_i32_def(&self, name: &str) -> Result<i32>

Access arguments by name Read more
source§

fn get_f64(&self, name: &str, space_delete: bool) -> Result<f64>

Access arguments by name Read more
source§

fn get_f64_def(&self, name: &str) -> Result<f64>

Access arguments by name Read more
source§

fn get_str(&self, name: &str, space_delete: bool) -> Result<String>

Access arguments by name Read more
source§

fn get_str_def(&self, name: &str) -> Result<String>

Access arguments by name Read more
source§

fn get_u64(&self, name: &str, space_delete: bool) -> Result<u64>

Access arguments by name Read more
source§

fn get_u64_def(&self, name: &str) -> Result<u64>

Access arguments by name Read more
source§

fn get_bool_idx(&self, index: i32, space_delete: bool) -> Result<bool>

Access positional arguments by index Read more
source§

fn get_bool_idx_def(&self, index: i32) -> Result<bool>

Access positional arguments by index Read more
source§

fn get_i32_idx(&self, index: i32, space_delete: bool) -> Result<i32>

Access positional arguments by index Read more
source§

fn get_i32_idx_def(&self, index: i32) -> Result<i32>

Access positional arguments by index Read more
source§

fn get_f64_idx(&self, index: i32, space_delete: bool) -> Result<f64>

Access positional arguments by index Read more
source§

fn get_f64_idx_def(&self, index: i32) -> Result<f64>

Access positional arguments by index Read more
source§

fn get_str_idx(&self, index: i32, space_delete: bool) -> Result<String>

Access positional arguments by index Read more
source§

fn get_str_idx_def(&self, index: i32) -> Result<String>

Access positional arguments by index Read more
source§

fn get_u64_idx(&self, index: i32, space_delete: bool) -> Result<u64>

Access positional arguments by index Read more
source§

fn get_u64_idx_def(&self, index: i32) -> Result<u64>

Access positional arguments by index Read more
source§

fn has(&self, name: &str) -> Result<bool>

Check if field was provided in the command line Read more
source§

fn check(&self) -> Result<bool>

Check for parsing errors Read more
source§

fn print_message(&self) -> Result<()>

Print help message Read more
source§

fn print_errors(&self) -> Result<()>

Print list of errors occurred Read more
source§

impl Debug for CommandLineParser

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for CommandLineParser

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for CommandLineParser

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.