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

fix(derive): Allow subcommands to directly nest in subcommands #2587

Merged
merged 1 commit into from
Jul 16, 2021

Conversation

epage
Copy link
Member

@epage epage commented Jul 14, 2021

structopt originally allowed

#[derive(Clap)]
pub enum Opt {
  Daemon(DaemonCommand),
}

#[derive(Clap)]
pub enum DaemonCommand {
  Start,
  Stop,
}

This was partially broken in #1681 where $ cmd daemon start works but cmd daemon,
panics. Originally, structopt relied on exposing the implementation
details of a derived type by providing a is_subcommand option, so we'd
know whether to provide SubcommandRequiredElseHelp or not. This was
removed in #1681

With this change, the above code is now:

#[derive(Clap)]
pub enum Opt {
  #[clap(subcommand)]
  Daemon(DaemonCommand),
}

#[derive(Clap)]
pub enum DaemonCommand {
  Start,
  Stop,
}

This can be taken a step further and support optional subcommands in variants like we do fields. That kind of consistency would help in people feeling confident in using the derives.

To implement this, I made it so we no longer re-used attribute validation between struct/enum and variants but instead we have variants broken out separately, like with fields. I tried to ensure the error validation was correct for struct/enum and variants. I think for struct/variant, we only want the default Kind, assuming Flatten and ExternalSubcommand were allowed for variant sake. I didn't see why FromGlobal was allowed on struct/enum/variant, so I made it error in both cases.

Depends on #2586

Fixes #2005

@epage
Copy link
Member Author

epage commented Jul 14, 2021

Until #2586 is landed, see the individual commit for reviewing whats relevant for this PR.

epage added a commit to epage/clap that referenced this pull request Jul 14, 2021
When debugging clap-rs#2586, I noticed we were developing match cases for
variant names that were flattened.  At minimum, this is dead code and at
worst this could cause the wrong behavior if a user does an update with
one of those names.

Depends on clap-rs#2587

Fixes clap-rs#2588
@epage
Copy link
Member Author

epage commented Jul 14, 2021

The mac job got canceled for some reason

epage added a commit to epage/clap that referenced this pull request Jul 14, 2021
When debugging clap-rs#2586, I noticed we were developing match cases for
variant names that were flattened.  At minimum, this is dead code and at
worst this could cause the wrong behavior if a user does an update with
one of those names.

Depends on clap-rs#2587

Fixes clap-rs#2588
epage added a commit to epage/clap that referenced this pull request Jul 14, 2021
When debugging clap-rs#2586, I noticed we were developing match cases for
variant names that were flattened.  At minimum, this is dead code and at
worst this could cause the wrong behavior if a user does an update with
one of those names.

Depends on clap-rs#2587

Fixes clap-rs#2588
epage added a commit to epage/clap that referenced this pull request Jul 15, 2021
When debugging clap-rs#2586, I noticed we were developing match cases for
variant names that were flattened.  At minimum, this is dead code and at
worst this could cause the wrong behavior if a user does an update with
one of those names.

Depends on clap-rs#2587

Fixes clap-rs#2588
`structopt` originally allowed
```
pub enum Opt {
  Daemon(DaemonCommand),
}

pub enum DaemonCommand {
  Start,
  Stop,
}
```

This was partially broken in clap-rs#1681 where `$ cmd daemon start` works but `cmd daemon`,
panics.  Originally, `structopt` relied on exposing the implementation
details of a derived type by providing a `is_subcommand` option, so we'd
know whether to provide `SubcommandRequiredElseHelp` or not.  This was
removed in clap-rs#1681

Fixes clap-rs#2005
@epage
Copy link
Member Author

epage commented Jul 16, 2021

Rebased

@pksunkara pksunkara merged commit fbd8e6d into clap-rs:master Jul 16, 2021
@epage epage deleted the sub branch July 16, 2021 20:39
epage added a commit to epage/clap that referenced this pull request Jul 16, 2021
When debugging clap-rs#2586, I noticed we were developing match cases for
variant names that were flattened.  At minimum, this is dead code and at
worst this could cause the wrong behavior if a user does an update with
one of those names.

Depends on clap-rs#2587

Fixes clap-rs#2588
epage added a commit that referenced this pull request Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use enum as subcommands in a subcommand
2 participants