2021-11-02 21:39:06 +00:00
|
|
|
use clap::{AppSettings, Args, IntoApp, Parser, Subcommand};
|
2021-10-12 23:09:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_help_heading_applied() {
|
|
|
|
#[derive(Debug, Clone, Parser)]
|
|
|
|
struct CliOptions {
|
|
|
|
#[clap(long)]
|
|
|
|
#[clap(help_heading = Some("HEADING A"))]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_section_a: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
|
|
|
|
#[clap(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
no_section: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let app = CliOptions::into_app();
|
|
|
|
|
|
|
|
let should_be_in_section_a = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-section-a")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
|
|
|
|
|
|
|
|
let should_be_in_section_b = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "no-section")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_section_b.get_help_heading(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn app_help_heading_applied() {
|
|
|
|
#[derive(Debug, Clone, Parser)]
|
2022-02-07 21:15:40 +00:00
|
|
|
#[clap(next_help_heading = "DEFAULT")]
|
2021-10-12 23:09:23 +00:00
|
|
|
struct CliOptions {
|
|
|
|
#[clap(long)]
|
|
|
|
#[clap(help_heading = Some("HEADING A"))]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_section_a: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
|
|
|
|
#[clap(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_default_section: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let app = CliOptions::into_app();
|
|
|
|
|
|
|
|
let should_be_in_section_a = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-section-a")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
|
|
|
|
|
|
|
|
let should_be_in_default_section = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-default-section")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
should_be_in_default_section.get_help_heading(),
|
|
|
|
Some("DEFAULT")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn app_help_heading_flattened() {
|
2021-11-17 20:23:22 +00:00
|
|
|
// Used to help track the cause in tests
|
|
|
|
#![allow(clippy::enum_variant_names)]
|
|
|
|
|
2021-10-12 23:09:23 +00:00
|
|
|
#[derive(Debug, Clone, Parser)]
|
|
|
|
struct CliOptions {
|
|
|
|
#[clap(flatten)]
|
|
|
|
options_a: OptionsA,
|
|
|
|
|
|
|
|
#[clap(flatten)]
|
|
|
|
options_b: OptionsB,
|
2021-10-15 15:04:37 +00:00
|
|
|
|
|
|
|
#[clap(subcommand)]
|
|
|
|
sub_a: SubA,
|
|
|
|
|
|
|
|
#[clap(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_default_section: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Args)]
|
2022-02-07 21:15:40 +00:00
|
|
|
#[clap(next_help_heading = "HEADING A")]
|
2021-10-12 23:09:23 +00:00
|
|
|
struct OptionsA {
|
|
|
|
#[clap(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_section_a: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Args)]
|
2022-02-07 21:15:40 +00:00
|
|
|
#[clap(next_help_heading = "HEADING B")]
|
2021-10-12 23:09:23 +00:00
|
|
|
struct OptionsB {
|
|
|
|
#[clap(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_section_b: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:04:37 +00:00
|
|
|
#[derive(Debug, Clone, Subcommand)]
|
|
|
|
enum SubA {
|
|
|
|
#[clap(flatten)]
|
|
|
|
SubB(SubB),
|
|
|
|
#[clap(subcommand)]
|
|
|
|
SubC(SubC),
|
|
|
|
SubAOne,
|
2022-02-07 21:15:40 +00:00
|
|
|
#[clap(next_help_heading = "SUB A")]
|
2021-10-15 15:04:37 +00:00
|
|
|
SubATwo {
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_sub_a: u32,
|
2021-10-15 15:04:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Subcommand)]
|
|
|
|
enum SubB {
|
2022-02-07 21:15:40 +00:00
|
|
|
#[clap(next_help_heading = "SUB B")]
|
2021-11-04 15:15:04 +00:00
|
|
|
SubBOne { should_be_in_sub_b: u32 },
|
2021-10-15 15:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Subcommand)]
|
|
|
|
enum SubC {
|
2022-02-07 21:15:40 +00:00
|
|
|
#[clap(next_help_heading = "SUB C")]
|
2021-11-04 15:15:04 +00:00
|
|
|
SubCOne { should_be_in_sub_c: u32 },
|
2021-10-15 15:04:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 23:09:23 +00:00
|
|
|
let app = CliOptions::into_app();
|
|
|
|
|
|
|
|
let should_be_in_section_a = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-section-a")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
|
|
|
|
|
|
|
|
let should_be_in_section_b = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-section-b")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_section_b.get_help_heading(), Some("HEADING B"));
|
2021-10-15 15:04:37 +00:00
|
|
|
|
|
|
|
let should_be_in_default_section = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-default-section")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_default_section.get_help_heading(), None);
|
|
|
|
|
|
|
|
let sub_a_two = app.find_subcommand("sub-a-two").unwrap();
|
|
|
|
|
|
|
|
let should_be_in_sub_a = sub_a_two
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-sub-a")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_sub_a.get_help_heading(), Some("SUB A"));
|
|
|
|
|
|
|
|
let sub_b_one = app.find_subcommand("sub-b-one").unwrap();
|
|
|
|
|
|
|
|
let should_be_in_sub_b = sub_b_one
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-sub-b")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_sub_b.get_help_heading(), Some("SUB B"));
|
|
|
|
|
|
|
|
let sub_c = app.find_subcommand("sub-c").unwrap();
|
|
|
|
let sub_c_one = sub_c.find_subcommand("sub-c-one").unwrap();
|
|
|
|
|
|
|
|
let should_be_in_sub_c = sub_c_one
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-sub-c")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_sub_c.get_help_heading(), Some("SUB C"));
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
2021-10-23 16:12:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_field_with_help_heading() {
|
|
|
|
#[derive(Debug, Clone, Parser)]
|
|
|
|
struct CliOptions {
|
|
|
|
#[clap(flatten)]
|
2022-02-07 21:15:40 +00:00
|
|
|
#[clap(next_help_heading = "HEADING A")]
|
2021-10-23 16:12:37 +00:00
|
|
|
options_a: OptionsA,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Args)]
|
|
|
|
struct OptionsA {
|
|
|
|
#[clap(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_section_a: u32,
|
2021-10-23 16:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let app = CliOptions::into_app();
|
|
|
|
|
|
|
|
let should_be_in_section_a = app
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_name() == "should-be-in-section-a")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
|
|
|
|
}
|
2021-10-29 21:08:59 +00:00
|
|
|
|
|
|
|
// The challenge with this test is creating an error situation not caught by `clap`'s error checking
|
|
|
|
// but by the code that `clap_derive` generates.
|
|
|
|
//
|
|
|
|
// Ultimately, the easiest way to confirm is to put a debug statement in the desired error path.
|
|
|
|
#[test]
|
|
|
|
fn derive_generated_error_has_full_context() {
|
|
|
|
#[derive(Debug, Parser)]
|
2022-02-10 17:51:40 +00:00
|
|
|
#[clap(subcommand_negates_reqs = true)]
|
2021-10-29 21:08:59 +00:00
|
|
|
struct Opts {
|
|
|
|
#[clap(long)]
|
|
|
|
req_str: String,
|
|
|
|
|
|
|
|
#[clap(subcommand)]
|
|
|
|
cmd: Option<SubCommands>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
enum SubCommands {
|
|
|
|
Sub {
|
|
|
|
#[clap(short, long, parse(from_occurrences))]
|
|
|
|
verbose: u8,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = Opts::try_parse_from(&["test", "sub"]);
|
|
|
|
assert!(
|
|
|
|
result.is_err(),
|
|
|
|
"`SubcommandsNegateReqs` with non-optional `req_str` should fail: {:?}",
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected = r#"error: The following required argument was not provided: req-str
|
|
|
|
|
|
|
|
USAGE:
|
2021-11-30 15:36:53 +00:00
|
|
|
clap --req-str <REQ_STR>
|
|
|
|
clap <SUBCOMMAND>
|
2021-10-29 21:08:59 +00:00
|
|
|
|
|
|
|
For more information try --help
|
|
|
|
"#;
|
|
|
|
assert_eq!(result.unwrap_err().to_string(), expected);
|
|
|
|
}
|
2022-02-08 00:17:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn derive_order_next_order() {
|
|
|
|
static HELP: &str = "test 1.2
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test [OPTIONS]
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
--flag-b first flag
|
|
|
|
--option-b <OPTION_B> first option
|
|
|
|
-h, --help Print help information
|
|
|
|
-V, --version Print version information
|
|
|
|
--flag-a second flag
|
|
|
|
--option-a <OPTION_A> second option
|
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[clap(name = "test", version = "1.2")]
|
|
|
|
#[clap(setting = AppSettings::DeriveDisplayOrder)]
|
|
|
|
struct Args {
|
|
|
|
#[clap(flatten)]
|
|
|
|
a: A,
|
|
|
|
#[clap(flatten)]
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
#[clap(next_display_order = 10000)]
|
|
|
|
struct A {
|
|
|
|
/// second flag
|
|
|
|
#[clap(long)]
|
|
|
|
flag_a: bool,
|
|
|
|
/// second option
|
|
|
|
#[clap(long)]
|
|
|
|
option_a: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
#[clap(next_display_order = 10)]
|
|
|
|
struct B {
|
|
|
|
/// first flag
|
|
|
|
#[clap(long)]
|
|
|
|
flag_b: bool,
|
|
|
|
/// first option
|
|
|
|
#[clap(long)]
|
|
|
|
option_b: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
use clap::IntoApp;
|
|
|
|
let mut app = Args::into_app();
|
|
|
|
|
|
|
|
let mut buffer: Vec<u8> = Default::default();
|
|
|
|
app.write_help(&mut buffer).unwrap();
|
|
|
|
let help = String::from_utf8(buffer).unwrap();
|
|
|
|
assert_eq!(help, HELP);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn derive_order_next_order_flatten() {
|
|
|
|
static HELP: &str = "test 1.2
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test [OPTIONS]
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
--flag-b first flag
|
|
|
|
--option-b <OPTION_B> first option
|
|
|
|
-h, --help Print help information
|
|
|
|
-V, --version Print version information
|
|
|
|
--flag-a second flag
|
|
|
|
--option-a <OPTION_A> second option
|
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[clap(setting = AppSettings::DeriveDisplayOrder)]
|
|
|
|
#[clap(name = "test", version = "1.2")]
|
|
|
|
struct Args {
|
|
|
|
#[clap(flatten)]
|
|
|
|
#[clap(next_display_order = 10000)]
|
|
|
|
a: A,
|
|
|
|
#[clap(flatten)]
|
|
|
|
#[clap(next_display_order = 10)]
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct A {
|
|
|
|
/// second flag
|
|
|
|
#[clap(long)]
|
|
|
|
flag_a: bool,
|
|
|
|
/// second option
|
|
|
|
#[clap(long)]
|
|
|
|
option_a: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct B {
|
|
|
|
/// first flag
|
|
|
|
#[clap(long)]
|
|
|
|
flag_b: bool,
|
|
|
|
/// first option
|
|
|
|
#[clap(long)]
|
|
|
|
option_b: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
use clap::IntoApp;
|
|
|
|
let mut app = Args::into_app();
|
|
|
|
|
|
|
|
let mut buffer: Vec<u8> = Default::default();
|
|
|
|
app.write_help(&mut buffer).unwrap();
|
|
|
|
let help = String::from_utf8(buffer).unwrap();
|
|
|
|
assert_eq!(help, HELP);
|
|
|
|
}
|
2022-02-08 01:15:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn derive_order_no_next_order() {
|
|
|
|
static HELP: &str = "test 1.2
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
test [OPTIONS]
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
--flag-a first flag
|
|
|
|
--flag-b second flag
|
|
|
|
-h, --help Print help information
|
|
|
|
--option-a <OPTION_A> first option
|
|
|
|
--option-b <OPTION_B> second option
|
|
|
|
-V, --version Print version information
|
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[clap(name = "test", version = "1.2")]
|
|
|
|
#[clap(setting = AppSettings::DeriveDisplayOrder)]
|
|
|
|
#[clap(next_display_order = None)]
|
|
|
|
struct Args {
|
|
|
|
#[clap(flatten)]
|
|
|
|
a: A,
|
|
|
|
#[clap(flatten)]
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct A {
|
|
|
|
/// first flag
|
|
|
|
#[clap(long)]
|
|
|
|
flag_a: bool,
|
|
|
|
/// first option
|
|
|
|
#[clap(long)]
|
|
|
|
option_a: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct B {
|
|
|
|
/// second flag
|
|
|
|
#[clap(long)]
|
|
|
|
flag_b: bool,
|
|
|
|
/// second option
|
|
|
|
#[clap(long)]
|
|
|
|
option_b: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
use clap::IntoApp;
|
|
|
|
let mut app = Args::into_app();
|
|
|
|
|
|
|
|
let mut buffer: Vec<u8> = Default::default();
|
|
|
|
app.write_help(&mut buffer).unwrap();
|
|
|
|
let help = String::from_utf8(buffer).unwrap();
|
|
|
|
assert_eq!(help, HELP);
|
|
|
|
}
|