These are chat archives for kbknapp/clap-rs

18th
Jan 2019
Pascal Precht
@PascalPrecht
Jan 18 10:56

Hey everyone!

Hope somebody's around :D

I'm very new to Rust and Clap as well, so I'm trying to build a little something that helps me getting used to the language.

I'm using clap for creating a CLI and I was wondering the following:

Is it possible to configure and argument that can have #n "properties" which aren't known at compile time?

So for example the CLI is calling another program under the hood and that program may receive program specific options.

$ some-cli --client some-program --clientOpts.foo 'bar' --clientOpts.bar 'foo'

some-program can have different own options, so I'd need something like --clientOpts that let's me receive options that are specific to the program, but the properties aren't known (foo and bar might be bazinga and booya in a different program)

I first thought ArgGroup could help here, but it doesn't seem like it.

Any thoughts?

Pascal Precht
@PascalPrecht
Jan 18 11:17

One way I could imagine this could work is by configuring an argument that can have multiple values but then having those values being in a parsable format.

So for example:

$ cli --clientOptions 'foo=bar' 'bar=foo' 'something=else'
Those values will need parsing, as it could also be that some client option has mulitple values itself.
Denis Lisov
@tanriol
Jan 18 11:33
Do you know the argument names before parsing, or are you supposed to just throw them all at some other program and see if it works?
Pascal Precht
@PascalPrecht
Jan 18 11:34
Yea I don't know the argument names before parsing

Basically, what I wanna say is:

Take all the options applied via --clientOptions and pass them onto program

Where --clientOptions is an unknown size and I also don't really care what those options are.
Denis Lisov
@tanriol
Jan 18 11:39
How about the well-known unix convention of cli --some-cli-option --another-cli-option -- --some-client-option # everything after -- is passed to the client directly?
Pascal Precht
@PascalPrecht
Jan 18 11:40
That would be great too!

Does that work within cargo run as well?

$ carg run -- foo -- --some-options --another-option

etc

Denis Lisov
@tanriol
Jan 18 11:41
Never tried, but if it did not I'd file a cargo bug :-)
Pascal Precht
@PascalPrecht
Jan 18 11:42
Is there a way to express that syntax with Clap Args ?
Denis Lisov
@tanriol
Jan 18 11:50
Yes, a final argument with .raw(true) should work, IIUC.
Pascal Precht
@PascalPrecht
Jan 18 11:53
Ah, thanks, I'll give that a spin!
🙏