From b40e1db7dfa7ab431485fb5b4c70183e43152cbc Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 17 Apr 2016 22:02:44 -0700 Subject: [PATCH] Have CLI take preference over config for verbosity If a CLI option is passed, that trumps any command line configuration, otherwise just do what we did previously. Closes #2588 --- src/bin/cargo.rs | 8 +++++--- src/cargo/core/shell.rs | 21 +++------------------ src/cargo/util/config.rs | 28 +++++++++++++++++++++++++--- tests/test_cargo_run.rs | 20 ++++++++++++++++++++ 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 9e4af20cf..a5e2bf562 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -11,8 +11,10 @@ use std::env; use std::fs; use std::path::PathBuf; +use cargo::core::shell::Verbosity; use cargo::execute_main_without_stdin; -use cargo::util::{self, CliResult, lev_distance, Config, human, CargoResult, ChainError}; +use cargo::util::ChainError; +use cargo::util::{self, CliResult, lev_distance, Config, human, CargoResult}; #[derive(RustcDecodable)] pub struct Flags { @@ -132,7 +134,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { // `cargo -h` so we can go through the normal process of printing the // help message. "" | "help" if flags.arg_args.is_empty() => { - config.shell().set_verbose(true); + config.shell().set_verbosity(Verbosity::Verbose); let args = &["cargo".to_string(), "-h".to_string()]; let r = cargo::call_main_without_stdin(execute, config, USAGE, args, false); @@ -160,7 +162,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { macro_rules! cmd{ ($name:ident) => (if args[1] == stringify!($name).replace("_", "-") { - config.shell().set_verbose(true); + config.shell().set_verbosity(Verbosity::Verbose); let r = cargo::call_main_without_stdin($name::execute, config, $name::USAGE, &args, diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 57876c971..1951cdeba 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -6,7 +6,7 @@ use term::color::{Color, BLACK, RED, GREEN, YELLOW}; use term::{self, Terminal, TerminfoTerminal, color, Attr}; use self::AdequateTerminal::{NoColor, Colored}; -use self::Verbosity::{Verbose, Normal, Quiet}; +use self::Verbosity::{Verbose, Quiet}; use self::ColorConfig::{Auto, Always, Never}; use util::errors::CargoResult; @@ -103,23 +103,8 @@ impl MultiShell { self.err().say_status("warning:", message, YELLOW, false) } - pub fn set_verbosity(&mut self, verbose: bool, quiet: bool) -> CargoResult<()> { - self.verbosity = match (verbose, quiet) { - (true, true) => bail!("cannot set both --verbose and --quiet"), - (true, false) => Verbose, - (false, true) => Quiet, - (false, false) => Normal - }; - Ok(()) - } - - /// shortcut for commands that don't have both --verbose and --quiet - pub fn set_verbose(&mut self, verbose: bool) { - if verbose { - self.verbosity = Verbose; - } else { - self.verbosity = Normal; - } + pub fn set_verbosity(&mut self, verbosity: Verbosity) { + self.verbosity = verbosity; } pub fn set_color_config(&mut self, color: Option<&str>) -> CargoResult<()> { diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 05e53c40d..9f41c56f5 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -277,11 +277,33 @@ impl Config { color: &Option) -> CargoResult<()> { let cfg_verbose = try!(self.get_bool("term.verbose")).map(|v| v.val); let cfg_color = try!(self.get_string("term.color")).map(|v| v.val); - let verbose = verbose.or(cfg_verbose).unwrap_or(false); - let quiet = quiet.unwrap_or(false); let color = color.as_ref().or(cfg_color.as_ref()); - try!(self.shell().set_verbosity(verbose, quiet)); + let verbosity = match (verbose, cfg_verbose, quiet) { + (Some(true), _, None) | + (None, Some(true), None) => Verbosity::Verbose, + + // command line takes precedence over configuration, so ignore the + // configuration. + (None, _, Some(true)) => Verbosity::Quiet, + + // Can't pass both at the same time on the command line regardless + // of configuration. + (Some(true), _, Some(true)) => { + bail!("cannot set both --verbose and --quiet"); + } + + // Can't actually get `Some(false)` as a value from the command + // line, so just ignore them here to appease exhaustiveness checking + // in match statements. + (Some(false), _, _) | + (_, _, Some(false)) | + + (None, Some(false), None) | + (None, None, None) => Verbosity::Normal, + }; + + self.shell().set_verbosity(verbosity); try!(self.shell().set_color_config(color.map(|s| &s[..]))); Ok(()) diff --git a/tests/test_cargo_run.rs b/tests/test_cargo_run.rs index b71073dc9..b1a87c102 100644 --- a/tests/test_cargo_run.rs +++ b/tests/test_cargo_run.rs @@ -70,6 +70,26 @@ test!(simple_quiet_and_verbose { error = ERROR))); }); +test!(quiet_and_verbose_config { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file(".cargo/config", r#" + [term] + verbose = true + "#) + .file("src/main.rs", r#" + fn main() { println!("hello"); } + "#); + + assert_that(p.cargo_process("run").arg("-q"), + execs().with_status(0)); +}); + test!(simple_with_args { let p = project("foo") .file("Cargo.toml", r#" -- 2.39.5