rust clap捕获派生API中一个字段中的所有剩余参数?

nnvyjq4y  于 2024-01-08  发布在  其他
关注(0)|答案(2)|浏览(133)

我使用clap v3.1.18,代码如下。

#[derive(Parser, Debug)]
#[clap(author, version, about = ABOUT_TEXT, long_about = Some(ABOUT_TEXT))]
struct Args {

    /// The root folder on local filesystem to scan
    #[clap(short, long, default_value_t = utils::get_current_dir())]
    folder: String,

    /// Max number of recursive folders to scan
    #[clap(short, long, default_value_t = 5)]
    depth: u8,

    /// Regular expression to include files. Only files whose name matches the specified regexp will be listed. E.g. to only list *.dll and *.exe files, you can specify `((\\.dll)$)|((\\.exe)$)`
    #[clap(short, long, default_value(".+"))]
    include: String,

    /// Regular expression to include files. Regular expression to exclude files and directories. Files or directories whose name matches this regexp will be skipped
    #[clap(short, long, default_value("(^~)|(^\\.)|((\\.tmp)$)|((\\.log)$)"))]
    exclude: String,

    /// Command line to start child process
    #[clap(multiple_occurrences=true, use_delimiter=false)]
    command: String,
}
let args = Args::parse();

字符串
最后一个command,我想在命令行中得到所有剩余的部分,包括空格。但是当前代码不起作用,它被第一个空格终止。
我想我应该设置.setting(AppSettings::TrailingVarArg),但是在派生API中找不到这样做的方法。请问如何才能做到这一点?
感谢@MeetTitan,这是我的代码finnally作品

#[derive(Parser, Debug)]
#[clap(author, version, about = ABOUT_TEXT, long_about = Some(ABOUT_TEXT), trailing_var_arg=true)]
struct Args {

    /// The root folder on local filesystem to scan
    #[clap(short, long, default_value_t = utils::get_current_dir())]
    folder: String,

    /// Max number of recursive folders to scan
    #[clap(short, long, default_value_t = 5)]
    depth: u8,

    /// Regular expression to include files. Only files whose name matches the specified regexp will be listed. E.g. to only list *.dll and *.exe files, you can specify `((\\.dll)$)|((\\.exe)$)`
    #[clap(short, long, default_value(".+"))]
    include: String,

    /// Regular expression to include files. Regular expression to exclude files and directories. Files or directories whose name matches this regexp will be skipped
    #[clap(short, long, default_value("(^~)|(^\\.)|((\\.tmp)$)|((\\.log)$)"))]
    exclude: String,

    /// Command line to start child process
    #[clap(long, multiple_values=true, allow_hyphen_values=true)]
    run: Vec<String>,
}

2nbm6dog

2nbm6dog1#

derive reference声明:
任何Command method也可以用作属性,语法参见术语。
例如,#[clap(arg_required_else_help(true))]将转换为cmd.arg_required_else_help(true)
使用这个公式,我们可以推导出.setting(AppSettings::TrailingVarArg),(更准确地说,App::trailing_var_arg())如下:

#[clap(... trailing_var_arg=true)]
struct Args { ... }

字符串

kfgdxczn

kfgdxczn2#

下面是我对最近的clap 4.4.8的类似问题的解决方案:

/// A dummy command that accepts any arguments
#[derive(clap::Args, Clone, Debug)]
struct DummyCommandArgs {
    #[arg(trailing_var_arg = true, allow_hyphen_values = true, hide = true)]
    _args: Vec<String>,
}

字符串
如果你想真正使用参数,你可能想摆脱hide=true。这也应该适用于额外的参数。

相关问题