Skip to content

Commit

Permalink
Auto merge of #804 - kbknapp:issues-801,694, r=kbknapp
Browse files Browse the repository at this point in the history
Issues 801,694 and tests overhaul
  • Loading branch information
homu committed Jan 3, 2017
2 parents 8e942c2 + 2fc5aca commit ce08391
Show file tree
Hide file tree
Showing 21 changed files with 619 additions and 528 deletions.
182 changes: 34 additions & 148 deletions clap-test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
#[allow(unused_imports, dead_code)]
mod test {
use std::str;
use std::io::Write;
use std::io::{Cursor, Write};

use regex::Regex;

use clap::{App, Arg, SubCommand, ArgGroup};

fn compare<S, S2>(l: S, r: S2) -> bool
where S: AsRef<str>,
S2: AsRef<str> {
let re = Regex::new("\x1b[^m]*m").unwrap();
// Strip out any mismatching \r character on windows that might sneak in on either side
let left = re.replace_all(&l.as_ref().trim().replace("\r", "")[..], "");
let right = re.replace_all(&r.as_ref().trim().replace("\r", "")[..], "");
let b = left == right;
if !b {
println!("");
println!("--> left");
println!("{}", left);
println!("--> right");
println!("{}", right);
println!("--")
}
b
}

pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool {
let mut buf = Cursor::new(Vec::with_capacity(50));
let res = l.get_matches_from_safe(args.split(' ').collect::<Vec<_>>());
let err = res.unwrap_err();
err.write_to(&mut buf).unwrap();
let content = buf.into_inner();
let left = String::from_utf8(content).unwrap();
assert_eq!(stderr, err.use_stderr());
compare(left, right)
}

// Legacy tests from the pyhton script days

pub fn complex_app() -> App<'static, 'static> {
let args = "-o --option=[opt]... 'tests options'
[positional] 'tests positionals'";
Expand Down Expand Up @@ -37,150 +69,4 @@ mod test {
.arg_from_usage("-o --option [scoption]... 'tests options'")
.arg_from_usage("[scpositional] 'tests positionals'"))
}

pub fn check_err_output(a: App, args: &str, out: &str, use_stderr: bool) {
let res = a.get_matches_from_safe(args.split(' ').collect::<Vec<_>>());
let re = Regex::new("\x1b[^m]*m").unwrap();

let mut w = vec![];
let err = res.unwrap_err();
err.write_to(&mut w).unwrap();
let err_s = str::from_utf8(&w).unwrap();
assert_eq!(re.replace_all(err_s, ""), out);
assert_eq!(use_stderr, err.use_stderr());
}

pub fn check_subcommand_help(mut a: App, cmd: &str, out: &str) {
// We call a get_matches method to cause --help and --version to be built
let _ = a.get_matches_from_safe_borrow(vec![""]);
let sc = a.p.subcommands.iter().filter(|s| s.p.meta.name == cmd).next().unwrap();

// Now we check the output of print_help()
let mut help = vec![];
sc.write_help(&mut help).expect("failed to print help");
assert_eq!(str::from_utf8(&help).unwrap(), out);
}

pub fn check_help(mut a: App, out: &str) {
// We call a get_matches method to cause --help and --version to be built
let _ = a.get_matches_from_safe_borrow(vec![""]);

// Now we check the output of print_help()
let mut help = vec![];
a.write_help(&mut help).expect("failed to print help");
assert_eq!(str::from_utf8(&help).unwrap(), out);
}

pub fn check_version(mut a: App, out: &str) {
// We call a get_matches method to cause --help and --version to be built
let _ = a.get_matches_from_safe_borrow(vec![""]);

// Now we check the output of print_version()
let mut ver = vec![];
a.write_version(&mut ver).expect("failed to print help");
assert_eq!(str::from_utf8(&ver).unwrap(), out);
}

pub fn check_complex_output(args: &str, out: &str) {
let mut w = vec![];
let matches = complex_app().get_matches_from(args.split(' ').collect::<Vec<_>>());
if matches.is_present("flag") {
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
} else {
writeln!(w, "flag NOT present").unwrap();
}

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
writeln!(w, "option present {} times with value: {}",matches.occurrences_of("option"), v).unwrap();
}
if let Some(ov) = matches.values_of("option") {
for o in ov {
writeln!(w, "An option: {}", o).unwrap();
}
}
} else {
writeln!(w, "option NOT present").unwrap();
}

if let Some(p) = matches.value_of("positional") {
writeln!(w, "positional present with value: {}", p).unwrap();
} else {
writeln!(w, "positional NOT present").unwrap();
}

if matches.is_present("flag2") {
writeln!(w, "flag2 present").unwrap();
writeln!(w, "option2 present with value of: {}", matches.value_of("long-option-2").unwrap()).unwrap();
writeln!(w, "positional2 present with value of: {}", matches.value_of("positional2").unwrap()).unwrap();
} else {
writeln!(w, "flag2 NOT present").unwrap();
writeln!(w, "option2 maybe present with value of: {}", matches.value_of("long-option-2").unwrap_or("Nothing")).unwrap();
writeln!(w, "positional2 maybe present with value of: {}", matches.value_of("positional2").unwrap_or("Nothing")).unwrap();
}

let _ = match matches.value_of("Option3").unwrap_or("") {
"fast" => writeln!(w, "option3 present quickly"),
"slow" => writeln!(w, "option3 present slowly"),
_ => writeln!(w, "option3 NOT present")
};

let _ = match matches.value_of("positional3").unwrap_or("") {
"vi" => writeln!(w, "positional3 present in vi mode"),
"emacs" => writeln!(w, "positional3 present in emacs mode"),
_ => writeln!(w, "positional3 NOT present")
};

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
writeln!(w, "option present {} times with value: {}",matches.occurrences_of("option"), v).unwrap();
}
if let Some(ov) = matches.values_of("option") {
for o in ov {
writeln!(w, "An option: {}", o).unwrap();
}
}
} else {
writeln!(w, "option NOT present").unwrap();
}

if let Some(p) = matches.value_of("positional") {
writeln!(w, "positional present with value: {}", p).unwrap();
} else {
writeln!(w, "positional NOT present").unwrap();
}
if matches.is_present("subcmd") {
writeln!(w, "subcmd present").unwrap();
if let Some(matches) = matches.subcommand_matches("subcmd") {
if matches.is_present("flag") {
writeln!(w, "flag present {} times", matches.occurrences_of("flag")).unwrap();
} else {
writeln!(w, "flag NOT present").unwrap();
}

if matches.is_present("option") {
if let Some(v) = matches.value_of("option") {
writeln!(w, "scoption present with value: {}", v).unwrap();
}
if let Some(ov) = matches.values_of("option") {
for o in ov {
writeln!(w, "An scoption: {}", o).unwrap();
}
}
} else {
writeln!(w, "scoption NOT present").unwrap();
}

if let Some(p) = matches.value_of("scpositional") {
writeln!(w, "scpositional present with value: {}", p).unwrap();
}
}
} else {
writeln!(w, "subcmd NOT present").unwrap();
}

let res = str::from_utf8(&w).unwrap();
assert_eq!(res, out);
}

}
}
21 changes: 20 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,12 @@ impl<'a, 'b> App<'a, 'b> {
/// [`io::stdout()`]: https://doc.rust-lang.org/std/io/fn.stdout.html
/// [`BufWriter`]: https://doc.rust-lang.org/std/io/struct.BufWriter.html
pub fn print_help(&mut self) -> ClapResult<()> {
// If there are global arguments, or settings we need to propgate them down to subcommands
// before parsing incase we run into a subcommand
self.p.propogate_globals();
self.p.propogate_settings();
self.p.derive_display_order();

self.p.create_help_and_version();
let out = io::stdout();
let mut buf_w = BufWriter::new(out.lock());
Expand All @@ -1022,7 +1028,14 @@ impl<'a, 'b> App<'a, 'b> {
/// app.write_help(&mut out).expect("failed to write to stdout");
/// ```
/// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
pub fn write_help<W: Write>(&self, w: &mut W) -> ClapResult<()> {
pub fn write_help<W: Write>(&mut self, w: &mut W) -> ClapResult<()> {
// If there are global arguments, or settings we need to propgate them down to subcommands
// before parsing incase we run into a subcommand
self.p.propogate_globals();
self.p.propogate_settings();
self.p.derive_display_order();

self.p.create_help_and_version();
Help::write_app_help(w, self)
}

Expand Down Expand Up @@ -1351,6 +1364,12 @@ impl<'a, 'b> App<'a, 'b> {
return Err(e);
}

if self.p.is_set(AppSettings::PropagateGlobalValuesDown) {
for a in &self.p.global_args {
matcher.propagate(a.name);
}
}

Ok(matcher.into())
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct Parser<'a, 'b>
#[doc(hidden)]
pub subcommands: Vec<App<'a, 'b>>,
groups: HashMap<&'a str, ArgGroup<'a>>,
global_args: Vec<Arg<'a, 'b>>,
pub global_args: Vec<Arg<'a, 'b>>,
overrides: Vec<&'b str>,
help_short: Option<char>,
version_short: Option<char>,
Expand Down
106 changes: 73 additions & 33 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,40 @@ use std::str::FromStr;
use std::ops::BitOr;

bitflags! {
flags Flags: u32 {
const SC_NEGATE_REQS = 0b00000000000000000000000000000001,
const SC_REQUIRED = 0b00000000000000000000000000000010,
const A_REQUIRED_ELSE_HELP = 0b00000000000000000000000000000100,
const GLOBAL_VERSION = 0b00000000000000000000000000001000,
const VERSIONLESS_SC = 0b00000000000000000000000000010000,
const UNIFIED_HELP = 0b00000000000000000000000000100000,
const WAIT_ON_ERROR = 0b00000000000000000000000001000000,
const SC_REQUIRED_ELSE_HELP= 0b00000000000000000000000010000000,
const NEEDS_LONG_HELP = 0b00000000000000000000000100000000,
const NEEDS_LONG_VERSION = 0b00000000000000000000001000000000,
const NEEDS_SC_HELP = 0b00000000000000000000010000000000,
const DISABLE_VERSION = 0b00000000000000000000100000000000,
const HIDDEN = 0b00000000000000000001000000000000,
const TRAILING_VARARG = 0b00000000000000000010000000000000,
const NO_BIN_NAME = 0b00000000000000000100000000000000,
const ALLOW_UNK_SC = 0b00000000000000001000000000000000,
const UTF8_STRICT = 0b00000000000000010000000000000000,
const UTF8_NONE = 0b00000000000000100000000000000000,
const LEADING_HYPHEN = 0b00000000000001000000000000000000,
const NO_POS_VALUES = 0b00000000000010000000000000000000,
const NEXT_LINE_HELP = 0b00000000000100000000000000000000,
const DERIVE_DISP_ORDER = 0b00000000001000000000000000000000,
const COLORED_HELP = 0b00000000010000000000000000000000,
const COLOR_ALWAYS = 0b00000000100000000000000000000000,
const COLOR_AUTO = 0b00000001000000000000000000000000,
const COLOR_NEVER = 0b00000010000000000000000000000000,
const DONT_DELIM_TRAIL = 0b00000100000000000000000000000000,
const ALLOW_NEG_NUMS = 0b00001000000000000000000000000000,
const LOW_INDEX_MUL_POS = 0b00010000000000000000000000000000,
const DISABLE_HELP_SC = 0b00100000000000000000000000000000,
const DONT_COLLAPSE_ARGS = 0b01000000000000000000000000000000,
const ARGS_NEGATE_SCS = 0b10000000000000000000000000000000,
flags Flags: u64 {
const SC_NEGATE_REQS = 0b000000000000000000000000000000001,
const SC_REQUIRED = 0b000000000000000000000000000000010,
const A_REQUIRED_ELSE_HELP = 0b000000000000000000000000000000100,
const GLOBAL_VERSION = 0b000000000000000000000000000001000,
const VERSIONLESS_SC = 0b000000000000000000000000000010000,
const UNIFIED_HELP = 0b000000000000000000000000000100000,
const WAIT_ON_ERROR = 0b000000000000000000000000001000000,
const SC_REQUIRED_ELSE_HELP= 0b000000000000000000000000010000000,
const NEEDS_LONG_HELP = 0b000000000000000000000000100000000,
const NEEDS_LONG_VERSION = 0b000000000000000000000001000000000,
const NEEDS_SC_HELP = 0b000000000000000000000010000000000,
const DISABLE_VERSION = 0b000000000000000000000100000000000,
const HIDDEN = 0b000000000000000000001000000000000,
const TRAILING_VARARG = 0b000000000000000000010000000000000,
const NO_BIN_NAME = 0b000000000000000000100000000000000,
const ALLOW_UNK_SC = 0b000000000000000001000000000000000,
const UTF8_STRICT = 0b000000000000000010000000000000000,
const UTF8_NONE = 0b000000000000000100000000000000000,
const LEADING_HYPHEN = 0b000000000000001000000000000000000,
const NO_POS_VALUES = 0b000000000000010000000000000000000,
const NEXT_LINE_HELP = 0b000000000000100000000000000000000,
const DERIVE_DISP_ORDER = 0b000000000001000000000000000000000,
const COLORED_HELP = 0b000000000010000000000000000000000,
const COLOR_ALWAYS = 0b000000000100000000000000000000000,
const COLOR_AUTO = 0b000000001000000000000000000000000,
const COLOR_NEVER = 0b000000010000000000000000000000000,
const DONT_DELIM_TRAIL = 0b000000100000000000000000000000000,
const ALLOW_NEG_NUMS = 0b000001000000000000000000000000000,
const LOW_INDEX_MUL_POS = 0b000010000000000000000000000000000,
const DISABLE_HELP_SC = 0b000100000000000000000000000000000,
const DONT_COLLAPSE_ARGS = 0b001000000000000000000000000000000,
const ARGS_NEGATE_SCS = 0b010000000000000000000000000000000,
const PROPAGATE_VALS_DOWN = 0b100000000000000000000000000000000,
}
}

Expand Down Expand Up @@ -88,6 +89,7 @@ impl AppFlags {
NeedsLongVersion => NEEDS_LONG_VERSION,
NeedsSubcommandHelp => NEEDS_SC_HELP,
NoBinaryName => NO_BIN_NAME,
PropagateGlobalValuesDown=> PROPAGATE_VALS_DOWN,
StrictUtf8 => UTF8_STRICT,
SubcommandsNegateReqs => SC_NEGATE_REQS,
SubcommandRequired => SC_REQUIRED,
Expand Down Expand Up @@ -512,6 +514,44 @@ pub enum AppSettings {
/// ```
NextLineHelp,

/// Specifies that the parser should propagate global arg's values down through any *used* child
/// subcommands. Meaning, if a subcommand wasn't used, the values won't be propagated down to
/// said subcommand.
///
/// **NOTE:** Values are only propagated *down* through futher child commands, not up
///
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg, AppSettings, SubCommand};
/// let m = App::new("myprog")
/// .setting(AppSettings::PropagateGlobalValuesDown)
/// .arg(Arg::from_usage("[cmd] 'command to run'")
/// .global(true))
/// .subcommand(SubCommand::with_name("foo"))
/// .get_matches_from(vec!["myprog", "set", "foo"]);
///
/// assert_eq!(m.value_of("cmd"), Some("set"));
///
/// let sub_m = m.subcommand_matches("foo").unwrap();
/// assert_eq!(sub_m.value_of("cmd"), Some("set"));
/// ```
/// Now doing the same thing, but *not* using any subcommands will result in the value not being
/// propagated down.
/// ```rust
/// # use clap::{App, Arg, AppSettings};
/// let m = App::new("myprog")
/// .setting(AppSettings::PropagateGlobalValuesDown)
/// .global_arg(Arg::from_usage("<cmd> 'command to run'"))
/// .subcommand(SubCommand::with_name("foo"))
/// .get_matches_from(vec!["myprog", "set"]);
///
/// assert_eq!(m.value_of("cmd"), Some("set"));
///
/// assert!(m.subcommand_matches("foo").is_none());
/// ```
PropagateGlobalValuesDown,

/// Allows [`SubCommand`]s to override all requirements of the parent command.
/// For example if you had a subcommand or top level application with a required argument
/// that is only required as long as there is no subcommand present,
Expand Down
Loading

0 comments on commit ce08391

Please sign in to comment.