首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >锈蚀自定义标题

锈蚀自定义标题
EN

Stack Overflow用户
提问于 2020-06-07 17:36:39
回答 1查看 985关注 0票数 2

我正在使用锈迹库来解析命令行参数。在显示帮助文本时,我希望将所需的参数从可选参数中分离出来,并将它们放在单独的标题下。与此类似的东西:

代码语言:javascript
复制
HELP:
    Example header 1:
        Arg 1
        Arg 2

    Example header 2:
        Arg 3
        Arg 4

这有可能吗。

在阅读了之后,我认为可能是,但我对如何做到这一点没有信心。

编辑:

所以有个评论要求我用一些想要的输出来更新文章,下面是上面一个链接的例子。我希望有两个选项部分,并命名它们。

代码语言:javascript
复制
$ 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部分更改为:

代码语言:javascript
复制
OPTIONS-1:
    -c, --config <FILE>    Sets a custom config file.

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-17 10:08:09

我想你可能在找help_heading。这似乎是最近添加的,所以您必须掌握最新的提交。

cargo.toml

代码语言:javascript
复制
[dependencies]
clap = { git = "https://github.com/clap-rs/clap", rev = "8145717" }

main.rs

代码语言:javascript
复制
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();
}
代码语言:javascript
复制
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时,上述任一项都将生成以下内容

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62249229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档