Clap: an argument forwarding question


#1

I’m building a utility which basically wraps some other commad, like time ls -lah. So the usage would be my_util [my_util_flags] cmd [cmd_flags]. I would like to use an awesome clap for argument parsing, but can’t find a way to treat all that follows the cmd as positional arguments.

That is, the following works:

> my_util ls foo bar baz
> my_util ls -- -l

But this does not work:

> my_util ls -l

error: The argument '-l' isn't valid 

How can I treat everything, which follow the first positional argument, as additional arguments?


#2

Hm, turns out that this is impossible at the moment, here is a relevant feature request: https://github.com/kbknapp/clap-rs/issues/278.

However I don’t see any workaround for this, except reimplementing argument parsing myself :disappointed:


#3

Docopt can do this.

Example of the usage string: https://github.com/BurntSushi/rust-cmail/blob/master/cmail.rs#L19

And make sure you set options_first to true: https://github.com/BurntSushi/rust-cmail/blob/master/cmail.rs#L53


#4

Wow, thanks! docopt looks cool, and docopt.rs, with the ability to decode options into a struct, looks just super cool!