我正在使用带有以下代码的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,这是我的密码

#[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>,
}

推荐答案

derive reference个州:

任何Command method也可以用作属性,语法请参见Terminology.

e、 g.#[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 { ... }

Rust相关问答推荐

如果A == B,则将Rc A下推到Rc B

无法理解铁 rust &S错误处理

编译项目期间使用Cargo生成时出现rustc错误

铁 rust 中的共享对象实现特征

闭包不会发送,即使它只捕获发送变量

在rust sqlx中使用ilike和push bind

为什么基于高山Linux的Docker镜像不能在绝对路径下找到要执行的命令?

Rust&;Tokio:如何处理更多的信号,而不仅仅是SIGINT,即SIGQUE?

在Rust中,Box:ed struct 与普通 struct 在删除顺序上有区别吗?

rust中的库插件管理器,现在是否可行?

应为关联类型,找到类型参数

Rust面向对象设计模式

在什么情况下 `..._or()` 比 `..._or_else(|| {})` 更好,为什么?

当我try 使用 SKI 演算中的S I I实现递归时,为什么 Rust 会失败?

将 &str 或 String 保存在变量中

为什么带有生命周期指定的方法不能被调用两次?

我可以在 Rust 中 serde struct camel_case 和 deserde PascalCase

为什么我可以同时传递可变和不可变引用?

我如何将 google_gmail1::Gmail> 传递给线程生成?

没有通用参数的通用返回