Add program description to full help

This commit is contained in:
2025-11-02 02:01:05 +11:00
parent 912d282d12
commit 57791b1a93
3 changed files with 22 additions and 4 deletions

View File

@@ -110,6 +110,11 @@ impl<ID> core::fmt::Display for StandardFullHelpWriter<'_, ID> {
}
writeln!(f)?;
if let Some(description) = self.0.options.description {
writeln!(f)?;
writeln!(f, "{description}")?;
}
fn calculate_left_pad<ID: 'static>(option: &Opt<ID>) -> usize {
(match option.names {
OptIdentifier::Single(name) => name.chars().count(),

View File

@@ -5,16 +5,22 @@
/// Static structure that contains instructions for parsing command-line arguments
pub struct Opts<ID: 'static> {
/// String containing single characters that match option prefixes
flag_chars: &'static str,
/// List of options
options: &'static[Opt<ID>],
/// String containing single characters that match option prefixes
flag_chars: &'static str,
/// A description of what the program does
description: Option<&'static str>,
}
impl<ID: 'static> Opts<ID> {
/// Build argument parser options with the default flag character of '-'
pub const fn new(options: &'static[Opt<ID>]) -> Self {
Self { flag_chars: "-", options }
Self {
options,
flag_chars: "-",
description: None,
}
}
/// Set the recognised flag/option characters.
@@ -22,4 +28,10 @@ impl<ID: 'static> Opts<ID> {
self.flag_chars = flag_chars;
self
}
/// Set the description of the program, available to help writers.
pub const fn with_description(mut self, description: &'static str) -> Self {
self.description = Some(description);
self
}
}