rust - 有没有办法让 clap treat -?和-h一样吗?

标签 rust command-line-interface rust-crates clap

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/

相关文章:

struct - 如何交换结构的两个字段

rust - 如何指定一个接受引用并返回实现与引用具有相同生命周期的特征的任何类型的闭包?

c++ - Cpp/Cli 将 std::map 转换为 .net 字典

linux - 匹配模式的 mv 文件夹

node.js - Angular 8 未在 nodejs 版本 8.10.0 上运行

rust - 是否有可能在 Rust 的不同源文件中有一个模块

rust - 使用 main.rs 文件从本地 crate 导入时未解析的导入

floating-point - 默认的浮点类型是什么?

multithreading - 在 Rust 中并行处理树

rust - 从 lint 中找出 crate 的当前版本?