我正在使用锈迹库来解析命令行参数。在显示帮助文本时,我希望将所需的参数从可选参数中分离出来,并将它们放在单独的标题下。与此类似的东西:
HELP:
Example header 1:
Arg 1
Arg 2
Example header 2:
Arg 3
Arg 4这有可能吗。
在阅读了这、这和这之后,我认为可能是,但我对如何做到这一点没有信心。
编辑:
所以有个评论要求我用一些想要的输出来更新文章,下面是上面一个链接的例子。我希望有两个选项部分,并命名它们。
$ 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.发布于 2020-06-17 10:08:09
我想你可能在找help_heading。这似乎是最近添加的,所以您必须掌握最新的提交。
cargo.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 commandhttps://stackoverflow.com/questions/62249229
复制相似问题