简体   繁体   中英

clap capture all remaining arguments in one field in Derive API?

I am using clap v3.1.18 with following code.

#[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();

The last command , I want to get all the remaining part in the command line, including whitespaces. But current code does not work and it is terminated by the first whitespace.

I think I should set .setting(AppSettings::TrailingVarArg) , but don't find a way to do that in Derive API. Please how can this be done?


Thanks @MeetTitan, here is my code finnally works

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

The derive reference states:

Any Command method can also be used as an attribute, see Terminology for syntax.

eg #[clap(arg_required_else_help(true))] would translate to cmd.arg_required_else_help(true)

Using this we can derive .setting(AppSettings::TrailingVarArg) , (more accurately, App::trailing_var_arg() ) like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM