Clap: Can't use named arguments if they have index

Created on 17 Aug 2018  路  8Comments  路  Source: clap-rs/clap

Rust Version

rustc 1.28.0

Affected Version of clap

clap 2.32.0

Bug or Feature Request Summary

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.

Expected Behavior Summary

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?

Actual Behavior Summary

error: Found argument '--htm' which wasn't expected, or isn't valid in this context

Steps to Reproduce the issue

Create argument with short or long name and index. Call app with named argument.

Sample Code or Link to Sample Code

let args = App::new( env!("CARGO_PKG_NAME") )
        .arg( Arg::with_name("htm file")
            .long("htm")
            .index(1)
            .required(true)
            .takes_value(true);
$5 asserts docs easy want to have good first issue help wanted

All 8 comments

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:

  • Introduce assertions in the index, long and short methods from the Arg class(and other methods if needed)
  • Add a function to the App class that gets called before _do_parse and that iterates over the Args looking for incongruities like this one.

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 :)

I thought this should work outside the debug config. Glad I asked, ty!

Was this page helpful?
0 / 5 - 0 ratings