Skip to content

Commit

Permalink
auto merge of #1051 : liigo/cargo/print-version-to-stdout, r=alexcric…
Browse files Browse the repository at this point in the history
…hton

e.g. docopt treat version (and help) as non-fatal error

Fixes #1033
  • Loading branch information
bors committed Dec 15, 2014
2 parents 4b6ebe4 + c79c1ea commit a0e7e7a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl MultiShell {
pub fn set_verbose(&mut self, verbose: bool) {
self.verbose = verbose;
}

pub fn get_verbose(&self) -> bool {
self.verbose
}
}

pub type ShellCallback<'a> = |&mut Shell|:'a -> IoResult<()>;
Expand Down
41 changes: 30 additions & 11 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use serialize::{Decoder, Encoder, Decodable, Encodable, json};
use docopt::Docopt;

use core::{Shell, MultiShell, ShellConfig};
use term::color::{BLACK};
use term::color::{BLACK, RED};

pub use util::{CargoError, CliError, CliResult, human};

Expand Down Expand Up @@ -178,35 +178,54 @@ pub fn shell(verbose: bool) -> MultiShell {
MultiShell::new(out, err, verbose)
}


// `output` print variant error strings to either stderr or stdout.
// For fatal errors, print to stderr;
// and for others, e.g. docopt version info, print to stdout.
fn output(caption: Option<String>, detail: Option<String>,
shell: &mut MultiShell, fatal: bool) {
let std_shell = if fatal {shell.err()} else {shell.out()};
if let Some(caption) = caption {
let color = if fatal {RED} else {BLACK};
let _ = std_shell.say(caption, color);
}
if let Some(detail) = detail {
let _ = std_shell.say(detail, BLACK); // always black
}
}

pub fn handle_error(err: CliError, shell: &mut MultiShell) {
log!(4, "handle_error; err={}", err);

let CliError { error, exit_code, unknown } = err;
let verbose = shell.get_verbose();
let fatal = exit_code != 0; // exit_code == 0 is non-fatal error

if unknown {
let _ = shell.error("An unknown error occurred");
output(Some("An unknown error occurred".to_string()), None, shell, fatal);
} else if error.to_string().len() > 0 {
let _ = shell.error(error.to_string());
output(Some(error.to_string()), None, shell, fatal);
}

if error.cause().is_some() || unknown {
let _ = shell.concise(|shell| {
shell.err().say("\nTo learn more, run the command again with --verbose.", BLACK)
});
if !verbose {
output(None,
Some("\nTo learn more, run the command again with --verbose.".to_string()),
shell, fatal);
}
}

let _ = shell.verbose(|shell| {
if verbose {
if unknown {
let _ = shell.error(error.to_string());
output(Some(error.to_string()), None, shell, fatal);
}
if let Some(detail) = error.detail() {
let _ = shell.err().say(format!("{}", detail), BLACK);
output(None, Some(detail), shell, fatal);
}
if let Some(err) = error.cause() {
let _ = handle_cause(err, shell);
}
Ok(())
});
}

std::os::set_exit_status(exit_code as int);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/test_cargo_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ test!(simple {
assert_that(p.cargo_process("version"),
execs().with_status(0).with_stdout(format!("{}\n",
cargo::version()).as_slice()));

assert_that(p.cargo_process("--version"),
execs().with_status(0).with_stdout(format!("{}\n",
cargo::version()).as_slice()));

})

0 comments on commit a0e7e7a

Please sign in to comment.