Skip to content

Commit

Permalink
feat(help): Allow flattening usage
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 9, 2023
1 parent a1fd922 commit caf5cdc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
24 changes: 22 additions & 2 deletions clap_builder/src/output/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,29 @@ impl<'cmd> Usage<'cmd> {
// Creates a usage string for display in help messages (i.e. not for errors)
fn write_help_usage(&self, styled: &mut StyledStr) {
debug!("Usage::write_help_usage");
use std::fmt::Write;

self.write_arg_usage(styled, &[], true);
self.write_subcommand_usage(styled);
if self.cmd.is_flatten_help_set() {
if !self.cmd.is_subcommand_required_set()
|| self.cmd.is_args_conflicts_with_subcommands_set()
{
self.write_arg_usage(styled, &[], true);
styled.trim_end();
let _ = write!(styled, "{}", USAGE_SEP);
}
let mut cmd = self.cmd.clone();
cmd.build();
for (i, sub) in cmd.get_subcommands().enumerate() {
if i != 0 {
styled.trim_end();
let _ = write!(styled, "{}", USAGE_SEP);
}
Usage::new(sub).write_usage_no_title(styled, &[]);
}
} else {
self.write_arg_usage(styled, &[], true);
self.write_subcommand_usage(styled);
}
}

// Creates a context aware usage string, or "smart usage" from currently used
Expand Down
5 changes: 4 additions & 1 deletion examples/git-derive.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ Default subcommand:
```console
$ git-derive stash -h
Usage: git-derive[EXE] stash [OPTIONS]
git-derive[EXE] stash <COMMAND>
git-derive[EXE] stash push [OPTIONS]
git-derive[EXE] stash pop [STASH]
git-derive[EXE] stash apply [STASH]
git-derive[EXE] stash help [COMMAND]...

Commands:
push
Expand Down
5 changes: 4 additions & 1 deletion examples/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ Default subcommand:
```console
$ git stash -h
Usage: git[EXE] stash [OPTIONS]
git[EXE] stash <COMMAND>
git[EXE] stash push [OPTIONS]
git[EXE] stash pop [STASH]
git[EXE] stash apply [STASH]
git[EXE] stash help [COMMAND]...

Commands:
push
Expand Down
54 changes: 43 additions & 11 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2952,7 +2952,9 @@ fn display_name_subcommand_explicit() {
#[test]
fn flatten_basic() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand All @@ -2976,7 +2978,9 @@ Options:
#[test]
fn flatten_short_help() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand Down Expand Up @@ -3006,7 +3010,9 @@ Options:
#[test]
fn flatten_long_help() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand Down Expand Up @@ -3039,7 +3045,9 @@ Options:
#[test]
fn flatten_help_cmd() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand Down Expand Up @@ -3072,7 +3080,9 @@ Options:
#[test]
fn flatten_with_global() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand All @@ -3096,7 +3106,9 @@ Options:
#[test]
fn flatten_arg_required() {
static EXPECTED: &str = "\
Usage: parent --parent <parent> [COMMAND]
Usage: parent --parent <parent>
parent --parent <parent> test --child <child>
parent --parent <parent> help [COMMAND]...
Commands:
test some
Expand All @@ -3120,7 +3132,9 @@ Options:
#[test]
fn flatten_with_external_subcommand() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand Down Expand Up @@ -3160,7 +3174,8 @@ Options:
#[test]
fn flatten_with_subcommand_required() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] <COMMAND>
Usage: parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand All @@ -3186,7 +3201,8 @@ Options:
fn flatten_with_args_conflicts_with_subcommands() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS]
parent <COMMAND>
parent test [OPTIONS]
parent help [COMMAND]...
Commands:
test some
Expand All @@ -3212,7 +3228,19 @@ Options:
#[test]
fn flatten_recursive() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent child1 [OPTIONS]
parent child1 grandchild1 [OPTIONS]
parent child1 grandchild1 greatgrandchild1 [OPTIONS]
parent child1 grandchild1 greatgrandchild2 [OPTIONS]
parent child1 grandchild1 greatgrandchild3 [OPTIONS]
parent child1 grandchild1 help [COMMAND]
parent child1 grandchild2 [OPTIONS]
parent child1 grandchild3 [OPTIONS]
parent child1 help [COMMAND]
parent child2 [OPTIONS]
parent child3 [OPTIONS]
parent help [COMMAND]...
Commands:
child1 some 1
Expand Down Expand Up @@ -3280,7 +3308,11 @@ Options:
#[test]
fn flatten_not_recursive() {
static EXPECTED: &str = "\
Usage: parent [OPTIONS] [COMMAND]
Usage: parent [OPTIONS]
parent child1 [OPTIONS] [COMMAND]
parent child2 [OPTIONS]
parent child3 [OPTIONS]
parent help [COMMAND]...
Commands:
child1 some 1
Expand Down

0 comments on commit caf5cdc

Please sign in to comment.