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

Command line parser is able to parse process arguments. Arguments could be of two types, single character argument (i.g -c) or long text argument (i.g. –config-file) Process argument could be of the following type: Boolean - classic is -h to show process help, Integer - an integer value for example verbosity level –verbose 5, Floating point - a floating point numer –ratio=123.25, String - a text argument, classic configuration file path –config-file app.properties. Integer, Floating point and String option has a mandatory angument while Boolean option does not require an argument. Argument can be passed in two ways: –config-file=config/app.properties for example or –config-file config/app.properties the same example is valid for single character options for example -c=onfig/app.properties or -c onfig/app.properties. Single character option can be pass grouped together (i.g -xvz). Be carful if a single character argument needs an argument you have to pass it or adding = and the value (i.g. -xvzf=file_to_compress.tar.gz) or as next process argument (i.g -xvzf file_to_compress.tar.gz). No more than a single character option with a mandatory argument can be grouped.

Implementations§

source§

impl CommandLineParser

source

pub fn new(errors_list: Option<[&str; 14]>) -> Self

Associated funtion to create a CommandLineParser

  • errors_list - Optional list of errors (None means default Elnglish language)
§Examples
use rsclp::{CommandLineParser, CommandLineParserError, CMD_LINE_OPTION_ERROR_NUM};
 
fn main()  {
    let it_error_list: [&str; CMD_LINE_OPTION_ERROR_NUM] = [
       "l'opzione già esiste",
        "l'opzione a singolo charattere già esiste",
        "l'opzione a formato lungo già esiste",
        "non è valorizzata",
        "non è del tipo richiesto",
        "opzione non trovata",
        "opzione di linea di comando non definita",
        "valore booleano non valido per questo tipo di opzione",
        "valore intero non valido per questo tipo di opzione",
        "valore a virgola mobile non valido per questo tipo di opzione",
        "manca l'argomento per questa opzione", 
        "opzione obbligatoria non valorizzata",
        "identificativo dell'opzione non trovato",
        "argomento dell'opzione fia assegnato"
    ];
    let mut clp = CommandLineParser::new(Some(it_error_list));
     
     
}
source

pub fn add_short_boolean_option( &mut self, short_form_option: char, mandatory: bool, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a boolean command line option identified by a single character only

  • short_form_option - single character option flag
  • mandatory - boolean value to set if command line option is mandatory or not
  • help_text - command line option halt text
source

pub fn add_long_boolean_option( &mut self, long_form_option: &str, mandatory: bool, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a boolean command line option identified by a long text only

  • long_form_option - long text option flag
  • mandatory - boolean value to set if command line option is mandatory or not
  • help_text - command line option halt text
source

pub fn add_boolean_option( &mut self, short_form_option: char, long_form_option: &str, mandatory: bool, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a boolean command line option identified by a single character and long form text

  • short_form_option - single character option flag
  • long_form_option - long text option flag
  • mandatory - boolean value to set if command line option is mandatory or not
  • help_text - command line option halt text
source

pub fn add_short_integer_option( &mut self, short_form_option: char, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add an integer command line option identified by a single character only

  • short_form_option - single character option flag
  • mandatory - boolean value to set if command line option is mandatory or not
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_long_integer_option( &mut self, long_form_option: &str, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add an integer command line option identified by a long text only

  • long_form_option - long text option flag
  • mandatory - boolean value to set if command line option is mandatory or not
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_integer_option( &mut self, short_form_option: char, long_form_option: &str, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add an integer command line option identified by a single character and long form text

  • short_form_option - single character option flag
  • long_form_option - long text option flag
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_short_fpoint_option( &mut self, short_form_option: char, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a floating point command line option identified by a single character only

  • short_form_option - single character option flag
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_long_fpoint_option( &mut self, long_form_option: &str, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a floating point command line option identified by a long text only

  • long_form_option - long text option flag
  • mandatory - boolean value to set if command line option is mandatory or not
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_fpoint_option( &mut self, short_form_option: char, long_form_option: &str, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a floating point command line option identified by a single character and long form text

  • short_form_option - single character option flag
  • long_form_option - long text option flag
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_short_string_option( &mut self, short_form_option: char, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a text command line option identified by a single character only

  • short_form_option - single character option flag
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_long_string_option( &mut self, long_form_option: &str, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a text command line option identified by a long text only

  • long_form_option - long text option flag
  • mandatory - boolean value to set if command line option is mandatory or not
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_string_option( &mut self, short_form_option: char, long_form_option: &str, mandatory: bool, arg_text: &str, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add a text command line option identified by a single character and long form text

  • short_form_option - single character option flag
  • long_form_option - long text option flag
  • arg_text - command line option argument text to explain argument in help text
  • help_text - command line option halt text
source

pub fn add_help_option( &mut self, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add the classic help command line option -h & –help

  • help_text - command line option halt text
source

pub fn add_version_option( &mut self, help_text: &str ) -> StdResult<u64, CommandLineParserError>

Method to add the classic version command line option -v & –version

  • help_text - command line option halt text
source

pub fn is_set(&self, option_hash: &u64) -> bool

Method to check if the parser found and set a command line option during command line parsing phase

  • option_hash - command line option identifier returned by an add_* method
source

pub fn get_value<T: FromStr + 'static>( &self, option_hash: &u64 ) -> StdResult<T, CommandLineParserError>

Generic method to get the value of a command line option (the first encountered) during command line parsing phase if command line option is not of the required type &ltT&gt or is not set error is returned

  • option_hash - command line option identifier returned by an add_* method
source

pub fn get_values<T: FromStr>(&self, option_hash: &u64) -> Option<Vec<T>>

Generic method to get all values of a command line option during command line parsing phase i.g if the option is -f/–input-file &ltfile name&gt input file to be merged and the command line is -f file1.txt -o output.txt –input-file=file2.txt get_value returns a vector containing file1.txt & file2.txt if command line option is not of the required type &ltT&gt or is not set error is returned

  • option_hash - command line option identifier returned by an add_* method
source

pub fn get_help_text(&self) -> String

Method to retrive the global command line help text

source

pub fn show_help(&self)

Method to show on standard output the global command line help text

source

pub fn show_help_on(&self, writer: &mut dyn Write) -> IOResult<()>

Method to show on object that implements the std::io::Write trait the global command line help text

  • writer - Write trait to show help text Writer::write_all is used
source

pub fn get_remaining_args(&self) -> &Vec<String>

Method to retrieve all arguments not related to an option

source

pub fn get_positional_args(&self) -> &Vec<String>

Method to retrieve all positionale arguments if ParsingMode is DefaultParsingMode the returned vector is empty

source

pub fn set_parsing_mode(&mut self, parsing_mode: ParsingMode)

Method to set parsing mode

  • parsing_mode - command line parser parsing mode
source

pub fn parse_args_os( &mut self, args_os: ArgsOs ) -> StdResult<(), CommandLineParserError>

Method to parse arguments of a process,

  • args_os - an iterator over the arguments of a process, yielding an OsString value for each argument.
source

pub fn parse_args( &mut self, args: Args ) -> StdResult<(), CommandLineParserError>

Method to parse arguments of a process,

  • args - an iterator over the arguments of a process, yielding a String value for each argument.
source

pub fn process(&mut self)

Method to process arguments of a process. As opposed to parse_args method, process controls the returned value of the parse_args method and in case of error it shows the error and the help text on standar error

source

pub fn process_os(&mut self)

Method to process arguments of a process. Performs the same functionality as the process method but using ArgOs

source

pub fn get_max_flags_len(&self) -> usize

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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.