mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
Auto merge of #503 - hoodie:master, r=kbknapp
test: adds failing alias doc test and example Hey there, I tried out the new aliasing feature, and they don't seem to play well with `.arg()`. This produces a lifetime errors: ```bash examples/20_aliases.rs:9:59: 9:74 error: borrowed value does not live long enough examples/20_aliases.rs:9 .aliases(&["list", "dir"]) ^~~~~~~~~~~~~~~ ```
This commit is contained in:
commit
11270d68d5
2 changed files with 47 additions and 4 deletions
39
examples/20_aliases.rs
Normal file
39
examples/20_aliases.rs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
extern crate clap;
|
||||||
|
|
||||||
|
use clap::{App, Arg, SubCommand};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
let matches = App::new("MyApp")
|
||||||
|
.subcommand(SubCommand::with_name("ls")
|
||||||
|
.aliases(&["list", "dir"])
|
||||||
|
.about("Adds files to myapp")
|
||||||
|
.version("0.1")
|
||||||
|
.author("Kevin K.")
|
||||||
|
.arg(Arg::with_name("input")
|
||||||
|
.help("the file to add")
|
||||||
|
.index(1)
|
||||||
|
.required(true))
|
||||||
|
)
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
// You can check if a subcommand was used like normal
|
||||||
|
if matches.is_present("add") {
|
||||||
|
println!("'myapp add' was run.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// You can get the independent subcommand matches (which function exactly like App matches)
|
||||||
|
if let Some(ref matches) = matches.subcommand_matches("add") {
|
||||||
|
// Safe to use unwrap() because of the required() option
|
||||||
|
println!("Adding file: {}", matches.value_of("input").unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
// You can also match on a subcommand's name
|
||||||
|
match matches.subcommand_name() {
|
||||||
|
Some("add") => println!("'myapp add' was used"),
|
||||||
|
None => println!("No subcommand was used"),
|
||||||
|
_ => println!("Some other subcommand was used"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Continued program logic goes here...
|
||||||
|
}
|
|
@ -535,21 +535,25 @@ impl<'a, 'b> App<'a, 'b> {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```rust
|
||||||
/// # use clap::{App, Arg, SubCommand};
|
/// # use clap::{App, Arg, SubCommand};
|
||||||
/// let m = App::new("myprog")
|
/// let m = App::new("myprog")
|
||||||
/// .subcommand(SubCommand::with_name("test")
|
/// .subcommand(SubCommand::with_name("test")
|
||||||
/// .aliases(&["do-stuff", "do-tests", "tests"]))
|
/// .aliases(&["do-stuff", "do-tests", "tests"]))
|
||||||
|
/// .arg(Arg::with_name("input")
|
||||||
|
/// .help("the file to add")
|
||||||
|
/// .index(1)
|
||||||
|
/// .required(false))
|
||||||
/// .get_matches_from(vec!["myprog", "do-tests"]);
|
/// .get_matches_from(vec!["myprog", "do-tests"]);
|
||||||
/// assert_eq!(m.subcommand_name(), Some("test"));
|
/// assert_eq!(m.subcommand_name(), Some("test"));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn aliases<S: AsRef<str> + 'b>(mut self, names: &'b [S]) -> Self {
|
pub fn aliases(mut self, names: &[&'b str]) -> Self {
|
||||||
if let Some(ref mut als) = self.p.meta.aliases {
|
if let Some(ref mut als) = self.p.meta.aliases {
|
||||||
for n in names {
|
for n in names {
|
||||||
als.push(n.as_ref());
|
als.push(n);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.p.meta.aliases = Some(names.iter().map(|n| n.as_ref()).collect());
|
self.p.meta.aliases = Some(names.iter().map(|n| *n).collect::<Vec<_>>());
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue