Merge branch 'dev'

This commit is contained in:
Kevin K 2015-03-03 22:42:06 -05:00
commit d084a40f4a
3 changed files with 74 additions and 70 deletions

View file

@ -46,7 +46,7 @@ pub struct App {
flags: HashMap<&'static str, FlagArg>, flags: HashMap<&'static str, FlagArg>,
opts: HashMap<&'static str, OptArg>, opts: HashMap<&'static str, OptArg>,
positionals_idx: BTreeMap<u8, PosArg>, positionals_idx: BTreeMap<u8, PosArg>,
positionals_name: HashMap<&'static str, PosArg>, // positionals_name: HashMap<&'static str, PosArg>,
needs_long_help: bool, needs_long_help: bool,
needs_long_version: bool, needs_long_version: bool,
needs_short_help: bool, needs_short_help: bool,
@ -78,7 +78,7 @@ impl App {
flags: HashMap::new(), flags: HashMap::new(),
opts: HashMap::new(), opts: HashMap::new(),
positionals_idx: BTreeMap::new(), positionals_idx: BTreeMap::new(),
positionals_name: HashMap::new(), // positionals_name: HashMap::new(),
needs_long_version: true, needs_long_version: true,
needs_long_help: true, needs_long_help: true,
needs_short_help: true, needs_short_help: true,
@ -101,7 +101,7 @@ impl App {
/// .author("Kevin <kbknapp@gmail.com>") /// .author("Kevin <kbknapp@gmail.com>")
/// # .get_matches(); /// # .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.author = Some(a);
self self
} }
@ -116,7 +116,7 @@ impl App {
/// .about("Does really amazing things to great people") /// .about("Does really amazing things to great people")
/// # .get_matches(); /// # .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.about = Some(a);
self self
} }
@ -131,7 +131,7 @@ impl App {
/// .version("v0.1.24") /// .version("v0.1.24")
/// # .get_matches(); /// # .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.version = Some(v);
self self
} }
@ -149,7 +149,7 @@ impl App {
/// ) /// )
/// # .get_matches(); /// # .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) { if self.arg_list.contains(a.name) {
panic!("Argument name must be unique, \"{}\" is already in use", a.name); panic!("Argument name must be unique, \"{}\" is already in use", a.name);
} else { } else {
@ -173,21 +173,21 @@ impl App {
self.required.insert(a.name); self.required.insert(a.name);
} }
if let Some(i) = a.index { if let Some(i) = a.index {
self.positionals_name.insert(a.name, PosArg { // self.positionals_name.insert(a.name, PosArg {
name: a.name, // name: a.name,
index: i, // index: i,
required: a.required, // required: a.required,
help: a.help, // help: a.help,
blacklist: a.blacklist.clone(), // blacklist: a.blacklist,
requires: a.requires.clone(), // requires: a.requires,
value: None // value: None
}); // });
self.positionals_idx.insert(i, PosArg { self.positionals_idx.insert(i, PosArg {
name: a.name, name: a.name,
index: i, index: i,
required: a.required, required: a.required,
blacklist: a.blacklist.clone(), blacklist: a.blacklist,
requires: a.requires.clone(), requires: a.requires,
help: a.help, help: a.help,
value: None value: None
}); });
@ -199,9 +199,9 @@ impl App {
name: a.name, name: a.name,
short: a.short, short: a.short,
long: a.long, long: a.long,
blacklist: a.blacklist.clone(), blacklist: a.blacklist,
help: a.help, help: a.help,
requires: a.requires.clone(), requires: a.requires,
required: a.required, required: a.required,
value: None value: None
}); });
@ -232,9 +232,9 @@ impl App {
short: a.short, short: a.short,
long: a.long, long: a.long,
help: a.help, help: a.help,
blacklist: a.blacklist.clone(), blacklist: a.blacklist,
multiple: a.multiple, multiple: a.multiple,
requires: a.requires.clone(), requires: a.requires,
occurrences: 1 occurrences: 1
}); });
} }
@ -252,9 +252,9 @@ impl App {
/// Arg::new("debug").short("d")]) /// Arg::new("debug").short("d")])
/// # .get_matches(); /// # .get_matches();
/// ``` /// ```
pub fn args(&mut self, args: Vec<&Arg>) -> &mut App { pub fn args(mut self, args: Vec<Arg>) -> App {
for arg in args.iter() { for arg in args.into_iter() {
self.arg(arg); self = self.arg(arg);
} }
self self
} }
@ -263,10 +263,10 @@ impl App {
unsafe { libc::exit(0); } unsafe { libc::exit(0); }
} }
fn report_error(&self, msg: &String, help: bool, quit: bool) { fn report_error(&self, msg: String, help: bool, quit: bool) {
println!("{}", msg); println!("{}", msg);
if help { self.print_help(); } if help { self.print_help(); }
if quit {self.exit(); } if quit { env::set_exit_status(1); self.exit(); }
} }
fn print_help(&self) { fn print_help(&self) {
@ -275,10 +275,10 @@ impl App {
let mut pos = false; let mut pos = false;
let mut opts = false; let mut opts = false;
if let Some(ref author) = self.author { if let Some(author) = self.author {
println!("{}", author); println!("{}", author);
} }
if let Some(ref about) = self.about { if let Some(about) = self.about {
println!("{}", about); println!("{}", about);
} }
println!(""); println!("");
@ -286,24 +286,24 @@ impl App {
print!("\t{} {} {} {}", self.name, print!("\t{} {} {} {}", self.name,
if ! self.flags.is_empty() {flags = true; "[FLAGS]"} else {""}, if ! self.flags.is_empty() {flags = true; "[FLAGS]"} else {""},
if ! self.opts.is_empty() {opts = true; "[OPTIONS]"} 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 { if flags || opts || pos {
println!(""); println!("");
} }
if flags { if flags {
println!(""); println!("");
println!("FLAGS:"); println!("FLAGS:");
for (_, v) in self.flags.iter() { for v in self.flags.values() {
println!("\t{}{}\t{}", println!("\t{}{}\t{}",
if let Some(ref s) = v.short{format!("-{}",s)}else{format!(" ")}, if let Some(s) = v.short{format!("-{}",s)}else{format!(" ")},
if let Some(ref l) = v.long {format!(",--{}",l)}else {format!(" \t")}, if let Some(l) = v.long {format!(",--{}",l)}else {format!(" \t")},
if let Some(ref h) = v.help {*h} else {" "} ); if let Some(h) = v.help {h} else {" "} );
} }
} }
if opts { if opts {
println!(""); println!("");
println!("OPTIONS:"); println!("OPTIONS:");
for (_, v) in self.opts.iter() { for v in self.opts.values() {
println!("\t{}{}{}\t\t{}", println!("\t{}{}{}\t\t{}",
if let Some(ref s) = v.short{format!("-{}",s)}else{format!(" ")}, if let Some(ref s) = v.short{format!("-{}",s)}else{format!(" ")},
if let Some(ref l) = v.long {format!(",--{}",l)}else {format!(" ")}, if let Some(ref l) = v.long {format!(",--{}",l)}else {format!(" ")},
@ -314,9 +314,9 @@ impl App {
if pos { if pos {
println!(""); println!("");
println!("POSITIONAL ARGUMENTS:"); println!("POSITIONAL ARGUMENTS:");
for (_, v) in self.positionals_idx.iter() { for v in self.positionals_idx.values() {
println!("\t{}\t\t\t{}", v.name, println!("\t{}\t\t\t{}", v.name,
if let Some(ref h) = v.help {*h} else {" "} ); if let Some(h) = v.help {h} else {" "} );
} }
} }
@ -324,7 +324,7 @@ impl App {
} }
fn print_version(&self, quit: bool) { fn print_version(&self, quit: bool) {
println!("{} {}", self.name, if let Some(ref v) = self.version {*v} else {""} ); println!("{} {}", self.name, if let Some(v) = self.version {v} else {""} );
if quit { self.exit(); } if quit { self.exit(); }
} }
@ -358,7 +358,7 @@ impl App {
if let Some(ref l) = v.long { if let Some(ref l) = v.long {
if *l == arg { if *l == arg {
if self.blacklist.contains(k) { if self.blacklist.contains(k) {
self.report_error(&format!("The argument --{} is mutually exclusive with one or more other arguments", arg), self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
false, true); false, true);
} }
matches.opts.insert(k, OptArg{ matches.opts.insert(k, OptArg{
@ -390,7 +390,7 @@ impl App {
} }
if ! multi { if ! multi {
if self.blacklist.contains(k) { if self.blacklist.contains(k) {
self.report_error(&format!("The argument --{} is mutually exclusive with one or more other arguments", arg), self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg),
false, true); false, true);
} }
matches.flags.insert(k, FlagArg{ matches.flags.insert(k, FlagArg{
@ -430,7 +430,7 @@ impl App {
if ! found { if ! found {
self.report_error( self.report_error(
&format!("Argument --{} isn't valid", arg), format!("Argument --{} isn't valid", arg),
false, true); false, true);
} }
None None
@ -444,7 +444,7 @@ impl App {
self.check_for_help_and_version(c); self.check_for_help_and_version(c);
if ! self.parse_single_short_flag(matches, c) { if ! self.parse_single_short_flag(matches, c) {
self.report_error( self.report_error(
&format!("Argument -{} isn't valid",c), format!("Argument -{} isn't valid",c),
false, true); false, true);
} }
} }
@ -463,7 +463,7 @@ impl App {
} }
self.report_error( self.report_error(
&format!("Argument -{} isn't valid",arg_c), format!("Argument -{} isn't valid",arg_c),
false, true); false, true);
} }
@ -478,7 +478,7 @@ impl App {
if !matches.flags.contains_key(k) { if !matches.flags.contains_key(k) {
if self.blacklist.contains(k) { if self.blacklist.contains(k) {
self.report_error(&format!("The argument -{} is mutually exclusive with one or more other arguments", arg), self.report_error(format!("The argument -{} is mutually exclusive with one or more other arguments", arg),
false, true); false, true);
} }
matches.flags.insert(k, FlagArg{ matches.flags.insert(k, FlagArg{
@ -526,7 +526,7 @@ impl App {
for name in self.blacklist.iter() { for name in self.blacklist.iter() {
for (k, v) in matches.flags.iter() { for (k, v) in matches.flags.iter() {
if k == name { if k == name {
self.report_error(&format!("The argument {} is mutually exclusive with one or more other arguments", self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments",
if let Some(s) = v.short { if let Some(s) = v.short {
format!("-{}", s) format!("-{}", s)
} else if let Some(l) = v.long { } else if let Some(l) = v.long {
@ -539,7 +539,7 @@ impl App {
} }
for (k, v) in matches.opts.iter() { for (k, v) in matches.opts.iter() {
if k == name { if k == name {
self.report_error(&format!("The argument {} is mutually exclusive with one or more other arguments", self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments",
if let Some(s) = v.short { if let Some(s) = v.short {
format!("-{}", s) format!("-{}", s)
} else if let Some(l) = v.long { } else if let Some(l) = v.long {
@ -552,7 +552,7 @@ impl App {
} }
for (k, v) in matches.positionals.iter() { for (k, v) in matches.positionals.iter() {
if k == name { if k == name {
self.report_error(&format!("The argument \"{}\" is mutually exclusive with one or more other arguments",v.name), self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments",v.name),
false, true); false, true);
} }
} }
@ -560,9 +560,7 @@ impl App {
} }
} }
pub fn get_matches(&mut self) -> ArgMatches { fn create_help_and_version(&mut self) {
let mut matches = ArgMatches::new(self);
if self.needs_long_help { if self.needs_long_help {
self.flags.insert("clap_help", FlagArg{ self.flags.insert("clap_help", FlagArg{
name: "clap_help", name: "clap_help",
@ -587,18 +585,24 @@ impl App {
occurrences: 1 occurrences: 1
}); });
} }
}
pub fn get_matches(mut self) -> ArgMatches {
let mut matches = ArgMatches::new(&self);
self.create_help_and_version();
// let mut needs_val = false; // let mut needs_val = false;
let mut needs_val_of: Option<&'static str> = None; let mut needs_val_of: Option<&'static str> = None;
let mut pos_counter = 1; let mut pos_counter = 1;
for arg in env::args().collect::<Vec<String>>().tail() { for arg in env::args().collect::<Vec<_>>().tail() {
let arg_slice = arg.as_slice(); let arg_slice = arg.as_slice();
let mut skip = false; let mut skip = false;
if let Some(ref nvo) = needs_val_of { if let Some(nvo) = needs_val_of {
if let Some(ref opt) = self.opts.get(nvo) { if let Some(ref opt) = self.opts.get(nvo) {
if self.blacklist.contains(opt.name) { if self.blacklist.contains(opt.name) {
self.report_error( self.report_error(
&format!("The argument {} is mutually exclusive with one or more other arguments", format!("The argument {} is mutually exclusive with one or more other arguments",
if let Some(long) = opt.long { if let Some(long) = opt.long {
format!("--{}",long) format!("--{}",long)
}else{ }else{
@ -651,14 +655,14 @@ impl App {
} else { } else {
// Positional // 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( self.report_error(
&format!("Found positional argument {}, but {} doesn't accept any", arg, self.name), format!("Found positional argument {}, but {} doesn't accept any", arg, self.name),
false, true); false, true);
} }
if let Some(ref p) = self.positionals_idx.get(&pos_counter) { if let Some(ref p) = self.positionals_idx.get(&pos_counter) {
if self.blacklist.contains(p.name) { if self.blacklist.contains(p.name) {
self.report_error(&format!("The argument \"{}\" is mutually exclusive with one or more other arguments", arg), self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments", arg),
false, true); false, true);
} }
matches.positionals.insert(p.name, PosArg{ matches.positionals.insert(p.name, PosArg{
@ -692,7 +696,7 @@ impl App {
} }
pos_counter += 1; pos_counter += 1;
} else { } else {
self.report_error(&format!("Positional argument \"{}\" was found, but {} wasn't expecting any", arg, self.name), false, true); self.report_error(format!("Positional argument \"{}\" was found, but {} wasn't expecting any", arg, self.name), false, true);
} }
} }
} }
@ -700,13 +704,13 @@ impl App {
match needs_val_of { match needs_val_of {
Some(ref a) => { Some(ref a) => {
self.report_error( self.report_error(
&format!("Argument \"{}\" requires a value but none was supplied", a), format!("Argument \"{}\" requires a value but none was supplied", a),
false, true); false, true);
} }
_ => {} _ => {}
} }
if ! self.required.is_empty() { if ! self.required.is_empty() {
self.report_error(&"One or more required arguments were not supplied".to_string(), self.report_error("One or more required arguments were not supplied".to_string(),
false, true); false, true);
} }

View file

@ -104,7 +104,7 @@ impl Arg {
/// # Arg::new("conifg") /// # Arg::new("conifg")
/// .short("c") /// .short("c")
/// # ).get_matches(); /// # ).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 == '-') self.short = Some(s.trim_left_matches(|c| c == '-')
.char_at(0)); .char_at(0));
self self
@ -129,7 +129,7 @@ impl Arg {
/// # Arg::new("conifg") /// # Arg::new("conifg")
/// .long("config") /// .long("config")
/// # ).get_matches(); /// # ).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.long = Some(l.trim_left_matches(|c| c == '-'));
self self
} }
@ -146,7 +146,7 @@ impl Arg {
/// # Arg::new("conifg") /// # Arg::new("conifg")
/// .help("The config file used by the myprog") /// .help("The config file used by the myprog")
/// # ).get_matches(); /// # ).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.help = Some(h);
self self
} }
@ -169,7 +169,7 @@ impl Arg {
/// # Arg::new("conifg") /// # Arg::new("conifg")
/// .required(true) /// .required(true)
/// # ).get_matches(); /// # ).get_matches();
pub fn required(&mut self, r: bool) -> &mut Arg { pub fn required(mut self, r: bool) -> Arg {
self.required = r; self.required = r;
self self
} }
@ -188,7 +188,7 @@ impl Arg {
/// # let myprog = App::new("myprog").arg(Arg::new("conifg") /// # let myprog = App::new("myprog").arg(Arg::new("conifg")
/// .mutually_excludes("debug") /// .mutually_excludes("debug")
/// # ).get_matches(); /// # ).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 { if let Some(ref mut vec) = self.blacklist {
vec.push(name); vec.push(name);
} else { } else {
@ -212,7 +212,7 @@ impl Arg {
/// .mutually_excludes_all( /// .mutually_excludes_all(
/// vec!["debug", "input"]) /// vec!["debug", "input"])
/// # ).get_matches(); /// # ).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 { if let Some(ref mut vec) = self.blacklist {
for n in names { for n in names {
vec.push(n); vec.push(n);
@ -235,7 +235,7 @@ impl Arg {
/// # let myprog = App::new("myprog").arg(Arg::new("conifg") /// # let myprog = App::new("myprog").arg(Arg::new("conifg")
/// .requires("debug") /// .requires("debug")
/// # ).get_matches(); /// # ).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 { if let Some(ref mut vec) = self.requires {
vec.push(name); vec.push(name);
} else { } else {
@ -258,7 +258,7 @@ impl Arg {
/// .requires_all( /// .requires_all(
/// vec!["debug", "input"]) /// vec!["debug", "input"])
/// # ).get_matches(); /// # ).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 { if let Some(ref mut vec) = self.requires {
for n in names { for n in names {
vec.push(n); vec.push(n);
@ -283,7 +283,7 @@ impl Arg {
/// # Arg::new("conifg") /// # Arg::new("conifg")
/// .takes_value(true) /// .takes_value(true)
/// # ).get_matches(); /// # ).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); assert!(self.index == None);
self.takes_value = tv; self.takes_value = tv;
self self
@ -305,7 +305,7 @@ impl Arg {
/// # Arg::new("conifg") /// # Arg::new("conifg")
/// .index(1) /// .index(1)
/// # ).get_matches(); /// # ).get_matches();
pub fn index(&mut self, idx: u8) -> &mut Arg { pub fn index(mut self, idx: u8) -> Arg {
assert!(self.takes_value == false); assert!(self.takes_value == false);
if idx < 1 { panic!("Argument index must start at 1"); } if idx < 1 { panic!("Argument index must start at 1"); }
self.index = Some(idx); self.index = Some(idx);
@ -329,7 +329,7 @@ impl Arg {
/// # Arg::new("debug") /// # Arg::new("debug")
/// .multiple(true) /// .multiple(true)
/// # ).get_matches(); /// # ).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.takes_value == false);
assert!(self.index == None); assert!(self.index == None);
self.multiple = multi; self.multiple = multi;