pub struct Args { /* private fields */ }Expand description
A struct representing parsed command-line arguments.
§Example:
let args = valargs::parse();
if let Some(cat_name) = args.nth(1) {
println!("the cat's name is {}", cat_name);
}
if args.has_option("orange") {
println!("the cat is an orange cat");
}
if let Some(favorite_food) = args.option("fav-food") {
println!("the cat likes {} a lot", favorite_food);
} else {
println!("no information about the cat's favorite food...");
}Implementations§
Source§impl Args
impl Args
Sourcepub fn nth(&self, index: usize) -> Option<&str>
pub fn nth(&self, index: usize) -> Option<&str>
Gets the nth argument (including the executable name).
§Example:
let args = valargs::parse();
let _ = args.nth(0); // executable name
let command = args.nth(1); // first argument
match command {
Some("hello") => {
let name = args.nth(2); // second argument
if let Some(name) = name {
println!("hello {} !!", name);
} else {
println!("hello !!");
}
}
Some(_) => println!("unknown command"),
None => {
println!("please provide a command");
}
}Sourcepub fn has_option(&self, option_name: &str) -> bool
pub fn has_option(&self, option_name: &str) -> bool
Check if the given option name is present.
Sourcepub fn option<'a>(&'a self, option_name: &str) -> Option<Option<&'a str>>
pub fn option<'a>(&'a self, option_name: &str) -> Option<Option<&'a str>>
Get the option and it potential value as an Option<Option<&str>>,
the first option represents the presence of the option in the
argument array and the second one the presence of a value associated
with that option.
Sourcepub fn option_value<'a>(&'a self, option_name: &str) -> Option<&'a str>
pub fn option_value<'a>(&'a self, option_name: &str) -> Option<&'a str>
Get the value associated with the given option name if present.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Args
impl RefUnwindSafe for Args
impl Send for Args
impl Sync for Args
impl Unpin for Args
impl UnwindSafe for Args
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more