clap/tests/derive/groups.rs

123 lines
2.7 KiB
Rust
Raw Normal View History

use clap::Parser;
use crate::utils::assert_output;
#[test]
fn test_safely_nest_parser() {
#[derive(Parser, Debug, PartialEq)]
struct Opt {
#[command(flatten)]
foo: Foo,
}
#[derive(Parser, Debug, PartialEq)]
struct Foo {
#[arg(long)]
foo: bool,
}
assert_eq!(
Opt {
foo: Foo { foo: true }
},
2022-11-24 13:54:25 +00:00
Opt::try_parse_from(["test", "--foo"]).unwrap()
);
}
2022-09-30 14:05:48 +00:00
#[test]
fn implicit_struct_group() {
#[derive(Parser, Debug)]
struct Opt {
#[arg(short, long, requires = "Source")]
add: bool,
#[command(flatten)]
source: Source,
}
#[derive(clap::Args, Debug)]
struct Source {
crates: Vec<String>,
#[arg(long)]
path: Option<std::path::PathBuf>,
#[arg(long)]
git: Option<String>,
}
const OUTPUT: &str = "\
error: The following required arguments were not provided:
<CRATES|--path <PATH>|--git <GIT>>
Usage: prog --add <CRATES|--path <PATH>|--git <GIT>>
For more information try '--help'
";
assert_output::<Opt>("prog --add", OUTPUT, true);
use clap::Args;
assert_eq!(Source::group_id(), Some(clap::Id::from("Source")));
assert_eq!(Opt::group_id(), Some(clap::Id::from("Opt")));
}
2022-09-30 14:05:48 +00:00
#[test]
fn skip_group_avoids_duplicate_ids() {
#[derive(Parser, Debug)]
#[group(skip)]
2022-09-30 14:05:48 +00:00
struct Opt {
#[command(flatten)]
first: Compose<Empty, Empty>,
#[command(flatten)]
second: Compose<Empty, Empty>,
}
#[derive(clap::Args, Debug)]
#[group(skip)]
2022-09-30 14:05:48 +00:00
pub struct Compose<L: clap::Args, R: clap::Args> {
#[clap(flatten)]
pub left: L,
#[clap(flatten)]
pub right: R,
}
#[derive(clap::Args, Clone, Copy, Debug)]
#[group(skip)]
2022-09-30 14:05:48 +00:00
pub struct Empty;
use clap::CommandFactory;
Opt::command().debug_assert();
use clap::Args;
assert_eq!(Empty::group_id(), None);
assert_eq!(Compose::<Empty, Empty>::group_id(), None);
assert_eq!(Opt::group_id(), None);
2022-09-30 14:05:48 +00:00
}
#[test]
#[should_panic = "\
Command clap: Argument group name must be unique
\t'Compose' is already in use (note: `Args` implicitly creates `ArgGroup`s; disable with `#[group(skip)]`"]
fn helpful_panic_on_duplicate_groups() {
#[derive(Parser, Debug)]
struct Opt {
#[command(flatten)]
first: Compose<Empty, Empty>,
#[command(flatten)]
second: Compose<Empty, Empty>,
}
#[derive(clap::Args, Debug)]
pub struct Compose<L: clap::Args, R: clap::Args> {
#[clap(flatten)]
pub left: L,
#[clap(flatten)]
pub right: R,
}
#[derive(clap::Args, Clone, Copy, Debug)]
pub struct Empty;
use clap::CommandFactory;
Opt::command().debug_assert();
}