mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
Merge pull request #4169 from epage/helper
feat(parser): Provide convenience accessors for Actions
This commit is contained in:
commit
5e69f92efc
10 changed files with 84 additions and 43 deletions
|
@ -12,13 +12,10 @@ fn main() {
|
||||||
let cli = DerivedArgs::augment_args(cli);
|
let cli = DerivedArgs::augment_args(cli);
|
||||||
|
|
||||||
let matches = cli.get_matches();
|
let matches = cli.get_matches();
|
||||||
println!(
|
println!("Value of built: {:?}", matches.get_flag("built"));
|
||||||
"Value of built: {:?}",
|
|
||||||
*matches.get_one::<bool>("built").unwrap()
|
|
||||||
);
|
|
||||||
println!(
|
println!(
|
||||||
"Value of derived via ArgMatches: {:?}",
|
"Value of derived via ArgMatches: {:?}",
|
||||||
*matches.get_one::<bool>("derived").unwrap()
|
matches.get_flag("derived")
|
||||||
);
|
);
|
||||||
|
|
||||||
// Since DerivedArgs implements FromArgMatches, we can extract it from the unstructured ArgMatches.
|
// Since DerivedArgs implements FromArgMatches, we can extract it from the unstructured ArgMatches.
|
||||||
|
|
|
@ -15,8 +15,8 @@ impl FromArgMatches for CliArgs {
|
||||||
}
|
}
|
||||||
fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> {
|
fn from_arg_matches_mut(matches: &mut ArgMatches) -> Result<Self, Error> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
foo: *matches.get_one::<bool>("foo").expect("defaulted by clap"),
|
foo: matches.get_flag("foo"),
|
||||||
bar: *matches.get_one::<bool>("bar").expect("defaulted by clap"),
|
bar: matches.get_flag("bar"),
|
||||||
quuz: matches.remove_one::<String>("quuz"),
|
quuz: matches.remove_one::<String>("quuz"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,8 @@ impl FromArgMatches for CliArgs {
|
||||||
self.update_from_arg_matches_mut(&mut matches)
|
self.update_from_arg_matches_mut(&mut matches)
|
||||||
}
|
}
|
||||||
fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> {
|
fn update_from_arg_matches_mut(&mut self, matches: &mut ArgMatches) -> Result<(), Error> {
|
||||||
self.foo |= *matches.get_one::<bool>("foo").expect("defaulted by clap");
|
self.foo |= matches.get_flag("foo");
|
||||||
self.bar |= *matches.get_one::<bool>("bar").expect("defaulted by clap");
|
self.bar |= matches.get_flag("bar");
|
||||||
if let Some(quuz) = matches.remove_one::<String>("quuz") {
|
if let Some(quuz) = matches.remove_one::<String>("quuz") {
|
||||||
self.quuz = Some(quuz);
|
self.quuz = Some(quuz);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,7 @@ fn main() {
|
||||||
// This is what will happen with `myprog -f -p=bob -- sloppy slop slop`...
|
// This is what will happen with `myprog -f -p=bob -- sloppy slop slop`...
|
||||||
|
|
||||||
// -f used: true
|
// -f used: true
|
||||||
println!(
|
println!("-f used: {:?}", matches.get_flag("eff"));
|
||||||
"-f used: {:?}",
|
|
||||||
*matches.get_one::<bool>("eff").expect("defaulted by clap")
|
|
||||||
);
|
|
||||||
// -p's value: Some("bob")
|
// -p's value: Some("bob")
|
||||||
println!("-p's value: {:?}", matches.get_one::<String>("pea"));
|
println!("-p's value: {:?}", matches.get_one::<String>("pea"));
|
||||||
// 'slops' values: Some(["sloppy", "slop", "slop"])
|
// 'slops' values: Some(["sloppy", "slop", "slop"])
|
||||||
|
|
|
@ -89,10 +89,7 @@ fn main() {
|
||||||
.collect();
|
.collect();
|
||||||
let values = packages.join(", ");
|
let values = packages.join(", ");
|
||||||
|
|
||||||
if *sync_matches
|
if sync_matches.get_flag("info") {
|
||||||
.get_one::<bool>("info")
|
|
||||||
.expect("defaulted by clap")
|
|
||||||
{
|
|
||||||
println!("Retrieving info for {}...", values);
|
println!("Retrieving info for {}...", values);
|
||||||
} else {
|
} else {
|
||||||
println!("Installing {}...", values);
|
println!("Installing {}...", values);
|
||||||
|
|
|
@ -10,10 +10,5 @@ fn main() {
|
||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
println!(
|
println!("verbose: {:?}", matches.get_flag("verbose"));
|
||||||
"verbose: {:?}",
|
|
||||||
*matches
|
|
||||||
.get_one::<bool>("verbose")
|
|
||||||
.expect("defaulted by clap")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,5 @@ fn main() {
|
||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
println!(
|
println!("verbose: {:?}", matches.get_count("verbose"));
|
||||||
"verbose: {:?}",
|
|
||||||
matches
|
|
||||||
.get_one::<u8>("verbose")
|
|
||||||
.expect("Count always defaulted")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,9 +50,9 @@ fn main() {
|
||||||
} else {
|
} else {
|
||||||
// Increment the one requested (in a real program, we'd reset the lower numbers)
|
// Increment the one requested (in a real program, we'd reset the lower numbers)
|
||||||
let (maj, min, pat) = (
|
let (maj, min, pat) = (
|
||||||
*matches.get_one::<bool>("major").expect("defaulted by clap"),
|
matches.get_flag("major"),
|
||||||
*matches.get_one::<bool>("minor").expect("defaulted by clap"),
|
matches.get_flag("minor"),
|
||||||
*matches.get_one::<bool>("patch").expect("defaulted by clap"),
|
matches.get_flag("patch"),
|
||||||
);
|
);
|
||||||
match (maj, min, pat) {
|
match (maj, min, pat) {
|
||||||
(true, _, _) => major += 1,
|
(true, _, _) => major += 1,
|
||||||
|
|
|
@ -35,10 +35,7 @@ fn main() {
|
||||||
|
|
||||||
// See if --set-ver was used to set the version manually
|
// See if --set-ver was used to set the version manually
|
||||||
let version = if let Some(ver) = matches.get_one::<String>("set-ver") {
|
let version = if let Some(ver) = matches.get_one::<String>("set-ver") {
|
||||||
if *matches.get_one::<bool>("major").expect("defaulted by clap")
|
if matches.get_flag("major") || matches.get_flag("minor") || matches.get_flag("patch") {
|
||||||
|| *matches.get_one::<bool>("minor").expect("defaulted by clap")
|
|
||||||
|| *matches.get_one::<bool>("patch").expect("defaulted by clap")
|
|
||||||
{
|
|
||||||
cmd.error(
|
cmd.error(
|
||||||
ErrorKind::ArgumentConflict,
|
ErrorKind::ArgumentConflict,
|
||||||
"Can't do relative and absolute version change",
|
"Can't do relative and absolute version change",
|
||||||
|
@ -49,9 +46,9 @@ fn main() {
|
||||||
} else {
|
} else {
|
||||||
// Increment the one requested (in a real program, we'd reset the lower numbers)
|
// Increment the one requested (in a real program, we'd reset the lower numbers)
|
||||||
let (maj, min, pat) = (
|
let (maj, min, pat) = (
|
||||||
*matches.get_one::<bool>("major").expect("defaulted by clap"),
|
matches.get_flag("major"),
|
||||||
*matches.get_one::<bool>("minor").expect("defaulted by clap"),
|
matches.get_flag("minor"),
|
||||||
*matches.get_one::<bool>("patch").expect("defaulted by clap"),
|
matches.get_flag("patch"),
|
||||||
);
|
);
|
||||||
match (maj, min, pat) {
|
match (maj, min, pat) {
|
||||||
(true, false, false) => major += 1,
|
(true, false, false) => major += 1,
|
||||||
|
|
|
@ -193,15 +193,15 @@ pub enum ArgAction {
|
||||||
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap();
|
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap();
|
||||||
/// assert!(matches.contains_id("flag"));
|
/// assert!(matches.contains_id("flag"));
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// matches.get_one::<u8>("flag").copied(),
|
/// matches.get_count("flag"),
|
||||||
/// Some(2)
|
/// 2
|
||||||
/// );
|
/// );
|
||||||
///
|
///
|
||||||
/// let matches = cmd.try_get_matches_from(["mycmd"]).unwrap();
|
/// let matches = cmd.try_get_matches_from(["mycmd"]).unwrap();
|
||||||
/// assert!(matches.contains_id("flag"));
|
/// assert!(matches.contains_id("flag"));
|
||||||
/// assert_eq!(
|
/// assert_eq!(
|
||||||
/// matches.get_one::<u8>("flag").copied(),
|
/// matches.get_count("flag"),
|
||||||
/// Some(0)
|
/// 0
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
Count,
|
Count,
|
||||||
|
|
|
@ -112,6 +112,69 @@ impl ArgMatches {
|
||||||
MatchesError::unwrap(id, self.try_get_one(id))
|
MatchesError::unwrap(id, self.try_get_one(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the value of a specific [`ArgAction::Count`][crate::ArgAction::Count] flag
|
||||||
|
///
|
||||||
|
/// # Panic
|
||||||
|
///
|
||||||
|
/// If the argument's action is not [`ArgAction::Count`][crate::ArgAction::Count]
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use clap::Command;
|
||||||
|
/// # use clap::Arg;
|
||||||
|
/// let cmd = Command::new("mycmd")
|
||||||
|
/// .arg(
|
||||||
|
/// Arg::new("flag")
|
||||||
|
/// .long("flag")
|
||||||
|
/// .action(clap::ArgAction::Count)
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap();
|
||||||
|
/// assert_eq!(
|
||||||
|
/// matches.get_count("flag"),
|
||||||
|
/// 2
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
#[track_caller]
|
||||||
|
pub fn get_count(&self, id: &str) -> u8 {
|
||||||
|
*self
|
||||||
|
.get_one::<u8>(id)
|
||||||
|
.expect("ArgAction::Count is defaulted")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the value of a specific [`ArgAction::SetTrue`][crate::ArgAction::SetTrue] or [`ArgAction::SetFalse`][crate::ArgAction::SetFalse] flag
|
||||||
|
///
|
||||||
|
/// # Panic
|
||||||
|
///
|
||||||
|
/// If the argument's action is not [`ArgAction::SetTrue`][crate::ArgAction::SetTrue] or [`ArgAction::SetFalse`][crate::ArgAction::SetFalse]
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use clap::Command;
|
||||||
|
/// # use clap::Arg;
|
||||||
|
/// let cmd = Command::new("mycmd")
|
||||||
|
/// .arg(
|
||||||
|
/// Arg::new("flag")
|
||||||
|
/// .long("flag")
|
||||||
|
/// .action(clap::ArgAction::SetTrue)
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// let matches = cmd.clone().try_get_matches_from(["mycmd", "--flag", "--flag"]).unwrap();
|
||||||
|
/// assert!(matches.contains_id("flag"));
|
||||||
|
/// assert_eq!(
|
||||||
|
/// matches.get_flag("flag"),
|
||||||
|
/// true
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
#[track_caller]
|
||||||
|
pub fn get_flag(&self, id: &str) -> bool {
|
||||||
|
*self
|
||||||
|
.get_one::<bool>(id)
|
||||||
|
.expect("ArgAction::SetTrue / ArgAction::SetFalse is defaulted")
|
||||||
|
}
|
||||||
|
|
||||||
/// Iterate over values of a specific option or positional argument.
|
/// Iterate over values of a specific option or positional argument.
|
||||||
///
|
///
|
||||||
/// i.e. an argument that takes multiple values at runtime.
|
/// i.e. an argument that takes multiple values at runtime.
|
||||||
|
|
Loading…
Add table
Reference in a new issue