簡體   English   中英

clap 在 Derive API 的一個字段中捕獲所有剩余參數?

[英]clap capture all remaining arguments in one field in Derive API?

我正在使用帶有以下代碼的 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) ,但在 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>,
}

派生參考指出:

任何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 { ... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM