1

I'm building a CLI which can call other underlying programs, that have their own options and arguments. I'd like to be able to pass those options to a program through the CLI.

$ cli --program [PROGRAM] --programOpts[OPT1, OPT2, ...]

Example:

$ cli --program foo --programOpts.bar 'foo' --programOpts.foo 'bar'

^ In this case bar and foo in programOpts are unknown to cli. The CLI only knows about programOpts and that it's an unknown vector of options specific to the underlying program that's being called.

I was hoping Clap had an API to implement such a thing (looked into Arg and ArgGroup) but it doesn't seem like it.

Is there such an API?

Pascal Precht
  • 8,803
  • 7
  • 41
  • 53
  • Does it work if you say that your argument is a `Vec`? – Boiethios Jan 18 '19 at 11:18
  • Take a look at `clap::Values` [here](https://docs.rs/clap/2.32.0/clap/struct.Values.html), if you cannot assume that the values are encoded with UTF-8, use `clap::OsValues`. – FrederikDS Jan 18 '19 at 11:33
  • @FrederikDS that however will end up with a different API: --opts 'one' 'two' 'tree'. I was looking for: --opts.one 'foo' --opts.two 'bar' where 'one' and 'two' aren't known – Pascal Precht Jan 18 '19 at 11:55
  • 1
    As a user of CLI tools, I'd be surprised by the `--programOpts.bar 'foo'` syntax. I would rather expect something like `--programOpts 'bar=foo'`. – SirDarius Jan 18 '19 at 13:36
  • See also [How can I pass all command line arguments through Clap to another program?](https://stackoverflow.com/q/54191836/155423) – Shepmaster Jan 20 '19 at 03:40
  • @SirDarius it actually seems quite common to have option.property syntax in CLI commands... – Pascal Precht Jan 23 '19 at 20:34

1 Answers1

1

After further research I decided to go down a slightly different path and rather take advantage of the known UNIX -- syntax.

Meaning that all options and flags coming after -- will be passed down to the underlying program:

$ cli --program [PROGRAM] -- foo bar --bazinga --yay=w00t

This can be done using Clap's .raw() configuration on Arg structs.

Pascal Precht
  • 8,803
  • 7
  • 41
  • 53