Skip to main content

HelpFormatter

Struct HelpFormatter 

Source
pub struct HelpFormatter { /* private fields */ }
Expand description

Help text formatter with terminal-aware wrapping.

This struct provides methods for formatting help text with proper indentation, text wrapping, and special formatting for definition lists (options/arguments).

§Example

use click::HelpFormatter;

let mut formatter = HelpFormatter::new(80);

formatter.write_usage("mycli", "[OPTIONS] COMMAND");
formatter.write_paragraph("A helpful CLI tool.");
formatter.write_heading("Options");
formatter.write_definition_list(&[
    ("--help, -h", "Show this help message"),
    ("--verbose, -v", "Enable verbose output"),
]);

println!("{}", formatter.get_help());

Implementations§

Source§

impl HelpFormatter

Source

pub fn new(width: usize) -> Self

Create a new HelpFormatter with the specified terminal width.

If width is 0 or less than minimum, uses default width.

Source

pub fn detect_width() -> Self

Create a new HelpFormatter that detects terminal width.

Source

pub fn get_help(&self) -> &str

Get the accumulated help text.

Source

pub fn into_help(self) -> String

Consume the formatter and return the help text.

Source

pub fn width(&self) -> usize

Get the current terminal width.

Source

pub fn set_indent(&mut self, indent: usize)

Set the current indentation level.

Source

pub fn indent(&mut self, amount: usize)

Increase indentation by the given amount.

Source

pub fn dedent(&mut self, amount: usize)

Decrease indentation by the given amount.

Source

pub fn write_blank(&mut self)

Write a blank line.

Source

pub fn write_raw(&mut self, text: &str)

Write raw text without any processing.

Source

pub fn write(&mut self, text: &str)

Write text with the current indentation.

Source

pub fn write_usage(&mut self, prog: &str, args: &str)

Write a usage line.

Format: Usage: prog [OPTIONS] ARGS

Source

pub fn write_heading(&mut self, heading: &str)

Write a section heading.

The heading is followed by a blank line.

Source

pub fn write_paragraph(&mut self, text: &str)

Write a paragraph of text with word wrapping.

Preserves paragraph breaks (double newlines) and wraps text to fit within the terminal width.

Source

pub fn write_definition_list(&mut self, items: &[(&str, &str)])

Write a definition list (term + description pairs).

Used for formatting options and arguments. Each item is a tuple of (term, description).

Format depends on term length:

  • Short terms: description on same line
  • Long terms: description on next line, indented
Source

pub fn write_definition_list_strings(&mut self, items: &[(String, String)])

Write a definition list with string tuples.

Trait Implementations§

Source§

impl Debug for HelpFormatter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HelpFormatter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.