rust 如何使clap派生不转换下划线为连字符选项名称

mdfafbf1  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(101)

使用Clap的derive macro:

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    /// Name of the person to greet
    #[arg(short, long)]
    full_name: String,
}

字符串
这将创建一个命令行参数--full-name。有没有办法让它使用--full_name?理想情况下,使用全局选项,而不是为每个选项单独设置它?

cbwuti44

cbwuti441#

您可以通过提供#[arg(long("custom_name")]来覆盖arg name

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    /// Name of the person to greet
    #[arg(short, long("full_name"))]
    full_name: String,
}

字符串
或者,如果你想对所有参数使用snake_case命名,你可以通过设置#[command(rename_all = "snake_case")]来更改默认的重命名约定。

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, rename_all = "snake_case")]
struct Args {
    /// Name of the person to greet
    #[arg(short, long)]
    full_name: String,
}

相关问题