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

Auto-generated Short Flags Cause Collision When Args Start With the Same Letter #5215

Closed
2 tasks done
Ghvstcode opened this issue Nov 15, 2023 · 1 comment
Closed
2 tasks done
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations

Comments

@Ghvstcode
Copy link

Please complete the following tasks

Clap Version

4.4.8

Describe your use case

When deriving command-line arguments, there appears to be an issue with the automatic generation of short flag identifiers. Specifically, if two arguments start with the same letter and the short flag (short) is not explicitly specified for them, clap defaults to using the first letter of each argument as the short flag. This results in a collision, as both arguments end up with the same short flag identifier, leading to the error: "Short option names must be unique for each argument".

use clap::Parser;

// My CLI Program
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
    /// An example option
    #[clap(short, long)]
    option: String,
    /// An example flag
    #[clap(short, long)]
    online: bool,
}

both option and online arguments start with the letter 'o'. Since short is not specified, clap defaults to using 'o' as the short flag for both, causing a conflict.

Describe the solution you'd like

Ideally, clap should handle such scenarios more gracefully. If two arguments begin with the same letter and no explicit short flags are provided, it could automatically generate unique short flags. One possible approach is to use the first two letters of the argument names for subsequent arguments that start with the same letter. For example, in the case above, option could default to o, and online could default to on.

Alternatives, if applicable

No response

Additional Context

No response

@Ghvstcode Ghvstcode added the C-enhancement Category: Raise on the bar on expectations label Nov 15, 2023
@epage
Copy link
Member

epage commented Nov 15, 2023

You should get asserts in debug builds about duplicates. #3133 is about making those asserts compiler errors.

Note that we do not support multi-character shorts (like -on).

I think this is case where it is better to fail error and have the developer fix it by explicitly specifying a short, rather than to pick something. The CLI should be specifically designed, rather than getting incidental behavior. For example, with this proposal, just changing the order that the fields are specified would change the CLI.

I'm going to go ahead and close this as I think its best we not go a route like this. If there is a reason for us to re-evaluate, let us know!

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2023
@epage epage added the A-derive Area: #[derive]` macro API label Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants