pub struct getopt {
pub options: HashMap<char, String>,
pub arguments: Vec<String>,
pub option_has_arg: HashMap<char, bool>,
}Expand description
Parsed command line options.
Created by new function. Structure contains isolated command line
arguments and collected options with their optional values.
Structure can contain options that are not listed in optstring
passed to new function. For strict parse mode pass this
structure to validate function.
Fields§
§options: HashMap<char, String>Map of command line options and their optional values extracted from command line arguments.
If an option does not have a value, the empty string “” is stored.
arguments: Vec<String>Isolated command line arguments without options.
option_has_arg: HashMap<char, bool>Map indicating whether an option has a required argument.
This map contains all recognized options. The presence of an option in this map does not mean that the option must always be present or supplied as a command line argument.
Implementations§
Source§impl getopt
impl getopt
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns number of command line arguments not counting options.
It is a convenience shortcut for getopt.arguments.len().
§Example
use std::env::args;
use getopt3::hideBin;
let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(g) = getopt_rc {
println!("Number of command line arguments is {}", g.len());
};Sourcepub fn iter(&self) -> Iter<'_, String>
pub fn iter(&self) -> Iter<'_, String>
Returns Iterator over command line arguments.
It is a convenience shortcut for getopt.arguments.iter().
§Note
Options are skipped by the Iterator.
§Example
use std::env::args;
use getopt3::hideBin;
let getopt_rc = getopt3::new(hideBin(args()), "abc");
if let Ok(my_getopt) = getopt_rc {
for arg in my_getopt.iter() {
println!("Argument: {}", arg);
}
}Sourcepub fn get(&self, option: char) -> Option<&String>
pub fn get(&self, option: char) -> Option<&String>
Return command line option value.
It is a convenience shortcut for getopt.options.get().
§Return value
- If option was supplied on command line returned value is
Some. - If option doesn’t have argument or argument is
missing, a reference to an empty
Stringis returned. - If option were not supplied by user returned value is
None.
§Example
use std::env::args;
use getopt3::hideBin;
let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(my_getopt) = getopt_rc {
if let Some(b_value) = my_getopt.get('b') {
println!("-b argument is: {}", b_value);
}
}Sourcepub fn has(&self, option: char) -> bool
pub fn has(&self, option: char) -> bool
Check if command line option was supplied on command line.
It is a convenience shortcut for getopt.options.contains_key().
§Return value
- If option was supplied on command line returned value is
true. - If option was not supplied by user returned value is
false.
§Example
use std::env::args;
use getopt3::hideBin;
let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(my_getopt) = getopt_rc {
if my_getopt.has('b') {
println!("-b argument is: {}", my_getopt.get('b').unwrap());
}
}Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if any non option arguments were supplied.
It is a convenience shortcut for getopt.arguments.is_empty().
§Return value
- If any arguments were supplied on the command line returned value is
true. - If no arguments were supplied on the command line returned value is
false.
§Example
use std::env::args;
use getopt3::hideBin;
let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(my_getopt) = getopt_rc {
if !my_getopt.is_empty() {
println!("Arguments were supplied on command line");
}
}Trait Implementations§
Source§impl<'a> IntoIterator for &'a getopt
Iterates over arguments without consuming getopt.
impl<'a> IntoIterator for &'a getopt
Iterates over arguments without consuming getopt.
Source§impl IntoIterator for getopt
Consumes getopt and returns Iterator over arguments.
impl IntoIterator for getopt
Consumes getopt and returns Iterator over arguments.