Struct rsclp::CommandLineParser
source · 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
impl CommandLineParser
sourcepub fn new(errors_list: Option<[&str; 14]>) -> Self
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));
}sourcepub fn add_short_boolean_option(
&mut self,
short_form_option: char,
mandatory: bool,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagmandatory- boolean value to set if command line option is mandatory or nothelp_text- command line option halt text
sourcepub fn add_long_boolean_option(
&mut self,
long_form_option: &str,
mandatory: bool,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagmandatory- boolean value to set if command line option is mandatory or nothelp_text- command line option halt text
sourcepub fn add_boolean_option(
&mut self,
short_form_option: char,
long_form_option: &str,
mandatory: bool,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flaglong_form_option- long text option flagmandatory- boolean value to set if command line option is mandatory or nothelp_text- command line option halt text
sourcepub fn add_short_integer_option(
&mut self,
short_form_option: char,
mandatory: bool,
arg_text: &str,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagmandatory- boolean value to set if command line option is mandatory or notarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub fn add_long_integer_option(
&mut self,
long_form_option: &str,
mandatory: bool,
arg_text: &str,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagmandatory- boolean value to set if command line option is mandatory or notarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub 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>
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 flaglong_form_option- long text option flagarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub fn add_short_fpoint_option(
&mut self,
short_form_option: char,
mandatory: bool,
arg_text: &str,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub fn add_long_fpoint_option(
&mut self,
long_form_option: &str,
mandatory: bool,
arg_text: &str,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagmandatory- boolean value to set if command line option is mandatory or notarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub 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>
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 flaglong_form_option- long text option flagarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub fn add_short_string_option(
&mut self,
short_form_option: char,
mandatory: bool,
arg_text: &str,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub fn add_long_string_option(
&mut self,
long_form_option: &str,
mandatory: bool,
arg_text: &str,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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 flagmandatory- boolean value to set if command line option is mandatory or notarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub 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>
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 flaglong_form_option- long text option flagarg_text- command line option argument text to explain argument in help texthelp_text- command line option halt text
sourcepub fn add_help_option(
&mut self,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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
sourcepub fn add_version_option(
&mut self,
help_text: &str
) -> StdResult<u64, CommandLineParserError>
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
sourcepub fn is_set(&self, option_hash: &u64) -> bool
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
sourcepub fn get_value<T: FromStr + 'static>(
&self,
option_hash: &u64
) -> StdResult<T, CommandLineParserError>
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 <T> or is not set error is returned
option_hash- command line option identifier returned by an add_* method
sourcepub fn get_values<T: FromStr>(&self, option_hash: &u64) -> Option<Vec<T>>
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 <file name> 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 <T> or is not set error is returned
option_hash- command line option identifier returned by an add_* method
sourcepub fn get_help_text(&self) -> String
pub fn get_help_text(&self) -> String
Method to retrive the global command line help text
sourcepub fn show_help_on(&self, writer: &mut dyn Write) -> IOResult<()>
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
sourcepub fn get_remaining_args(&self) -> &Vec<String>
pub fn get_remaining_args(&self) -> &Vec<String>
Method to retrieve all arguments not related to an option
sourcepub fn get_positional_args(&self) -> &Vec<String>
pub fn get_positional_args(&self) -> &Vec<String>
Method to retrieve all positionale arguments if ParsingMode is DefaultParsingMode the returned vector is empty
sourcepub fn set_parsing_mode(&mut self, parsing_mode: ParsingMode)
pub fn set_parsing_mode(&mut self, parsing_mode: ParsingMode)
Method to set parsing mode
parsing_mode- command line parser parsing mode
sourcepub fn parse_args_os(
&mut self,
args_os: ArgsOs
) -> StdResult<(), CommandLineParserError>
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.
sourcepub fn parse_args(
&mut self,
args: Args
) -> StdResult<(), CommandLineParserError>
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.
sourcepub fn process(&mut self)
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
sourcepub fn process_os(&mut self)
pub fn process_os(&mut self)
Method to process arguments of a process. Performs the same functionality as the process method but using ArgOs