diff --git a/substrate/bin/node/cli/src/command.rs b/substrate/bin/node/cli/src/command.rs index 91c6298c9f..81a22011b5 100644 --- a/substrate/bin/node/cli/src/command.rs +++ b/substrate/bin/node/cli/src/command.rs @@ -63,8 +63,6 @@ impl SubstrateCli for Cli { /// Parse command line arguments into service configuration. pub fn run() -> Result<()> { - sc_cli::reset_signal_pipe_handler()?; - let cli = Cli::from_args(); match &cli.subcommand { diff --git a/substrate/client/cli/src/lib.rs b/substrate/client/cli/src/lib.rs index e723573bc2..2b26259b92 100644 --- a/substrate/client/cli/src/lib.rs +++ b/substrate/client/cli/src/lib.rs @@ -129,7 +129,27 @@ pub trait SubstrateCli: Sized { AppSettings::SubcommandsNegateReqs, ]); - ::from_clap(&app.get_matches_from(iter)) + let matches = match app.get_matches_from_safe(iter) { + Ok(matches) => matches, + Err(mut e) => { + // To support pipes, we can not use `writeln!` as any error + // results in a "broken pipe" error. + // + // Instead we write directly to `stdout` and ignore any error + // as we exit afterwards anyway. + e.message.extend("\n".chars()); + + if e.use_stderr() { + let _ = std::io::stderr().write_all(e.message.as_bytes()); + std::process::exit(1); + } else { + let _ = std::io::stdout().write_all(e.message.as_bytes()); + std::process::exit(0); + } + }, + }; + + ::from_clap(&matches) } /// Helper function used to parse the command line arguments. This is the equivalent of @@ -264,21 +284,3 @@ fn kill_color(s: &str) -> String { } RE.replace_all(s, "").to_string() } - -/// Reset the signal pipe (`SIGPIPE`) handler to the default one provided by the system. -/// This will end the program on `SIGPIPE` instead of panicking. -/// -/// This should be called before calling any cli method or printing any output. -pub fn reset_signal_pipe_handler() -> Result<()> { - #[cfg(target_family = "unix")] - { - use nix::sys::signal; - - unsafe { - signal::signal(signal::Signal::SIGPIPE, signal::SigHandler::SigDfl) - .map_err(|e| Error::Other(e.to_string()))?; - } - } - - Ok(()) -}