gpt4 book ai didi

rust - 调用 Clap 的 get_matches 后如何显示帮助?

转载 作者:行者123 更新时间:2023-11-29 08:26:38 25 4
gpt4 key购买 nike

我遇到了与 Is there any straightforward way for Clap to display help when no command is provided? 相同的问题,但该问题中提出的解决方案对我来说还不够好。

.setting(AppSettings::ArgRequiredElseHelp) 如果没有提供参数则停止程序,即使没有提供参数我也需要程序继续执行。我需要额外显示的帮助。

最佳答案

你可以写之前的字符串。

use clap::{App, SubCommand};

use std::str;

fn main() {
let mut app = App::new("myapp")
.version("0.0.1")
.about("My first CLI APP")
.subcommand(SubCommand::with_name("ls").about("List anything"));

let mut help = Vec::new();
app.write_long_help(&mut help).unwrap();

let _ = app.get_matches();

println!("{}", str::from_utf8(&help).unwrap());
}

或者您可以使用 get_matches_safe

use clap::{App, AppSettings, ErrorKind, SubCommand};

fn main() {
let app = App::new("myapp")
.setting(AppSettings::ArgRequiredElseHelp)
.version("0.0.1")
.about("My first CLI APP")
.subcommand(SubCommand::with_name("ls").about("List anything"));

let matches = app.get_matches_safe();

match matches {
Err(e) => {
if e.kind == ErrorKind::MissingArgumentOrSubcommand {
println!("{}", e.message)
}
}
_ => (),
}
}

关于rust - 调用 Clap 的 get_matches 后如何显示帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54837057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com