pub struct CommandLine { /* private fields */ }Expand description
The CommandLine is the struct holding all parsed options and arguments.
For options, the method has_option will return true if that option is specified,
regardless of whether it has a value. If the option has values, method get_value
or get_values can be used to retrieve the parsed value of your requested type.
The resultant value is Option<Result<>> for get_value and Vec<Result<>>
for get_values, which is a bit verbose to use. You could use get_expected_value
and get_expected_values instead. They auto unwrap the results and exit with
well described error message in case of no value or type conversion error.
The arguments are the additional values that are not captured by any option.
Method get_arg_list is to retrieve all arguments in type Vec<&str>, the type
conversion should be done in user application if needed.
Implementations§
Source§impl CommandLine
impl CommandLine
pub fn builder() -> CmdBuilder
pub fn add_arg(&mut self, arg: &str)
pub fn add_option(&mut self, option: Rc<RefCell<AnpOption>>)
Sourcepub fn get_arg_list(&self) -> Vec<&str>
pub fn get_arg_list(&self) -> Vec<&str>
Get additional arguments that are not captured by any options.
The first arguments is typically the filename of the executable.
Sourcepub fn get_option_properties(&self, option: &str) -> HashMap<String, String>
pub fn get_option_properties(&self, option: &str) -> HashMap<String, String>
Get option values as an key-value pair.
For example, option --value a b results in {"a": "b"} and option --value a
results in {"a", "true"}. Note that if the values are more than 2,
remaining values are ignored.
Sourcepub fn get_options(&self) -> Vec<Ref<'_, AnpOption>>
pub fn get_options(&self) -> Vec<Ref<'_, AnpOption>>
Get all AnpOption that passed to the command line.
Sourcepub fn get_value<T: FromStr>(&self, opt: &str) -> Option<Result<T, T::Err>>
pub fn get_value<T: FromStr>(&self, opt: &str) -> Option<Result<T, T::Err>>
Get parsed option value in requested type.
None is returned if no option opt or opt has no value.
If the opt has more than 1 value, the first value is returned.
The generic type must implement the trait FromStr.
It the generic type is set to String, it’s guaranteed that the result is ok.
Also see CommandLine::get_values.
Sourcepub fn get_values<T: FromStr>(
&self,
opt: &str,
) -> Option<Vec<Result<T, T::Err>>>
pub fn get_values<T: FromStr>( &self, opt: &str, ) -> Option<Vec<Result<T, T::Err>>>
Get parsed option values in requested type.
Empty Vec is returned if no option opt or opt has no value.
The generic type must implement the trait FromStr.
It the generic type is set to String, it’s guaranteed that the result is ok.
Also see CommandLine::get_value.
Sourcepub fn get_expected_value<T: FromStr + Debug>(&self, opt: &str) -> T
pub fn get_expected_value<T: FromStr + Debug>(&self, opt: &str) -> T
Get parsed option value in requested type or exit.
The method auto unwrap result from CommandLine::get_value.
If the result is None or Err, the program exit with error message.
Also see CommandLine::get_expected_values.
Sourcepub fn get_expected_values<T: FromStr + Debug>(&self, opt: &str) -> Vec<T>
pub fn get_expected_values<T: FromStr + Debug>(&self, opt: &str) -> Vec<T>
Get parsed option values in requested type or exit.
The method auto unwrap result from CommandLine::get_values.
If any value in Vec is Err, the program exit with error message.
Also see CommandLine::get_expected_value.
Sourcepub fn has_option(&self, opt: &str) -> bool
pub fn has_option(&self, opt: &str) -> bool
Check if the opt is specified in command line.