Struct rusty_dumb_tools::arg::DumbArgParser
source · pub struct DumbArgParser { /* private fields */ }
Expand description
a simple argument parser.
example usage:
use rusty_dumb_tools::{arg::{DumbArgParser, DumbArgBuilder}, dap_arg};
let mut parser = DumbArgParser::new();
parser.set_description("This is a simple argument parser.");
parser.set_allow_missing_arguments(); // normal should not do this
dap_arg!("-v", flag2="--verbose", fixed=true).add_to(&mut parser); // argument flag "-v" / "--verbose" with fixed value (true) when the flag is present
dap_arg!("-n", flag2="--name", default="nobody").add_to(&mut parser); // argument "-n" / "--name" requiring input value, with default "nobody"
dap_arg!("str-arg").add_to(&mut parser); // positional argument "str-arg" (of type String)
dap_arg!("i32-arg", value=123).add_to(&mut parser); // positional argument "i32-arg" of type i32 (inferred from the value 123)
dap_arg!("multi-arg").set_multi().add_to(&mut parser); // positional multi-argument "multi-arg" that will accept multiple values (one + rest)
// parser.show_help(None, false);
// assert_eq!("<program> [-h] [-v] [-n name] str-arg i32-arg multi-arg", parser.compose_usage());
parser.parse_args();
println!(". -v: {:?}", parser.get::<bool>("-v"));
println!(". --verbose: {:?}", parser.get::<bool>("--verbose")); // will be the same parameter value as "-v"
println!(". --name: {:?}", parser.get::<String>("--name")); // can use "-n" as well
println!(". str-arg: {:?}", parser.get::<String>("str-arg"));
println!(". i32-arg: {:?}", parser.get::<i32>("i32-arg"));
println!(". multi-arg: {:?}", parser.get_multi::<String>("multi-arg"));
notes:
- -h and –help are reserved for showing help message; after showing the help message, the program will exit
- in case of invalid input argument, will show the error message as well as the help message, then the program will exit
- arguments are typed; the default is
String
; others arestd::i32
,std::i64
,std::f32
,std::f64
andbool
- also see the macro
dap_arg
you may want to refer to crate::demo::run_demo
for a demo program that uses DumbArgParser
.
Implementations§
source§impl DumbArgParser
impl DumbArgParser
sourcepub fn new() -> DumbArgParser
pub fn new() -> DumbArgParser
create and instance of DumbArgParser; program name will be extracted from env::args
when DumbArgParser::parse_args
is called
use DumbArgParser::new_with_name
if want to create an instance with a specific program name
sourcepub fn new_with_name(program_name: &str) -> DumbArgParser
pub fn new_with_name(program_name: &str) -> DumbArgParser
create an instance of DumbArgParser with the specific program name to be shown in the help message
sourcepub fn set_description(&mut self, description: &str)
pub fn set_description(&mut self, description: &str)
set the program description to be shown in the help message
sourcepub fn set_allow_missing_arguments(&mut self)
pub fn set_allow_missing_arguments(&mut self)
in case of missing arguments, DumbArgParser::get
will return None
instead of exiting the program
sourcepub fn compose_usage(&self) -> String
pub fn compose_usage(&self) -> String
compose the “usage” part of the help message, mostly for illustration use
sourcepub fn compose_inputs(&self) -> String
pub fn compose_inputs(&self) -> String
compose the equivalent argument inputs after parsing, mostly for illustration use
sourcepub fn get<T: ArgValueTrait>(&self, arg_name: &str) -> Option<T>
pub fn get<T: ArgValueTrait>(&self, arg_name: &str) -> Option<T>
get the parsed – DumbArgParser::parse_args
– argument value (parameter) assigned to the given argument name
arg_name
- the argument name of which the value is to be retrieved; it can be a positional argument name, or can be a flag argument name (includingflag2
, which is just an alias to the flag argument)
e.g.
let param: i32 = parser.get("i32-arg").unwrap();
or
let param = parser.get::<i32>("i32-arg").unwrap();
note: except that all types can be implicitly converted to String
, no other implicit type conversion; if type does not agree, will panic
sourcepub fn get_string(&self, arg_name: &str) -> Option<String>
pub fn get_string(&self, arg_name: &str) -> Option<String>
like DumbArgParser::get
but returns a String
sourcepub fn get_multi<T: ArgValueTrait>(&self, arg_name: &str) -> Option<Vec<T>>
pub fn get_multi<T: ArgValueTrait>(&self, arg_name: &str) -> Option<Vec<T>>
get the parsed – DumbArgParser::parse_args
– multi-argument values – see DumbArgBuilder::set_multi
– associated with the given argument name
arg_name
- the argument name of which the values are to be retrieved
note: like DumbArgParser::get
, except when target type is String
, no implicit type conversion
sourcepub fn get_multi_strings(&self, arg_name: &str) -> Option<Vec<String>>
pub fn get_multi_strings(&self, arg_name: &str) -> Option<Vec<String>>
like DumbArgParser::get_multi
but returns a Vec
of String
sourcepub fn get_rest(&self, arg_name: &str) -> Option<Vec<String>>
pub fn get_rest(&self, arg_name: &str) -> Option<Vec<String>>
get the parsed – DumbArgParser::parse_args
– “rest” multi-argument values – see DumbArgBuilder::set_rest
– associated with the given argument name
arg_name
- the argument name of which the values are to be retrieved
sourcepub fn process_rest_args(&self, arg_name: &str, parser: &mut DumbArgParser)
pub fn process_rest_args(&self, arg_name: &str, parser: &mut DumbArgParser)
IMPORTANT: assume DumbArgParser::get_rest
is able to retrieve the “rest” multi-argument values
sourcepub fn check_process_rest_args(
&self,
arg_name: &str,
parser: &mut DumbArgParser,
show_help_if_needed: bool
) -> Result<bool, DumbError>
pub fn check_process_rest_args( &self, arg_name: &str, parser: &mut DumbArgParser, show_help_if_needed: bool ) -> Result<bool, DumbError>
IMPORTANT: assume DumbArgParser::get_rest
is able to retrieve the “rest” multi-argument values
sourcepub fn parse_args(&mut self)
pub fn parse_args(&mut self)
parse from the input program arguments – env::args
– for argument values (parameters);
after parsing, the argument values can be retrieved by DumbArgParser::get
sourcepub fn check_parse_args(
&mut self,
show_help_if_needed: bool
) -> Result<bool, DumbError>
pub fn check_parse_args( &mut self, show_help_if_needed: bool ) -> Result<bool, DumbError>
like DumbArgParser::parse_args
but returns a Result
instead of exiting the program
pub fn show_help(&self, err_msg: Option<String>, and_exit: bool)
sourcepub fn process_args(&mut self, in_args: Vec<&str>)
pub fn process_args(&mut self, in_args: Vec<&str>)
like DumbArgParser::parse_args
but the input arguments to parse are provided explicitly
sourcepub fn check_process_args(
&mut self,
in_args: Vec<&str>,
show_help_if_needed: bool
) -> Result<bool, DumbError>
pub fn check_process_args( &mut self, in_args: Vec<&str>, show_help_if_needed: bool ) -> Result<bool, DumbError>
like DumbArgParser::process_args
but returns a Result
instead of exiting the program