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

Create a group from an Args struct #3165

Closed
6 of 7 tasks
epage opened this issue Dec 13, 2021 · 4 comments
Closed
6 of 7 tasks

Create a group from an Args struct #3165

epage opened this issue Dec 13, 2021 · 4 comments
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Experience needed to fix: Medium / intermediate 💸 $20
Milestone

Comments

@epage
Copy link
Member

epage commented Dec 13, 2021

Maintainer's notes


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

I have a group of fields that I want to make a group for conflicting with other fields

Describe the solution you'd like

Inspired by #2621, what if we implicitly created a group from a struct?

#[derive(Args)]
struct Group {
    arg1: String,
    arg2: String
}

would have an implicitly created ArgGroup::new("Group").multiple(true).

You could then do

#[derive(Args)]
#[group(...group builder methods...)]
struct Group {
    arg1: String,
    arg2: String
}

Alternatives, if applicable

Manually define the args and deal with typos in arg and group names and issues like #2475

Additional Context

The big issue is defining this to not conflict with other app or group attributes. Somewhere I brought up the idea of more explicitly naming our attributes as clap::app,. clap::args, etc (#1553). Maybe that can help?

Per-builder attributes are n
ow in

@epage epage added C-enhancement Category: Raise on the bar on expectations A-derive Area: #[derive]` macro API S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing labels Dec 13, 2021
@epage epage changed the title Create a group from an Args Create a group from an Args struct Dec 13, 2021
@epage epage added 💸 $20 E-medium Call for participation: Experience needed to fix: Medium / intermediate and removed S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing labels Sep 13, 2022
@epage
Copy link
Member Author

epage commented Sep 13, 2022

#4210 reserved some functionality in 4.0.0 that we could use for this.

@epage epage added the E-help-wanted Call for participation: Help is requested to fix this issue. label Sep 20, 2022
@epage epage added this to the 4.x milestone Sep 29, 2022
epage added a commit to epage/clap that referenced this issue Sep 30, 2022
This implements the basics for clap-rs#3165, just missing
- `flatten` support (waiting on improved group support)
- `group` attributes
epage added a commit to epage/clap that referenced this issue Sep 30, 2022
This will be needed when we support flattening for clap-rs#3165
epage added a commit to epage/clap that referenced this issue Sep 30, 2022
This will be needed when we support flattening for clap-rs#3165
epage added a commit to epage/clap that referenced this issue Oct 5, 2022
If the inner type never implemented `group_id()`, then it won't work and
people will be confused.  This at least gives people an idea of whats
going wrong.

This is most likely to be a problem until clap-rs#3165 is fully implemented but
hand-implementations can still run into this.  Probably should have made
the groups another trait to catch this in type system but too late.
@ModProg
Copy link
Contributor

ModProg commented Oct 17, 2022

I was actually stumbling on this trying to design a shard set of options for dynamic completions.

Would we support the use case of conflicting groups? I see two ways of doing it with increasing type safety (both not fully supported yet AFAICT):

Semi-Typesafe

Type system ensures completeness of groups

#[derive(Parser)]
struct CompleteArgs {
    #[clap(flatten)]
    register: Option<Register>,
    #[clap(flatten)]
    complete: Option<Complete>,
}
#[derive(Args)]
#[group(conflicts_with = Complete)]
struct Register {
    #[arg(long)]
    shell: String,
}
#[derive(Args)]
struct Complete {
    #[arg(long)]
    current: String,
}

Fully-Typesafe

Type system ensures group conflict
somewhat related to #2621

#[derive(Parser)]
struct CompleteArgs {
    #[clap(arg_group)]
    register_or_complete: RegisterComplete,
}
#[derive(ArgGroups)]
enum RegisterComplete {
    Register(Register),
    Complete(Complete),
}
#[derive(Args)]
struct Register {
    #[arg(long)]
    shell: String,
}
#[derive(Args)]
struct Complete {
    #[arg(long)]
    current: String,
}

@epage
Copy link
Member Author

epage commented Oct 17, 2022

My expectation is we'll eventually support the fully type-safe solution as part of #2621

@epage
Copy link
Member Author

epage commented Mar 28, 2023

I'm going to consider this done with a known bug (#4697)

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 E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Experience needed to fix: Medium / intermediate 💸 $20
Projects
None yet
Development

No branches or pull requests

2 participants