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

Mr.*_*oor 7 rust clap

我正在使用 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();
Run Code Online (Sandbox Code Playgroud)

最后command,我想获取命令行中的所有剩余部分,包括空格。但当前代码不起作用,它被第一个空格终止。

我认为我应该设置.setting(AppSettings::TrailingVarArg),但没有找到在 Derive API 中执行此操作的方法。请问这该怎么办?


谢谢@MeetTitan,这是我的代码终于可以工作了

#[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>,
}
Run Code Online (Sandbox Code Playgroud)

Mee*_*tan 3

派生参考指出:

任何Command方法都可以用作属性,请参阅语法术语。

例如#[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 { ... }
Run Code Online (Sandbox Code Playgroud)