如何在 Clap 中允许使用大写标志?

1 rust clap

对于我的 RUST 程序,我使用 Clap 来解析我的命令行参数。我想让用户像这样输入标志:

my_program -L testfile.txt
Run Code Online (Sandbox Code Playgroud)

我像这样设置我的结构:

struct Args {
    #[arg(short)]
    L: bool,

    #[arg(short)]
    s: bool,

    name: String,
}
Run Code Online (Sandbox Code Playgroud)

当我测试我的程序时,它给了我这个错误:

error: Found argument '-L' which wasn't expected, or isn't valid in this context.
Run Code Online (Sandbox Code Playgroud)

我也不能使用ignore_case(),因为这是一个标志并且不接受值。

有谁知道如何解决这个问题?

hal*_*elf 5

从clap 派生文档中的Arg 属性:

short [= <char>]Arg::short

  • 不存在时:无短集
  • 不带<char>:默认为大小写转换字段名称中的第一个字符
use clap::Parser;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[arg(short = 'L')]
    L: bool,

    #[arg(short)]
    s: bool,

    name: String,
}

fn main() {
    let args = Cli::parse();
}
Run Code Online (Sandbox Code Playgroud)

内置可执行帮助:

Usage: xxxxxx [OPTIONS] <NAME>

Arguments:
  <NAME>

Options:
  -L
  -s
  -h, --help     Print help information
  -V, --version  Print version information
Run Code Online (Sandbox Code Playgroud)