mirror of
https://github.com/clap-rs/clap
synced 2024-12-12 22:02:35 +00:00
Moved to consuming builder pattern, removed unneeded fields
This commit is contained in:
parent
809b43995b
commit
e057ed4e60
3 changed files with 39 additions and 40 deletions
55
src/app.rs
55
src/app.rs
|
@ -46,7 +46,7 @@ pub struct App {
|
|||
flags: HashMap<&'static str, FlagArg>,
|
||||
opts: HashMap<&'static str, OptArg>,
|
||||
positionals_idx: BTreeMap<u8, PosArg>,
|
||||
positionals_name: HashMap<&'static str, PosArg>,
|
||||
// positionals_name: HashMap<&'static str, PosArg>,
|
||||
needs_long_help: bool,
|
||||
needs_long_version: bool,
|
||||
needs_short_help: bool,
|
||||
|
@ -78,7 +78,7 @@ impl App {
|
|||
flags: HashMap::new(),
|
||||
opts: HashMap::new(),
|
||||
positionals_idx: BTreeMap::new(),
|
||||
positionals_name: HashMap::new(),
|
||||
// positionals_name: HashMap::new(),
|
||||
needs_long_version: true,
|
||||
needs_long_help: true,
|
||||
needs_short_help: true,
|
||||
|
@ -101,7 +101,7 @@ impl App {
|
|||
/// .author("Kevin <kbknapp@gmail.com>")
|
||||
/// # .get_matches();
|
||||
/// ```
|
||||
pub fn author(&mut self, a: &'static str) -> &mut App {
|
||||
pub fn author(mut self, a: &'static str) -> App {
|
||||
self.author = Some(a);
|
||||
self
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ impl App {
|
|||
/// .about("Does really amazing things to great people")
|
||||
/// # .get_matches();
|
||||
/// ```
|
||||
pub fn about(&mut self, a: &'static str) -> &mut App {
|
||||
pub fn about(mut self, a: &'static str) -> App {
|
||||
self.about = Some(a);
|
||||
self
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ impl App {
|
|||
/// .version("v0.1.24")
|
||||
/// # .get_matches();
|
||||
/// ```
|
||||
pub fn version(&mut self, v: &'static str)-> &mut App {
|
||||
pub fn version(mut self, v: &'static str)-> App {
|
||||
self.version = Some(v);
|
||||
self
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ impl App {
|
|||
/// )
|
||||
/// # .get_matches();
|
||||
/// ```
|
||||
pub fn arg(&mut self, a: &Arg) -> &mut App {
|
||||
pub fn arg(mut self, a: Arg) -> App {
|
||||
if self.arg_list.contains(a.name) {
|
||||
panic!("Argument name must be unique, \"{}\" is already in use", a.name);
|
||||
} else {
|
||||
|
@ -173,21 +173,21 @@ impl App {
|
|||
self.required.insert(a.name);
|
||||
}
|
||||
if let Some(i) = a.index {
|
||||
self.positionals_name.insert(a.name, PosArg {
|
||||
name: a.name,
|
||||
index: i,
|
||||
required: a.required,
|
||||
help: a.help,
|
||||
blacklist: a.blacklist.clone(),
|
||||
requires: a.requires.clone(),
|
||||
value: None
|
||||
});
|
||||
// self.positionals_name.insert(a.name, PosArg {
|
||||
// name: a.name,
|
||||
// index: i,
|
||||
// required: a.required,
|
||||
// help: a.help,
|
||||
// blacklist: a.blacklist,
|
||||
// requires: a.requires,
|
||||
// value: None
|
||||
// });
|
||||
self.positionals_idx.insert(i, PosArg {
|
||||
name: a.name,
|
||||
index: i,
|
||||
required: a.required,
|
||||
blacklist: a.blacklist.clone(),
|
||||
requires: a.requires.clone(),
|
||||
blacklist: a.blacklist,
|
||||
requires: a.requires,
|
||||
help: a.help,
|
||||
value: None
|
||||
});
|
||||
|
@ -199,9 +199,9 @@ impl App {
|
|||
name: a.name,
|
||||
short: a.short,
|
||||
long: a.long,
|
||||
blacklist: a.blacklist.clone(),
|
||||
blacklist: a.blacklist,
|
||||
help: a.help,
|
||||
requires: a.requires.clone(),
|
||||
requires: a.requires,
|
||||
required: a.required,
|
||||
value: None
|
||||
});
|
||||
|
@ -232,9 +232,9 @@ impl App {
|
|||
short: a.short,
|
||||
long: a.long,
|
||||
help: a.help,
|
||||
blacklist: a.blacklist.clone(),
|
||||
blacklist: a.blacklist,
|
||||
multiple: a.multiple,
|
||||
requires: a.requires.clone(),
|
||||
requires: a.requires,
|
||||
occurrences: 1
|
||||
});
|
||||
}
|
||||
|
@ -252,14 +252,14 @@ impl App {
|
|||
/// Arg::new("debug").short("d")])
|
||||
/// # .get_matches();
|
||||
/// ```
|
||||
pub fn args(&mut self, args: Vec<&Arg>) -> &mut App {
|
||||
for arg in args.iter() {
|
||||
self.arg(arg);
|
||||
pub fn args(mut self, args: Vec<Arg>) -> App {
|
||||
for arg in args.into_iter() {
|
||||
self = self.arg(arg);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn exit(self) {
|
||||
fn exit(&self) {
|
||||
unsafe { libc::exit(0); }
|
||||
}
|
||||
|
||||
|
@ -286,7 +286,7 @@ impl App {
|
|||
print!("\t{} {} {} {}", self.name,
|
||||
if ! self.flags.is_empty() {flags = true; "[FLAGS]"} else {""},
|
||||
if ! self.opts.is_empty() {opts = true; "[OPTIONS]"} else {""},
|
||||
if ! self.positionals_name.is_empty() {pos = true; "[POSITIONAL]"} else {""});
|
||||
if ! self.positionals_idx.is_empty() {pos = true; "[POSITIONAL]"} else {""});
|
||||
if flags || opts || pos {
|
||||
println!("");
|
||||
}
|
||||
|
@ -585,7 +585,6 @@ impl App {
|
|||
occurrences: 1
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn get_matches(mut self) -> ArgMatches {
|
||||
|
@ -656,7 +655,7 @@ impl App {
|
|||
} else {
|
||||
// Positional
|
||||
|
||||
if self.positionals_idx.is_empty() || self.positionals_name.is_empty() {
|
||||
if self.positionals_idx.is_empty() { // || self.positionals_name.is_empty() {
|
||||
self.report_error(
|
||||
format!("Found positional argument {}, but {} doesn't accept any", arg, self.name),
|
||||
false, true);
|
||||
|
|
22
src/arg.rs
22
src/arg.rs
|
@ -104,7 +104,7 @@ impl Arg {
|
|||
/// # Arg::new("conifg")
|
||||
/// .short("c")
|
||||
/// # ).get_matches();
|
||||
pub fn short(&mut self, s: &'static str) -> &mut Arg {
|
||||
pub fn short(mut self, s: &'static str) -> Arg {
|
||||
self.short = Some(s.trim_left_matches(|c| c == '-')
|
||||
.char_at(0));
|
||||
self
|
||||
|
@ -129,7 +129,7 @@ impl Arg {
|
|||
/// # Arg::new("conifg")
|
||||
/// .long("config")
|
||||
/// # ).get_matches();
|
||||
pub fn long(&mut self, l: &'static str) -> &mut Arg {
|
||||
pub fn long(mut self, l: &'static str) -> Arg {
|
||||
self.long = Some(l.trim_left_matches(|c| c == '-'));
|
||||
self
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ impl Arg {
|
|||
/// # Arg::new("conifg")
|
||||
/// .help("The config file used by the myprog")
|
||||
/// # ).get_matches();
|
||||
pub fn help(&mut self, h: &'static str) -> &mut Arg {
|
||||
pub fn help(mut self, h: &'static str) -> Arg {
|
||||
self.help = Some(h);
|
||||
self
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ impl Arg {
|
|||
/// # Arg::new("conifg")
|
||||
/// .required(true)
|
||||
/// # ).get_matches();
|
||||
pub fn required(&mut self, r: bool) -> &mut Arg {
|
||||
pub fn required(mut self, r: bool) -> Arg {
|
||||
self.required = r;
|
||||
self
|
||||
}
|
||||
|
@ -188,7 +188,7 @@ impl Arg {
|
|||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .mutually_excludes("debug")
|
||||
/// # ).get_matches();
|
||||
pub fn mutually_excludes(&mut self, name: &'static str) -> &mut Arg {
|
||||
pub fn mutually_excludes(mut self, name: &'static str) -> Arg {
|
||||
if let Some(ref mut vec) = self.blacklist {
|
||||
vec.push(name);
|
||||
} else {
|
||||
|
@ -212,7 +212,7 @@ impl Arg {
|
|||
/// .mutually_excludes_all(
|
||||
/// vec!["debug", "input"])
|
||||
/// # ).get_matches();
|
||||
pub fn mutually_excludes_all(&mut self, names: Vec<&'static str>) -> &mut Arg {
|
||||
pub fn mutually_excludes_all(mut self, names: Vec<&'static str>) -> Arg {
|
||||
if let Some(ref mut vec) = self.blacklist {
|
||||
for n in names {
|
||||
vec.push(n);
|
||||
|
@ -235,7 +235,7 @@ impl Arg {
|
|||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .requires("debug")
|
||||
/// # ).get_matches();
|
||||
pub fn requires(&mut self, name: &'static str) -> &mut Arg {
|
||||
pub fn requires(mut self, name: &'static str) -> Arg {
|
||||
if let Some(ref mut vec) = self.requires {
|
||||
vec.push(name);
|
||||
} else {
|
||||
|
@ -258,7 +258,7 @@ impl Arg {
|
|||
/// .requires_all(
|
||||
/// vec!["debug", "input"])
|
||||
/// # ).get_matches();
|
||||
pub fn requires_all(&mut self, names: Vec<&'static str>) -> &mut Arg {
|
||||
pub fn requires_all(mut self, names: Vec<&'static str>) -> Arg {
|
||||
if let Some(ref mut vec) = self.requires {
|
||||
for n in names {
|
||||
vec.push(n);
|
||||
|
@ -283,7 +283,7 @@ impl Arg {
|
|||
/// # Arg::new("conifg")
|
||||
/// .takes_value(true)
|
||||
/// # ).get_matches();
|
||||
pub fn takes_value(&mut self, tv: bool) -> &mut Arg {
|
||||
pub fn takes_value(mut self, tv: bool) -> Arg {
|
||||
assert!(self.index == None);
|
||||
self.takes_value = tv;
|
||||
self
|
||||
|
@ -305,7 +305,7 @@ impl Arg {
|
|||
/// # Arg::new("conifg")
|
||||
/// .index(1)
|
||||
/// # ).get_matches();
|
||||
pub fn index(&mut self, idx: u8) -> &mut Arg {
|
||||
pub fn index(mut self, idx: u8) -> Arg {
|
||||
assert!(self.takes_value == false);
|
||||
if idx < 1 { panic!("Argument index must start at 1"); }
|
||||
self.index = Some(idx);
|
||||
|
@ -329,7 +329,7 @@ impl Arg {
|
|||
/// # Arg::new("debug")
|
||||
/// .multiple(true)
|
||||
/// # ).get_matches();
|
||||
pub fn multiple(&mut self, multi: bool) -> &mut Arg {
|
||||
pub fn multiple(mut self, multi: bool) -> Arg {
|
||||
assert!(self.takes_value == false);
|
||||
assert!(self.index == None);
|
||||
self.multiple = multi;
|
||||
|
|
|
@ -104,7 +104,7 @@ impl ArgMatches {
|
|||
if let Some(ref pos) = self.positionals.get(name) {
|
||||
if let Some(ref v) = pos.value {
|
||||
return Some(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue