Skip to content

Commit

Permalink
Have CLI take preference over config for verbosity
Browse files Browse the repository at this point in the history
If a CLI option is passed, that trumps any command line configuration, otherwise
just do what we did previously.

Closes #2588
  • Loading branch information
alexcrichton committed Apr 18, 2016
1 parent 88e3081 commit b40e1db
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -132,7 +134,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
// `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);
Expand Down Expand Up @@ -160,7 +162,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {

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,
Expand Down
21 changes: 3 additions & 18 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<()> {
Expand Down
28 changes: 25 additions & 3 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,33 @@ impl Config {
color: &Option<String>) -> 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(())
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down

0 comments on commit b40e1db

Please sign in to comment.