clap/clap_derive/tests/issues.rs
Ed Page 7f08773a5a fix(derive)!: Compile-error on nested subcommands
Before, partial command lines would panic at runtime.  Now it'll be a
compile error

For example:
```
pub enum Opt {
  Daemon(DaemonCommand),
}

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

Gives:
```
error[E0277]: the trait bound `DaemonCommand: clap::Args` is not satisfied
   --> clap_derive/tests/subcommands.rs:297:16
    |
297 |         Daemon(DaemonCommand),
    |                ^^^^^^^^^^^^^ the trait `clap::Args` is not implemented for `DaemonCommand`
    |
    = note: required by `augment_args`
```

To nest this, you currently need `enum -> struct -> enum`.  A later
change will make it so you can use the `subcommand` attribute within
enums to cover this case.

This is a part of #2005
2021-07-15 11:45:13 -05:00

78 lines
1.9 KiB
Rust

// https://github.com/TeXitoi/structopt/issues/{NUMBER}
mod utils;
use utils::*;
use clap::{AppSettings, ArgGroup, Clap};
#[test]
fn issue_151() {
#[derive(Clap, Debug)]
#[clap(group = ArgGroup::new("verb").required(true).multiple(true))]
struct Opt {
#[clap(long, group = "verb")]
foo: bool,
#[clap(long, group = "verb")]
bar: bool,
}
#[derive(Debug, Clap)]
struct Cli {
#[clap(flatten)]
a: Opt,
}
assert!(Cli::try_parse_from(&["test"]).is_err());
assert!(Cli::try_parse_from(&["test", "--foo"]).is_ok());
assert!(Cli::try_parse_from(&["test", "--bar"]).is_ok());
assert!(Cli::try_parse_from(&["test", "--zebra"]).is_err());
assert!(Cli::try_parse_from(&["test", "--foo", "--bar"]).is_ok());
}
#[test]
fn issue_289() {
#[derive(Clap)]
#[clap(setting = AppSettings::InferSubcommands)]
enum Args {
SomeCommand {
#[clap(subcommand)]
sub: SubSubCommand,
},
AnotherCommand,
}
// FIXME (@CreepySkeleton): current implementation requires us to
// derive IntoApp here while we don't really need it
#[derive(Clap)]
#[clap(setting = AppSettings::InferSubcommands)]
enum SubSubCommand {
TestCommand,
}
assert!(Args::try_parse_from(&["test", "some-command", "test-command"]).is_ok());
assert!(Args::try_parse_from(&["test", "some", "test-command"]).is_ok());
assert!(Args::try_parse_from(&["test", "some-command", "test"]).is_ok());
assert!(Args::try_parse_from(&["test", "some", "test"]).is_ok());
}
#[test]
fn issue_324() {
fn my_version() -> &'static str {
"MY_VERSION"
}
#[derive(Clap)]
#[clap(version = my_version())]
struct Opt {
#[clap(subcommand)]
_cmd: Option<SubCommand>,
}
#[derive(Clap)]
enum SubCommand {
Start,
}
let help = get_long_help::<Opt>();
assert!(help.contains("MY_VERSION"));
}