Skip to content

Commit

Permalink
feat(help): Allow controlling flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 9, 2023
1 parent 3383242 commit a1fd922
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 0 deletions.
1 change: 1 addition & 0 deletions clap_builder/src/builder/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub(crate) enum AppSettings {
SubcommandsNegateReqs,
ArgsNegateSubcommands,
SubcommandPrecedenceOverArg,
FlattenHelp,
ArgRequiredElseHelp,
NextLineHelp,
DisableColoredHelp,
Expand Down
21 changes: 21 additions & 0 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,21 @@ impl Command {
self
}

/// Flatten subcommand help into the current command's help
///
/// This shows a summary of subcommands within the usage and help for the current command, similar to
/// `git stash --help` showing information on `push`, `pop`, etc.
/// To see more information, a user can still pass `--help` to the individual subcommands.
#[inline]
#[must_use]
pub fn flatten_help(self, yes: bool) -> Self {
if yes {
self.setting(AppSettings::FlattenHelp)
} else {
self.unset_setting(AppSettings::FlattenHelp)
}
}

/// Set the default section heading for future args.
///
/// This will be used for any arg that hasn't had [`Arg::help_heading`] called.
Expand Down Expand Up @@ -3430,6 +3445,12 @@ impl Command {
self.long_about.as_ref()
}

/// Get the custom section heading specified via [`Command::flatten_help`].
#[inline]
pub fn is_flatten_help_set(&self) -> bool {
self.is_set(AppSettings::FlattenHelp)
}

/// Get the custom section heading specified via [`Command::next_help_heading`].
///
/// [`Command::help_heading`]: Command::help_heading()
Expand Down
1 change: 1 addition & 0 deletions examples/git-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl std::fmt::Display for ColorWhen {

#[derive(Debug, Args)]
#[command(args_conflicts_with_subcommands = true)]
#[command(flatten_help = true)]
struct StashArgs {
#[command(subcommand)]
command: Option<StashCommands>,
Expand Down
1 change: 1 addition & 0 deletions examples/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn cli() -> Command {
.subcommand(
Command::new("stash")
.args_conflicts_with_subcommands(true)
.flatten_help(true)
.args(push_args())
.subcommand(Command::new("push").args(push_args()))
.subcommand(Command::new("pop").arg(arg!([STASH])))
Expand Down
Loading

0 comments on commit a1fd922

Please sign in to comment.