2022-08-10 20:13:44 +00:00
|
|
|
use clap::{ArgAction, Args, CommandFactory, Parser, Subcommand};
|
2021-10-12 23:09:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_help_heading_applied() {
|
|
|
|
#[derive(Debug, Clone, Parser)]
|
|
|
|
struct CliOptions {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
|
|
|
#[arg(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
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
no_section: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:04:07 +00:00
|
|
|
let cmd = CliOptions::command();
|
2021-10-12 23:09:23 +00:00
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_section_a = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_section_a")
|
|
|
|
.unwrap();
|
2021-10-12 23:09:23 +00:00
|
|
|
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
|
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_section_b = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "no_section")
|
|
|
|
.unwrap();
|
2021-10-12 23:09:23 +00:00
|
|
|
assert_eq!(should_be_in_section_b.get_help_heading(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn app_help_heading_applied() {
|
|
|
|
#[derive(Debug, Clone, Parser)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(next_help_heading = "DEFAULT")]
|
2021-10-12 23:09:23 +00:00
|
|
|
struct CliOptions {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
|
|
|
#[arg(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
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_default_section: u32,
|
2021-10-12 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:04:07 +00:00
|
|
|
let cmd = CliOptions::command();
|
2021-10-12 23:09:23 +00:00
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_section_a = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_section_a")
|
|
|
|
.unwrap();
|
2021-10-12 23:09:23 +00:00
|
|
|
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
|
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_default_section = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_default_section")
|
|
|
|
.unwrap();
|
2021-10-12 23:09:23 +00:00
|
|
|
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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2021-10-12 23:09:23 +00:00
|
|
|
options_a: OptionsA,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2021-10-12 23:09:23 +00:00
|
|
|
options_b: OptionsB,
|
2021-10-15 15:04:37 +00:00
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-10-15 15:04:37 +00:00
|
|
|
sub_a: SubA,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(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-09-02 20:37:23 +00:00
|
|
|
#[command(next_help_heading = "HEADING A")]
|
2021-10-12 23:09:23 +00:00
|
|
|
struct OptionsA {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(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-09-02 20:37:23 +00:00
|
|
|
#[command(next_help_heading = "HEADING B")]
|
2021-10-12 23:09:23 +00:00
|
|
|
struct OptionsB {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2021-10-15 15:04:37 +00:00
|
|
|
SubB(SubB),
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-10-15 15:04:37 +00:00
|
|
|
SubC(SubC),
|
|
|
|
SubAOne,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(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-09-02 20:37:23 +00:00
|
|
|
#[command(next_help_heading = "SUB B")]
|
2022-07-22 14:19:14 +00:00
|
|
|
SubBOne { should_be_in_sub_b: u32 },
|
2021-10-15 15:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Subcommand)]
|
|
|
|
enum SubC {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(next_help_heading = "SUB C")]
|
2022-07-22 14:19:14 +00:00
|
|
|
SubCOne { should_be_in_sub_c: u32 },
|
2021-10-15 15:04:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:04:07 +00:00
|
|
|
let cmd = CliOptions::command();
|
2021-10-12 23:09:23 +00:00
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_section_a = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_section_a")
|
|
|
|
.unwrap();
|
2021-10-12 23:09:23 +00:00
|
|
|
assert_eq!(should_be_in_section_a.get_help_heading(), Some("HEADING A"));
|
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_section_b = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_section_b")
|
|
|
|
.unwrap();
|
2021-10-12 23:09:23 +00:00
|
|
|
assert_eq!(should_be_in_section_b.get_help_heading(), Some("HEADING B"));
|
2021-10-15 15:04:37 +00:00
|
|
|
|
2022-09-16 20:26:34 +00:00
|
|
|
let should_be_in_section_b = cmd
|
2022-07-22 14:19:14 +00:00
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_default_section")
|
|
|
|
.unwrap();
|
2022-09-16 20:26:34 +00:00
|
|
|
assert_eq!(should_be_in_section_b.get_help_heading(), Some("HEADING B"));
|
2021-10-15 15:04:37 +00:00
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let sub_a_two = cmd.find_subcommand("sub-a-two").unwrap();
|
2021-10-15 15:04:37 +00:00
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_sub_a = sub_a_two
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_sub_a")
|
|
|
|
.unwrap();
|
2021-10-15 15:04:37 +00:00
|
|
|
assert_eq!(should_be_in_sub_a.get_help_heading(), Some("SUB A"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let sub_b_one = cmd.find_subcommand("sub-b-one").unwrap();
|
2021-10-15 15:04:37 +00:00
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_sub_b = sub_b_one
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_sub_b")
|
|
|
|
.unwrap();
|
2021-10-15 15:04:37 +00:00
|
|
|
assert_eq!(should_be_in_sub_b.get_help_heading(), Some("SUB B"));
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
let sub_c = cmd.find_subcommand("sub-c").unwrap();
|
2021-10-15 15:04:37 +00:00
|
|
|
let sub_c_one = sub_c.find_subcommand("sub-c-one").unwrap();
|
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_sub_c = sub_c_one
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_sub_c")
|
|
|
|
.unwrap();
|
2021-10-15 15:04:37 +00:00
|
|
|
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 {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
|
|
|
#[command(next_help_heading = "HEADING A")]
|
2021-10-23 16:12:37 +00:00
|
|
|
options_a: OptionsA,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Args)]
|
|
|
|
struct OptionsA {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-11-04 15:15:04 +00:00
|
|
|
should_be_in_section_a: u32,
|
2021-10-23 16:12:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:04:07 +00:00
|
|
|
let cmd = CliOptions::command();
|
2021-10-23 16:12:37 +00:00
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let should_be_in_section_a = cmd
|
|
|
|
.get_arguments()
|
|
|
|
.find(|a| a.get_id() == "should_be_in_section_a")
|
|
|
|
.unwrap();
|
2021-10-23 16:12:37 +00:00
|
|
|
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-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand_negates_reqs = true)]
|
2021-10-29 21:08:59 +00:00
|
|
|
struct Opts {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-10-29 21:08:59 +00:00
|
|
|
req_str: String,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(subcommand)]
|
2021-10-29 21:08:59 +00:00
|
|
|
cmd: Option<SubCommands>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
enum SubCommands {
|
|
|
|
Sub {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, action = clap::ArgAction::Count)]
|
2022-06-09 16:09:36 +00:00
|
|
|
verbose: u8,
|
2021-10-29 21:08:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:54:25 +00:00
|
|
|
let result = Opts::try_parse_from(["test", "sub"]);
|
2021-10-29 21:08:59 +00:00
|
|
|
assert!(
|
|
|
|
result.is_err(),
|
|
|
|
"`SubcommandsNegateReqs` with non-optional `req_str` should fail: {:?}",
|
|
|
|
result.unwrap()
|
|
|
|
);
|
|
|
|
|
2022-07-22 14:19:14 +00:00
|
|
|
let expected = r#"error: The following required argument was not provided: req_str
|
2022-05-09 15:35:52 +00:00
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: clap --req-str <REQ_STR>
|
|
|
|
clap <COMMAND>
|
2022-05-09 15:35:52 +00:00
|
|
|
|
2023-01-03 19:22:35 +00:00
|
|
|
For more information, try '--help'.
|
2022-05-09 15:35:52 +00:00
|
|
|
"#;
|
2022-07-22 14:19:14 +00:00
|
|
|
assert_eq!(result.unwrap_err().to_string(), expected);
|
2021-10-29 21:08:59 +00:00
|
|
|
}
|
2022-02-08 00:17:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn derive_order_next_order() {
|
2022-08-31 14:29:00 +00:00
|
|
|
static HELP: &str = "\
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test [OPTIONS]
|
2022-02-08 00:17:27 +00:00
|
|
|
|
2022-08-26 14:40:23 +00:00
|
|
|
Options:
|
2022-09-07 20:29:15 +00:00
|
|
|
--flag-b first flag
|
|
|
|
--option-b <OPTION_B> first option
|
2023-01-03 16:49:43 +00:00
|
|
|
-h, --help Print help
|
|
|
|
-V, --version Print version
|
2022-09-07 20:29:15 +00:00
|
|
|
--flag-a second flag
|
|
|
|
--option-a <OPTION_A> second option
|
2022-02-08 00:17:27 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(name = "test", version = "1.2")]
|
2022-02-08 00:17:27 +00:00
|
|
|
struct Args {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2022-02-08 00:17:27 +00:00
|
|
|
a: A,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2022-02-08 00:17:27 +00:00
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(next_display_order = 10000)]
|
2022-02-08 00:17:27 +00:00
|
|
|
struct A {
|
|
|
|
/// second flag
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
flag_a: bool,
|
|
|
|
/// second option
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
option_a: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(next_display_order = 10)]
|
2022-02-08 00:17:27 +00:00
|
|
|
struct B {
|
|
|
|
/// first flag
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
flag_b: bool,
|
|
|
|
/// first option
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
option_b: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:09:38 +00:00
|
|
|
use clap::CommandFactory;
|
2022-02-14 22:04:07 +00:00
|
|
|
let mut cmd = Args::command();
|
2022-02-08 00:17:27 +00:00
|
|
|
|
2022-09-22 14:39:24 +00:00
|
|
|
let help = cmd.render_help().to_string();
|
2022-06-03 18:25:17 +00:00
|
|
|
snapbox::assert_eq(HELP, help);
|
2022-02-08 00:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn derive_order_next_order_flatten() {
|
2022-08-31 14:29:00 +00:00
|
|
|
static HELP: &str = "\
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test [OPTIONS]
|
2022-02-08 00:17:27 +00:00
|
|
|
|
2022-08-26 14:40:23 +00:00
|
|
|
Options:
|
2022-09-07 20:29:15 +00:00
|
|
|
--flag-b first flag
|
|
|
|
--option-b <OPTION_B> first option
|
2023-01-03 16:49:43 +00:00
|
|
|
-h, --help Print help
|
|
|
|
-V, --version Print version
|
2022-09-07 20:29:15 +00:00
|
|
|
--flag-a second flag
|
|
|
|
--option-a <OPTION_A> second option
|
2022-02-08 00:17:27 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(name = "test", version = "1.2")]
|
2022-02-08 00:17:27 +00:00
|
|
|
struct Args {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
|
|
|
#[command(next_display_order = 10000)]
|
2022-02-08 00:17:27 +00:00
|
|
|
a: A,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
|
|
|
#[command(next_display_order = 10)]
|
2022-02-08 00:17:27 +00:00
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct A {
|
|
|
|
/// second flag
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
flag_a: bool,
|
|
|
|
/// second option
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
option_a: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct B {
|
|
|
|
/// first flag
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
flag_b: bool,
|
|
|
|
/// first option
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 00:17:27 +00:00
|
|
|
option_b: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:09:38 +00:00
|
|
|
use clap::CommandFactory;
|
2022-02-14 22:04:07 +00:00
|
|
|
let mut cmd = Args::command();
|
2022-02-08 00:17:27 +00:00
|
|
|
|
2022-09-22 14:39:24 +00:00
|
|
|
let help = cmd.render_help().to_string();
|
2022-06-03 18:25:17 +00:00
|
|
|
snapbox::assert_eq(HELP, help);
|
2022-02-08 00:17:27 +00:00
|
|
|
}
|
2022-02-08 01:15:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn derive_order_no_next_order() {
|
2022-08-31 14:29:00 +00:00
|
|
|
static HELP: &str = "\
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: test [OPTIONS]
|
2022-02-08 01:15:45 +00:00
|
|
|
|
2022-08-26 14:40:23 +00:00
|
|
|
Options:
|
2022-09-07 20:29:15 +00:00
|
|
|
--flag-a first flag
|
|
|
|
--flag-b second flag
|
2023-01-03 16:49:43 +00:00
|
|
|
-h, --help Print help
|
2022-09-07 20:29:15 +00:00
|
|
|
--option-a <OPTION_A> first option
|
|
|
|
--option-b <OPTION_B> second option
|
2023-01-03 16:49:43 +00:00
|
|
|
-V, --version Print version
|
2022-02-08 01:15:45 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(name = "test", version = "1.2")]
|
|
|
|
#[command(next_display_order = None)]
|
2022-02-08 01:15:45 +00:00
|
|
|
struct Args {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2022-02-08 01:15:45 +00:00
|
|
|
a: A,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(flatten)]
|
2022-02-08 01:15:45 +00:00
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct A {
|
|
|
|
/// first flag
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 01:15:45 +00:00
|
|
|
flag_a: bool,
|
|
|
|
/// first option
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 01:15:45 +00:00
|
|
|
option_a: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct B {
|
|
|
|
/// second flag
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 01:15:45 +00:00
|
|
|
flag_b: bool,
|
|
|
|
/// second option
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2022-02-08 01:15:45 +00:00
|
|
|
option_b: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:09:38 +00:00
|
|
|
use clap::CommandFactory;
|
2022-02-14 22:04:07 +00:00
|
|
|
let mut cmd = Args::command();
|
2022-02-08 01:15:45 +00:00
|
|
|
|
2022-09-22 14:39:24 +00:00
|
|
|
let help = cmd.render_help().to_string();
|
2022-06-03 18:25:17 +00:00
|
|
|
snapbox::assert_eq(HELP, help);
|
2022-02-08 01:15:45 +00:00
|
|
|
}
|
2022-06-04 12:02:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn derive_possible_value_help() {
|
2022-08-31 14:29:00 +00:00
|
|
|
static HELP: &str = "\
|
2022-06-04 12:02:20 +00:00
|
|
|
Application help
|
|
|
|
|
2022-09-07 16:03:55 +00:00
|
|
|
Usage: clap <ARG>
|
2022-06-04 12:02:20 +00:00
|
|
|
|
2022-08-26 14:40:23 +00:00
|
|
|
Arguments:
|
2022-09-07 20:29:15 +00:00
|
|
|
<ARG>
|
|
|
|
Argument help
|
2022-06-04 12:02:20 +00:00
|
|
|
|
2022-09-07 20:29:15 +00:00
|
|
|
Possible values:
|
|
|
|
- foo: Foo help
|
|
|
|
- bar: Bar help
|
2022-06-04 12:02:20 +00:00
|
|
|
|
2022-08-26 14:40:23 +00:00
|
|
|
Options:
|
2022-09-07 20:29:15 +00:00
|
|
|
-h, --help
|
2023-01-03 16:49:43 +00:00
|
|
|
Print help (see a summary with '-h')
|
2022-06-04 12:02:20 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
/// Application help
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Args {
|
|
|
|
/// Argument help
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_enum)]
|
2022-06-04 12:02:20 +00:00
|
|
|
arg: ArgChoice,
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:56:57 +00:00
|
|
|
#[derive(clap::ValueEnum, PartialEq, Debug, Clone)]
|
2022-06-04 12:02:20 +00:00
|
|
|
enum ArgChoice {
|
|
|
|
/// Foo help
|
|
|
|
Foo,
|
|
|
|
/// Bar help
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
use clap::CommandFactory;
|
|
|
|
let mut cmd = Args::command();
|
|
|
|
|
2022-09-22 14:39:24 +00:00
|
|
|
let help = cmd.render_long_help().to_string();
|
2022-06-04 12:02:20 +00:00
|
|
|
snapbox::assert_eq(HELP, help);
|
|
|
|
}
|
2022-08-10 20:13:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_help_flag() {
|
|
|
|
#[derive(Debug, Clone, Parser)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(disable_help_flag = true)]
|
2022-08-10 20:13:44 +00:00
|
|
|
struct CliOptions {
|
2022-10-11 15:29:19 +00:00
|
|
|
#[arg(short = 'h', long = "verbose-help", action = ArgAction::Help, value_parser = clap::value_parser!(bool))]
|
|
|
|
help: (),
|
2022-08-10 20:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = CliOptions::try_parse_from(["cmd", "--verbose-help"]);
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp);
|
2022-10-11 15:29:19 +00:00
|
|
|
|
|
|
|
CliOptions::try_parse_from(["cmd"]).unwrap();
|
2022-08-10 20:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_version_flag() {
|
|
|
|
#[derive(Debug, Clone, Parser)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(disable_version_flag = true, version = "2.0.0")]
|
2022-08-10 20:13:44 +00:00
|
|
|
struct CliOptions {
|
2022-10-11 15:29:19 +00:00
|
|
|
#[arg(short = 'V', long = "verbose-version", action = ArgAction::Version, value_parser = clap::value_parser!(bool))]
|
|
|
|
version: (),
|
2022-08-10 20:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = CliOptions::try_parse_from(["cmd", "--verbose-version"]);
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(err.kind(), clap::error::ErrorKind::DisplayVersion);
|
2022-10-11 15:29:19 +00:00
|
|
|
|
|
|
|
CliOptions::try_parse_from(["cmd"]).unwrap();
|
2022-08-10 20:13:44 +00:00
|
|
|
}
|