-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(derive): Ensure App help_heading is applied
We normally set all app attributes at the end. This can be changed but will require some work to ensure - Top-level item's doc cmment ins our over flattened - We still support `Args` / `Subcommand` be used to initialize an `App` when creating a subcommand In the mean time, this special cases `help_heading` to happen first. We'll need this special casing anyways to address #2803 since we'll need to capture the old help heading before addings args and then restore it after. I guess we could unconditionally do that but its extra work / boilerplate for when people have to dig into their what the derives do. Fixes #2785
- Loading branch information
Showing
5 changed files
with
140 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use clap::{Args, IntoApp, Parser}; | ||
|
||
#[test] | ||
fn arg_help_heading_applied() { | ||
#[derive(Debug, Clone, Parser)] | ||
struct CliOptions { | ||
#[clap(long)] | ||
#[clap(help_heading = Some("HEADING A"))] | ||
should_be_in_section_a: Option<u32>, | ||
|
||
#[clap(long)] | ||
no_section: Option<u32>, | ||
} | ||
|
||
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)] | ||
#[clap(help_heading = "DEFAULT")] | ||
struct CliOptions { | ||
#[clap(long)] | ||
#[clap(help_heading = Some("HEADING A"))] | ||
should_be_in_section_a: Option<u32>, | ||
|
||
#[clap(long)] | ||
should_be_in_default_section: Option<u32>, | ||
} | ||
|
||
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() { | ||
#[derive(Debug, Clone, Parser)] | ||
struct CliOptions { | ||
#[clap(flatten)] | ||
options_a: OptionsA, | ||
|
||
#[clap(flatten)] | ||
options_b: OptionsB, | ||
} | ||
|
||
#[derive(Debug, Clone, Args)] | ||
#[clap(help_heading = "HEADING A")] | ||
struct OptionsA { | ||
#[clap(long)] | ||
should_be_in_section_a: Option<u32>, | ||
} | ||
|
||
#[derive(Debug, Clone, Args)] | ||
#[clap(help_heading = "HEADING B")] | ||
struct OptionsB { | ||
#[clap(long)] | ||
should_be_in_section_b: Option<u32>, | ||
} | ||
|
||
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")); | ||
} |