From 741dfd4d7ef14f6aeeba8bf6cf5f4df7440389b7 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Sat, 15 Nov 2025 16:13:41 +1100 Subject: [PATCH] Replace non-general shorts-only contractive Usage line in the full standard help writer with the short standard help writer --- jaarg/src/help.rs | 58 ++++++++--------------------------------------- 1 file changed, 9 insertions(+), 49 deletions(-) diff --git a/jaarg/src/help.rs b/jaarg/src/help.rs index 6de6e8b..a96189a 100644 --- a/jaarg/src/help.rs +++ b/jaarg/src/help.rs @@ -9,6 +9,12 @@ pub struct HelpWriterContext<'a, ID: 'static> { pub program_name: &'a str, } +impl Clone for HelpWriterContext<'_, ID> { + fn clone(&self) -> Self { + Self { options: self.options, program_name: self.program_name } + } +} + pub trait HelpWriter<'a, ID: 'static>: core::fmt::Display { fn new(ctx: HelpWriterContext<'a, ID>) -> Self; } @@ -62,54 +68,8 @@ impl core::fmt::Display for StandardFullHelpWriter<'_, ID> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { use core::fmt::Write; - // Base usage - write!(f, "Usage: {}", self.0.program_name)?; - let short_flag = self.0.options.flag_chars.chars().next().unwrap(); - - // Write optional short options - let mut first = true; - for option in self.0.options.options { - if let (OptType::Flag | OptType::Value, false) = (option.r#type, option.is_required()) { - if let Some(c) = option.first_short_name_char() { - if first { - write!(f, " [{short_flag}")?; - first = false; - } - f.write_char(c)?; - } - } - } - if !first { - f.write_char(']')?; - } - - // Write required short options - first = true; - for option in self.0.options.options { - if let (OptType::Flag | OptType::Value, true) = (option.r#type, option.is_required()) { - if let Some(c) = option.first_short_name_char() { - if first { - write!(f, " <{short_flag}")?; - first = false; - } - f.write_char(c)?; - } - } - } - if !first { - f.write_char('>')?; - } - - // Write positional arguments - for option in self.0.options.iter() - .filter(|o| matches!(o.r#type, OptType::Positional)) { - let name = option.first_name(); - match option.is_required() { - true => write!(f, " <{name}>")?, - false => write!(f, " [{name}]")?, - } - } - writeln!(f)?; + // Base short usage + writeln!(f, "{}", StandardShortUsageWriter::new(self.0.clone()))?; if let Some(description) = self.0.options.description { writeln!(f)?; @@ -129,7 +89,7 @@ impl core::fmt::Display for StandardFullHelpWriter<'_, ID> { .map(|o| calculate_left_pad(o)).max().unwrap_or(0); // Write positional argument descriptions - first = true; + let mut first = true; for option in self.0.options.iter() .filter(|o| matches!(o.r#type, OptType::Positional)) { if first {