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

Suggestion: flags share aliases #4098

Closed
2 tasks done
InCogNiTo124 opened this issue Aug 19, 2022 · 2 comments
Closed
2 tasks done

Suggestion: flags share aliases #4098

InCogNiTo124 opened this issue Aug 19, 2022 · 2 comments
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@InCogNiTo124
Copy link

InCogNiTo124 commented Aug 19, 2022

Please complete the following tasks

Clap Version

3.2.17

Describe your use case

My use case is reproducing something along of this feature:

  • -H
    • Same as ‘--header-in --header-out’. A short option indicating the input file has a header line, and the output should contain a header line as well.

So basically, an boolean flag which enables two other flags. Currently, clap does not permit this:

'short option names must be unique, but '-H' is in use by both 'headers-in' and 'headers-out''

Describe the solution you'd like

Ideally, the code should be allowed to look like this:

Command::new()
.arg(
    Arg::new("headers-in")
        .long("headers-in")
        .short_visible_alias('H')
        .action(ArgAction::SetTrue))
.arg(
    Arg::new("headers-out")
        .long("headers-out")
        .short_visible_alias('H')
        .action(ArgAction::SetTrue))

and the invocation would then look something like:

$ my_program -H x.txt
Headers in detected!
Headers out detected!

Alternatives, if applicable

Right now, the alternative is to have a complicated condition in if:

Code right now:

if *matches.get_one::<bool>("headers-in").unwrap_or(&false) || *matches.get_one::<bool>("headers").unwrap_or(&false) {
.... something ...
}

Ideally, this should look like

if *matches.get_one::<bool>("headers-in").unwrap_or(&false) {
.... something ...
}

Additional Context

I'm struggling with the source code a bit, however I imagine a hashmap of aliases to an iterable of args. The value of the alias would be the value which is given to the original arguments.

@InCogNiTo124 InCogNiTo124 added the C-enhancement Category: Raise on the bar on expectations label Aug 19, 2022
@epage
Copy link
Member

epage commented Aug 19, 2022

We have the unstable feature Command::replace that is meant to help with cases like this. The tracking issue is #2836. No one has stepped up to see it completed though.

Another option is to use default_value_if. It would look something like

Command::new()
.arg(
    Arg::new("all-headers")
        .short('H')
        .action(ArgAction::SetTrue))
.arg(
    Arg::new("headers-in")
        .long("headers-in")
        .default_value_if("all-headers", Some("true"), Some("true"))
        .action(ArgAction::SetTrue))
.arg(
    Arg::new("headers-out")
        .long("headers-out")
        .short_visible_alias('H')
        .default_value_if("all-headers", Some("true"), Some("true"))
        .action(ArgAction::SetTrue))

@InCogNiTo124
Copy link
Author

The second suggestion seems better than what I've come up with so I'll use this from now on

@epage epage closed this as completed Aug 19, 2022
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