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

Make it easier to use clap validators with derive by generating arg associated constants? #3171

Open
2 tasks done
epage opened this issue Dec 14, 2021 · 1 comment
Open
2 tasks done
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state.

Comments

@epage
Copy link
Member

epage commented Dec 14, 2021

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Clap Version

3.0.0-rc.4

Describe your use case

When deriving, if you define a group, deal with requires, conflicts, overrides, etc, then you need to reference the field name. You can type it out and App::debug_assert will fail if you get it wrong.

Example

#[derive(Debug, Parser)]
struct Opts {
    #[clap(long, conflicts_with = "hello-world")] // I thought it should be hello_world
    one: Option<String>,
    #[clap(long)]
    hello_world: Option<String>,
}

Describe the solution you'd like

What if instead we generated something like

impl Opt {
    const ONE_CLAP_ARG: &str = "one";
    const HELLO_WORLD_CLAP_ARG: &str = "hello-world";
}

So you could do

#[derive(Debug, Parser)]
struct Opts {
    #[clap(long, conflicts_with = Opts::HELLO_WORLD_CLAP_ARG)]
    one: Option<String>,
    #[clap(long)]
    hello_world: Option<String>,
}

I'm assuming this will work with macro expansion

Alternatives, if applicable

Provide helpers to avoid it, like #3165

Additional Context

This was inspired by #2475

@epage epage added C-enhancement Category: Raise on the bar on expectations A-derive Area: #[derive]` macro API S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. labels Dec 14, 2021
@epage
Copy link
Member Author

epage commented Feb 10, 2022

Benefits:

  • Move errors to compile time

Downsides

  • I personally find it confusing accessing items generated by a macro that are unrelated to the trait
  • Potential name conflicts

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 S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state.
Projects
None yet
Development

No branches or pull requests

1 participant