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

Is there a way to make clap treat -? the same way as -h?

clap crate 为 -h 选项实现了内置行为,但它似乎没有为 -? 做同样的事情。有没有办法告诉它这样做?

从 Clap 2.29.0 开始,没有为同一选项添加多个短名称的内置方法。

App::help_short lets you override the default -h option for help, but although the function accepts a string, it only cares about the first character (after stripping leading dashes). Arg::short does the same.

您可以将 -? 选项定义为单独的选项,并通过调用 App::print_help. This will have the effect of displaying -? separately in the help text, although you can hide 选项自行处理。

试图将此作为评论,但不合适。

您将要 运行 解决的另一个问题是,至少在类 Unix 系统上,大多数 shell 都会为“?”赋予特殊含义。我有一个 Rust 程序,它只打印出它在命令行上找到的参数。

> $ cargo run one two three
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/cmdl one two three`
["target/debug/cmdl", "one", "two", "three"]
args len: 4
arg: target/debug/cmdl
arg: one
arg: two
arg: three

当我通过它时-?:

> $ cargo run -?
zsh: no matches found: -?

我引用的话可以传入-? 前面放--:

> $ cargo run -- "-?"
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/cmdl '-?'`
["target/debug/cmdl", "-?"]
args len: 2
arg: target/debug/cmdl
arg: -?

对于寻求帮助的人来说,这似乎有点过高,但也许在 OP 的平台上更友好。我正在研究 Mac OS X.

更新:

类 Unix 系统上大多数基于 Bourne 的 shells 解释 ?作为匹配文件名中单个字符的通配符。我对鱼一无所知,但我查看了下面的 /bin/sh、/bin/ksh、/bin/bash 和 /bin/zsh。

如果你使用 -?作为带有这些 shell 的命令行选项,并且工作目录中有一个由连字符后跟一个字符组成的文件名,您的程序将看到文件名而不是“-?”因为 shell 会在命令行到达您的程序之前进行替换。

一些 shells(特别是 zsh)会被 -?在检查文件之前,如我的原始消息中所述。

如下图,如果-?包含在引号中,shell 不会弄乱它,但我不希望大多数人知道这一点。当我尝试查找我不熟悉的程序的帮助页面时,我什至不相信自己会记住这一点。

$ /bin/sh
$ ls
-a      -bb     -ccc    a       bb      ccc
$ echo -?
-a
$ echo ?
a
$ echo ??
-a bb
$ echo "-?"
-?

$ /bin/ksh
$ ls
-a      -bb     -ccc    a       bb      ccc
$ echo ?
a
$ echo -?
-a
$ echo ??
-a bb
$ echo "-?"
-?

$ /bin/bash
$ ls
-a      -bb     -ccc    a       bb      ccc
$ echo ?
a
$ echo -?
-a
$ echo ??
-a bb
$ echo "-?"
-?

$ /bin/zsh
$ ls
-a   -bb  -ccc a    bb   ccc
$ echo ?
a
$ echo -?
-a
$ echo ??
-a bb
$ echo "-?"
-?

我在 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...
}