#[derive(Parser)]
struct Cli {
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(clap::Subcommand)]
enum Subcommand {
Index {
#[clap(parse(from_os_str))]
path: path::PathBuf,
},
Show {
item: Option<String>,
},
Cd {
term: String,
},
List,
Init {
shell: InitShell,
},
Search {
term: Option<String>,
},
Add {
category: String,
title: String,
},
}
fn main(){
let cli = Cli::parse();
match cli.subcommand{
Subcommand::Index=>{/*code here*/}
Subcommand::Show=>{/*and here*/}
Subcommand::Display=>{/*and also here*/}
Subcommand::Cd=>{}
Subcommand::List=>{}
// ... more matches
}
}当我使用--help运行我的程序时,子命令部分如下所示:
SUBCOMMANDS:
add
cd
help
index
init
list
search
show我想定义一些别名,例如ls表示list或display表示show,这样帮助看起来如下所示:
SUBCOMMANDS:
add
cd, path
help
index
init
list, ls
search
show, display我可以看到,在cargo build等于cargo b的情况下,货物会做类似的事情。
我查看了clap文档,并为builder找到了一个别名函数,但是我无法找到如何使用派生api来实现这一点。这是可能的吗?如果可能的话,我该怎么做?
发布于 2022-09-20 17:14:35
Clap派生文档,例如(在可能值属性下):
可能值属性 它们对应于一个
PossibleValue。 原始属性: AnyPossibleValue方法也可以用作属性,请参阅语法术语。
#[clap(alias("foo"))]将转换为pv.alias("foo")你还说你希望化名出现在help下面。在这种情况下,您需要使用visible_alias来代替:
#[clap(visible_alias("foo"))]https://stackoverflow.com/questions/73789062
复制相似问题