如何使用 Clap 提供多行帮助消息?

Yuc*_*ong 7 rust clap

有没有办法让 clap 的帮助消息中出现换行符?

我尝试了多行注释,也尝试了\n混合插入。但两者都不起作用。

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

输出:

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
Run Code Online (Sandbox Code Playgroud)

有没有办法实现以下目标?

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

Cae*_*sar 15

我知道有两种选择。使第二行仅包含///,或使用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,
}
Run Code Online (Sandbox Code Playgroud)

操场


Fin*_*nis 5

您可以添加verbatim_doc_comment选项:

#[derive(Parser, Debug)]
#[clap(author, version, about)]
struct Args {
    /// The input filename.
    /// Hello world!
    #[clap(short, long, value_parser, verbatim_doc_comment)]
    input: String,
}
Run Code Online (Sandbox Code Playgroud)
#[derive(Parser, Debug)]
#[clap(author, version, about)]
struct Args {
    /// The input filename.
    /// Hello world!
    #[clap(short, long, value_parser, verbatim_doc_comment)]
    input: String,
}
Run Code Online (Sandbox Code Playgroud)

似乎没有它,Rust 只能通过双换行来识别段落分隔符。