Clap: Ignore and save in vector unknow args

Created on 24 Jan 2019  路  4Comments  路  Source: clap-rs/clap

Affected Version of clap

  • 2.32.0

Bug or Feature Request Summary

It would be great to have a way to save unknow args into a Vec<> or slice. For example, an Arg::with_name() option, e.g. Arg::with_name('unknow').unknow_args() or something like this.
Maybe it could be that instead of calling get_matches() on the arg list, add a get_know_matches() that returns a tuple, the first element being what would be get_matches() and the second a vector of the unknow args... Something like Python's argparse:

args, unknown = parser.parse_known_args()
let (matches, unknow_matches) = App::new("myprog")
    .version("0.1")
    .author("You <[email protected]>")
    .about("Something about your program")
    .arg(Arg::with_name("debug")
        .short("d")
        .multiple(true)
        .help("Turn debugging information on"))
     .arg(Arg::with_name("quiet")
         .short("q")
         .long("shut-up")
         .help("Shut up"))
      .get_know_matches();

And then, if you call myprog -d --something, you have in matches the normal clap behaviour, and in unknow_matches a vector containg '--something'

I don't know if I'm explaining it well, as English is not my primary language.
EDIT: Save unknow in vector or slice instead of ignoring them

The reason for this is that i'm building a program that has "subargs" (e.g. myprogram -Xh is the message help for myprogram -X, but not the same help as myprogram -Yh or myprogram -h.) I can build this by adding -X and -Y arguments, and run another function based on which arg was used, but clap needs to know all arguments, and that's why this would be nice.

args options new feature

Most helpful comment

I could still quite use this: I want to be able to pass-through the args to a specific subcommand to a child process, ignoring all the direct args to the subcommand itself.

All 4 comments

I found this: https://github.com/clap-rs/clap/issues/1361 It's exactly what I was looking for... maybe we should focus on that issue.

I could still quite use this: I want to be able to pass-through the args to a specific subcommand to a child process, ignoring all the direct args to the subcommand itself.

It would be useful for me too

I just found out how to do it! I used: AppSettings::TrailingVarArg, AppSettings::AllowLeadingHyphen

I did it with structopt but you can translate to the clap equivalent.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smklein picture smklein  路  35Comments

jojva picture jojva  路  18Comments

CAD97 picture CAD97  路  21Comments

neysofu picture neysofu  路  41Comments

kbknapp picture kbknapp  路  23Comments