let (cmd, args) = match process(os::args()) {
Ok((cmd, args)) => (cmd, args),
- Err(err) => return handle_error(err, &mut shell(), false)
+ Err(err) => return handle_error(err, &mut shell(false))
};
match cmd.as_slice() {
match command {
Ok(ExitStatus(0)) => (),
Ok(ExitStatus(i)) | Ok(ExitSignal(i)) => {
- handle_error(CliError::new("", i as uint), &mut shell(), false)
+ handle_error(CliError::new("", i as uint), &mut shell(false))
}
- Err(_) => handle_error(CliError::new("No such subcommand", 127), &mut shell(), false)
+ Err(_) => handle_error(CliError::new("No such subcommand", 127), &mut shell(false))
}
}
}
pub struct MultiShell {
out: Shell,
- err: Shell
+ err: Shell,
+ verbose: bool
}
+pub type Callback<'a> = |&mut MultiShell|:'a -> IoResult<()>;
+
impl MultiShell {
- pub fn new(out: Shell, err: Shell) -> MultiShell {
- MultiShell { out: out, err: err }
+ pub fn new(out: Shell, err: Shell, verbose: bool) -> MultiShell {
+ MultiShell { out: out, err: err, verbose: verbose }
}
pub fn out<'a>(&'a mut self) -> &'a mut Shell {
self.out().say_status(status, message, GREEN)
}
+ pub fn verbose(&mut self, callback: Callback) -> IoResult<()> {
+ if self.verbose { return callback(self) }
+ Ok(())
+ }
+
+ pub fn concise(&mut self, callback: Callback) -> IoResult<()> {
+ if !self.verbose { return callback(self) }
+ Ok(())
+ }
+
pub fn error<T: ToStr>(&mut self, message: T) -> IoResult<()> {
self.err().say(message, RED)
}
}
-
+pub type ShellCallback<'a> = |&mut Shell|:'a -> IoResult<()>;
impl Shell {
pub fn create(out: Box<Writer>, config: ShellConfig) -> Shell {
}
}
- pub fn verbose(&mut self,
- callback: |&mut Shell| -> IoResult<()>) -> IoResult<()> {
- if self.config.verbose {
- return callback(self)
- }
+ pub fn verbose(&mut self, callback: ShellCallback) -> IoResult<()> {
+ if self.config.verbose { return callback(self) }
+ Ok(())
+ }
+ pub fn concise(&mut self, callback: ShellCallback) -> IoResult<()> {
+ if !self.config.verbose { return callback(self) }
Ok(())
}
V: Encodable<json::Encoder<'a>, io::IoError>>(
callback: |&[String], &mut MultiShell| -> CliResult<Option<V>>) {
- let mut shell = shell();
match global_flags() {
- Err(e) => handle_error(e, &mut shell, true),
+ Err(e) => handle_error(e, &mut shell(false)),
Ok(val) => {
+ let mut shell = shell(val.verbose);
+
if val.help {
let (desc, options) = hammer::usage::<T>(true);
let (_, options) = hammer::usage::<GlobalFlags>(false);
print!("{}", options);
} else {
- process_executed(callback(val.rest.as_slice(), &mut shell), val, &mut shell)
+ process_executed(callback(val.rest.as_slice(), &mut shell), &mut shell)
}
}
}
pub fn process_executed<'a,
T: Encodable<json::Encoder<'a>, io::IoError>>(
result: CliResult<Option<T>>,
- flags: GlobalFlags, shell: &mut MultiShell)
+ shell: &mut MultiShell)
{
match result {
- Err(e) => handle_error(e, shell, flags.verbose),
+ Err(e) => handle_error(e, shell),
Ok(encodable) => {
encodable.map(|encodable| {
let encoded = json::Encoder::str_encode(&encodable);
}
}
-pub fn shell() -> MultiShell {
+pub fn shell(verbose: bool) -> MultiShell {
let tty = stderr_raw().isatty();
let stderr = box stderr() as Box<Writer>;
- let config = ShellConfig { color: true, verbose: false, tty: tty };
+ let config = ShellConfig { color: true, verbose: verbose, tty: tty };
let err = Shell::create(stderr, config);
let tty = stdout_raw().isatty();
let stdout = box stdout() as Box<Writer>;
- let config = ShellConfig { color: true, verbose: false, tty: tty };
+ let config = ShellConfig { color: true, verbose: verbose, tty: tty };
let out = Shell::create(stdout, config);
- MultiShell::new(out, err)
+ MultiShell::new(out, err, verbose)
}
-pub fn handle_error(err: CliError, shell: &mut MultiShell, verbose: bool) {
+pub fn handle_error(err: CliError, shell: &mut MultiShell) {
log!(4, "handle_error; err={}", err);
let CliError { error, exit_code, unknown, .. } = err;
let _ = shell.error(error.to_str());
}
- if unknown && !verbose {
- let _ = shell.err().say("\nTo learn more, run the command again with --verbose.", BLACK);
+ if unknown {
+ let _ = shell.concise(|shell| {
+ shell.err().say("\nTo learn more, run the command again with --verbose.", BLACK)
+ });
}
- if verbose {
- handle_cause(error, shell);
- }
+ let _ = shell.verbose(|shell| {
+ let _ = handle_cause(error, shell);
+ Ok(())
+ });
std::os::set_exit_status(exit_code as int);
}