Skip to content

Commit

Permalink
make help() return a String
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Dec 19, 2023
1 parent c206234 commit b3ee70b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
16 changes: 8 additions & 8 deletions derive/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
2 changes: 1 addition & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 8 additions & 9 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -118,33 +118,32 @@ pub fn print_flags(
indent_size: usize,
width: usize,
options: impl IntoIterator<Item = (&'static str, &'static str)>,
) -> 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)]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<T: Arguments> ArgumentIter<T> {
})? {
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 => {
Expand Down

0 comments on commit b3ee70b

Please sign in to comment.