Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide multiple line help message with Clap?

Tags:

rust

clap

Is there a way for us to have line breaks in clap's help message?

I tried multiple line comments, also tried inserting \n in the mix. But neither works.

#[derive(Parser, Debug)]
#[clap(author, version, about)]
struct Args {
    /// The input filename.\n
    /// Hello world!
    #[clap(short, long, value_parser)]
    input: String,
}

Output:

USAGE:
    ascii_tree --input <INPUT>

OPTIONS:
    -h, --help             Print help information
    -i, --input <INPUT>    The input filename.\n Hello world!
    -V, --version          Print version information

Is there away to achieve the following?

USAGE:
    ascii_tree --input <INPUT>

OPTIONS:
    -h, --help             Print help information
    -i, --input <INPUT>    The input filename.
                           Hello world!
    -V, --version          Print version information
like image 396
Yuchen Avatar asked Dec 11 '25 22:12

Yuchen


2 Answers

I know of two options. Make the second line only contain ///, or use verbatim_doc_comment:

#[derive(Parser, Debug)]
struct Args {
    /// Name of the person to greet
    ///
    /// Has a multi-line help.
    #[clap(short, long)]
    name: String,

    /// Number of times to greet
    /// Use the verbatim arg
    #[clap(short, long, verbatim_doc_comment)]
    count: u8,
}

Playground

like image 50
Caesar Avatar answered Dec 15 '25 13:12

Caesar


You can add the verbatim_doc_comment option:

#[derive(Parser, Debug)]
#[clap(author, version, about)]
struct Args {
    /// The input filename.
    /// Hello world!
    #[clap(short, long, value_parser, verbatim_doc_comment)]
    input: String,
}
USAGE:
    ascii_tree --input <INPUT>

OPTIONS:
    -h, --help             Print help information
    -i, --input <INPUT>    The input filename.
                           Hello world!
    -V, --version          Print version information

It seems without it, Rust only recognizes paragraph breaks via double-newline.

like image 45
Finomnis Avatar answered Dec 15 '25 13:12

Finomnis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!