Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit compile-time error if the derive interface produces duplicate short/long arguments #3592

Closed
2 tasks done
nsunderland1 opened this issue Mar 30, 2022 · 2 comments
Closed
2 tasks done
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@nsunderland1
Copy link

Please complete the following tasks

Clap Version

3.1.3

Describe your use case

Not much special context for the use case, reasoning behind the feature is given below.

Describe the solution you'd like

Given the following code (playground):

use clap::Parser;

#[derive(Parser)]
struct Args {
    #[clap(short)]
    arg1: String,

    #[clap(short)]
    arg2: String,
}

fn main() {
    let _ = Args::parse();
}

clap will successfully detect at runtime that arg1 and arg2 have the duplicate short form -a:

thread 'main' panicked at 'Command playground: Short option names must be unique for each argument, but '-a' is in use by both 'arg1' and 'arg2'', /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-3.1.3/src/build/debug_asserts.rs:100:17
note: [run with `RUST_BACKTRACE=1` environment variable to display a backtrace](https://play.rust-lang.org/#)

With clap's derive interface, it seems like this could be caught at compile time when the proc macro executes on Args, by producing a compiler error if it picks up on duplicate short (or long) names.

This would give much quicker feedback for programmer error. Also, since clap only checks for argument uniqueness in debug mode, if you're only ever running a release build of your binary you won't actually hit this panic.

Alternatives, if applicable

I personally wouldn't mind it if the assertions were enabled in release builds, but the compile-time approach seems better as it avoids the runtime overhead.

Additional Context

We were mostly only running our binary in release mode, and we went a few days with duplicate short arguments. When we hit the panic, it wasn't immediately obvious what was going on.

@nsunderland1 nsunderland1 added the C-enhancement Category: Raise on the bar on expectations label Mar 30, 2022
@epage
Copy link
Member

epage commented Mar 30, 2022

FYI this has previously been reported as #3133

@epage epage closed this as completed Mar 30, 2022
@nsunderland1
Copy link
Author

Ah thanks, went searching with "unique" as my query, shoulda tried flipping it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants