rustc 1.28.0
clap 2.32.0
When I create an argument with a name and give it explicit index, it can only be used by index; its name is not parsed correctly.
I'm not sure if it's a bug, it may be an intenional design decision.
So I would be glad to see one of two behaviours:
If it's a bug: I can give argument name and index simultaneously and it will work both ways - if no name is given - argument is parsed by it's index
If it's an intentional behaviour: I would like to see a more clear error when I try to use index and argument name (i.e. short() or long() with index()) simultaneously. A panic maybe?
error: Found argument '--htm' which wasn't expected, or isn't valid in this context
Create argument with short or long name and index. Call app with named argument.
let args = App::new( env!("CARGO_PKG_NAME") )
.arg( Arg::with_name("htm file")
.long("htm")
.index(1)
.required(true)
.takes_value(true);
From the doc:
Specifies the index of a positional argument
Index should not be used for short/long options.
I'm leaving this open as a reminder to check and tidy up the doc.
We should create an assert also.
This issue now has bounty of $5. More info here
As a counterpoint, virsh from libvirt allows positional parameters to be defined as flags instead. As in:
virsh vol-list --pool=images
virsh vol-list images
behave the same way. Similarly, the following are identical in behaviour:
virsh vol-create-as images guest 20G
virsh vol-create-as --pool=images --name=guest --capacity=20G
virsh vol-create-as --name=guest --pool=images 20G
I'm not suggesting clap needs this functionality, but there is a precedent for positional arguments being supported as flags.
You can still do that here. But you have to actually define two args for this.
I would be interested in solving this. I've looking at the code and I've thought of two solutions:
The first one seems simpler to me and it makes more sense to me. If an Arg is inconsistent it should be the Arg itself that catches that.
The second one is more general and could also be used to catch other errors. Also, there could be a special clap::parse::Error variant for this, if that seems more elegant than a panic or an assertion.
I would like some guidance on which way should I take :)
We already have a place for it, https://github.com/clap-rs/clap/blob/1519ec81f79ad6f0ff1c94bf6a9af1c84e1a943f/src/build/arg/mod.rs#L4346
I thought this should work outside the debug config. Glad I asked, ty!