clap crate 为 -h
选项实现了内置行为,但它似乎没有为 -?
做同样的事情。有没有办法告诉它这样做?
最佳答案
我在 clap repository 上开了一个问题.作者/主要贡献者已在那里回答。这是代码的副本,它回答了这个问题:
extern crate clap;
use std::env;
use std::process;
use clap::{App, Arg};
fn main() {
// We build the App instance and save it, so we can
// use it later if needed
let mut app = App::new("prog").arg(
Arg::with_name("help")
.short("?")
.help("Also prints the help message"),
);
// We call this method which will do all the
//parsing, but not consume our App instance
let res = app.get_matches_from_safe_borrow(env::args_os());
// This calls all the normal clap error messages
// if one should exist
let matches = res.unwrap_or_else(|e| e.exit());
// Now we check for ?
if matches.is_present("help") {
let _ = app.print_help();
println!(""); // adds a newline
process::exit(0);
}
// Now we can use matches like normal...
}
关于rust - 有没有办法让 clap treat -?和-h一样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746869/