pub struct Args { /* private fields */ }Expand description
Implementations§
Source§impl Args
impl Args
Sourcepub fn try_into_sub_cmd(self) -> Result<(Option<String>, Self)>
pub fn try_into_sub_cmd(self) -> Result<(Option<String>, Self)>
§Transforms into sub command
For example:
-
Command line:
~> program help version 1 -
Parsed as:
help version 1 -
After calling this function:
version 1
use dia_args;
const CMD_VERSION: &str = "version";
let (cmd, args) = dia_args::parse()?.try_into_sub_cmd()?;
match cmd.as_ref().map(|s| s.as_str()) {
Some(CMD_VERSION) => if args.is_empty() {
println!("Version: ...");
} else {
eprintln!("{:?} command doesn't take arguments", CMD_VERSION);
},
Some(other) => eprintln!("Command {:?} not supported", other),
None => eprintln!("Missing command"),
};
Sourcepub fn take_sub_args(&mut self) -> Vec<String>
pub fn take_sub_args(&mut self) -> Vec<String>
Sourcepub fn merge_options(
&mut self,
other: &mut Self,
filter: &[&[&str]],
merge_option: MergeOption,
) -> Result<usize>
pub fn merge_options( &mut self, other: &mut Self, filter: &[&[&str]], merge_option: MergeOption, ) -> Result<usize>
§Merges options with other
- This function works on options, not commands/sub arguments…
- Other’s options will be taken out, if conditions are met.
- Result is number of items merged.
§Parameters
-
filter:- If you provide some sets of keys, only those (from other) are accepted.
- If you provide an empty slice, or any of its items is empty, an error is returned.
§Examples
Your program allows the user to set options from file. Later you want to give the user new ability to set options via command line, overwriting the ones from file. Then this function can help.
use dia_args::MergeOption;
const OPTION_DEBUG: &[&str] = &["-d", "--debug"];
const OPTION_PORT: &[&str] = &["--port"];
// Here in test, we're parsing from strings.
// In real code, you might want to use dia_args::parse_file()
let mut args_from_file = dia_args::parse_strings(
["--debug=false", "--port=6789"]
)?;
// Command line arguments
let mut cmd_line_args = dia_args::parse_strings(
["-d=true", "--address", "localhost"]
)?;
// Merge
let count = cmd_line_args.merge_options(
&mut args_from_file, &[OPTION_DEBUG, OPTION_PORT], MergeOption::IgnoreExisting,
)?;
assert_eq!(count, 1);
// Verify
assert_eq!(cmd_line_args.take(OPTION_DEBUG)?, Some(true));
assert_eq!(cmd_line_args.take::<String>(&["--address"])?.unwrap(), "localhost");
assert_eq!(cmd_line_args.take::<u16>(OPTION_PORT)?, Some(6789));
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