diff --git a/derive/src/help.rs b/derive/src/help.rs index a606ecf..77af149 100644 --- a/derive/src/help.rs +++ b/derive/src/help.rs @@ -74,27 +74,27 @@ pub fn help_string( } let options = if !options.is_empty() { - quote!(::uutils_args::internal::print_flags(&mut w, #indent, #width, [#(#options),*])?;) + quote!(::uutils_args::internal::print_flags(&mut w, #indent, #width, [#(#options),*]);) } else { quote!() }; quote!( - let mut w = ::std::io::stdout(); - use ::std::io::Write; + let mut w = String::new(); + use ::std::fmt::Write; writeln!(w, "{} {}", option_env!("CARGO_BIN_NAME").unwrap_or(env!("CARGO_PKG_NAME")), env!("CARGO_PKG_VERSION"), - )?; + ).unwrap(); - writeln!(w, "{}", #summary)?; + writeln!(w, "{}", #summary).unwrap(); - writeln!(w, "\nUsage:\n {}", format!(#usage, bin_name))?; + writeln!(w, "\nUsage:\n {}", format!(#usage, bin_name)).unwrap(); #options - writeln!(w, "{}", #after_options)?; - Ok(()) + writeln!(w, "{}", #after_options).unwrap(); + w ) } diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 486530c..45b8df5 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -95,7 +95,7 @@ pub fn arguments(input: TokenStream) -> TokenStream { } } - fn help(bin_name: &str) -> ::std::io::Result<()> { + fn help(bin_name: &str) -> String { #help_string } diff --git a/src/internal.rs b/src/internal.rs index 412c737..b74dedd 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -14,7 +14,7 @@ use crate::error::ErrorKind; use crate::value::Value; use std::{ ffi::{OsStr, OsString}, - io::Write, + fmt::Write, }; /// Parses an echo-style positional argument @@ -118,33 +118,32 @@ pub fn print_flags( indent_size: usize, width: usize, options: impl IntoIterator, -) -> std::io::Result<()> { +) { let indent = " ".repeat(indent_size); - writeln!(w, "\nOptions:")?; + writeln!(w, "\nOptions:").unwrap(); for (flags, help_string) in options { let mut help_lines = help_string.lines(); - write!(w, "{}{}", &indent, &flags)?; + write!(w, "{}{}", &indent, &flags).unwrap(); if flags.len() <= width { let line = match help_lines.next() { Some(line) => line, None => { - writeln!(w)?; + writeln!(w).unwrap(); continue; } }; let help_indent = " ".repeat(width - flags.len() + 2); - writeln!(w, "{}{}", help_indent, line)?; + writeln!(w, "{}{}", help_indent, line).unwrap(); } else { - writeln!(w)?; + writeln!(w).unwrap(); } let help_indent = " ".repeat(width + indent_size + 2); for line in help_lines { - writeln!(w, "{}{}", help_indent, line)?; + writeln!(w, "{}{}", help_indent, line).unwrap(); } } - Ok(()) } #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index 780f7ac..694bcdf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,7 +82,7 @@ pub trait Arguments: Sized { /// Print the help string for this command. /// /// The `bin_name` specifies the name that executable was called with. - fn help(bin_name: &str) -> std::io::Result<()>; + fn help(bin_name: &str) -> String; /// Get the version string for this command. fn version() -> String; @@ -132,7 +132,7 @@ impl ArgumentIter { })? { match arg { Argument::Help => { - T::help(self.parser.bin_name().unwrap()).unwrap(); + print!("{}", T::help(self.parser.bin_name().unwrap())); std::process::exit(0); } Argument::Version => {