繁体   English   中英

Rust 拍手自定义标题

[英]Rust Clap custom headings

我正在使用 rust Clap 库来解析命令行 arguments。 当显示我的帮助文本时,我想将必需的 arguments 与可选的 arguments 分开,并将它们放在单独的标题下。 类似这样的东西:

HELP:
    Example header 1:
        Arg 1
        Arg 2

    Example header 2:
        Arg 3
        Arg 4

这可能吗。

读完这个这个这个之后,我认为它可能是,但我不知道如何 go 这样做。

编辑:
所以评论者要求我用一些想要的 output 更新帖子,所以下面是上面链接之一的示例。 我希望能够有两个选项部分并命名它们。

$ myprog --help
My Super Program 1.0
Kevin K. <kbknapp@gmail.com>
Does awesome things

USAGE:
    MyApp [FLAGS] [OPTIONS] <INPUT> [SUBCOMMAND]

FLAGS:
    -h, --help       Prints this message
    -v               Sets the level of verbosity
    -V, --version    Prints version information

OPTIONS:
    -c, --config <FILE>    Sets a custom config file

ARGS:
    INPUT    The input file to use

SUBCOMMANDS:
    help    Prints this message
    test    Controls testing features

因此,将上面的OPTIONS部分更改为:

OPTIONS-1:
    -c, --config <FILE>    Sets a custom config file.

OPTIONS-2:
    -a, --another <FILE>    Another example command.

我想你可能正在寻找help_heading 似乎这是最近添加的,因此您必须获取最新的提交。

货物.toml

[dependencies]
clap = { git = "https://github.com/clap-rs/clap", rev = "8145717" }

main.rs

use clap::Clap;

#[derive(Clap, Debug)]
#[clap(
    name = "My Application",
    version = "1.0",
    author = "Jason M.",
    about = "Stack Overflow"
)]
struct Opts {
    #[clap(
        help_heading = Some("OPTIONS-1"),
        short,
        long,
        value_name="FILE",
        about = "Sets a custom config file"
    )]
    config: String,
    #[clap(
        help_heading = Some("OPTIONS-2"),
        short,
        long,
        value_name="FILE",
        about = "Another example command"
    )]
    another: String,
}

fn main() {
    let opts: Opts = Opts::parse();
}
use clap::{App, Arg};

fn main() {
    let app = App::new("My Application")
        .version("1.0")
        .author("Jason M.")
        .about("Stack Overflow")
        .help_heading("OPTIONS-1")
        .arg(
            Arg::new("config")
                .short('c')
                .long("config")
                .value_name("FILE")
                .about("Sets a custom config file"),
        )
        .help_heading("OPTIONS-2")
        .arg(
            Arg::new("another")
                .short('a')
                .long("another")
                .value_name("FILE")
                .about("Another example command"),
        );

    app.get_matches();
}

以上任何一个都将在运行cargo run -- --help时生成以下内容:

My Application 1.0
Jason M.
Stack Overflow

USAGE:
    clap_headings --config <FILE> --another <FILE>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS-1:
    -c, --config <FILE>    Sets a custom config file

OPTIONS-2:
    -a, --another <FILE>    Another example command

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM