Skip to content

Conversation

aparcar
Copy link

@aparcar aparcar commented Oct 7, 2024

The flatten_help argument combines all subcommands on a single page. Until now this wasn't supported for mangen. With this command the sections SYNOPSIS as well as (SUB)COMMANDS are changed to imitate the style of git stash --help.

I reached out via discussions and it looks like this doesn't work just yet #5761

This is not necessarily the most beautiful implementation due to my limited knowledge of both clap and Rust itself.

I implemented this for a project I'm working on and the results look quite okay:

image

Not using man.render() but call each command manually feels a bit awkward but does work just fine, see example here https://github.com/rosenpass/rosenpass/pull/434/files#diff-352422e105afd3138a54ea14e33af782243398f1d768d271655729dd4bcb99d2R18

@aparcar aparcar force-pushed the flat-man branch 3 times, most recently from 3f70e85 to c2c7c26 Compare October 16, 2024 15:15
@aparcar aparcar marked this pull request as ready for review October 16, 2024 16:09
@aparcar aparcar force-pushed the flat-man branch 2 times, most recently from 5094ea2 to 77e56f6 Compare October 17, 2024 13:10
@aparcar aparcar marked this pull request as draft October 17, 2024 13:42
@aparcar
Copy link
Author

aparcar commented Oct 17, 2024

I added a wip commit to handle is_subcommand_required. Still trying to figure out is_args_conflicts_with_subcommands_set Incorporated is_subcommand_required and is_args_conflicts_with_subcommands_set

@aparcar aparcar force-pushed the flat-man branch 2 times, most recently from 7acd84c to 52a7a4c Compare October 18, 2024 09:31
@aparcar aparcar marked this pull request as ready for review October 18, 2024 09:44
@aparcar
Copy link
Author

aparcar commented Oct 21, 2024

I added the differences between flattened --help and flattened Man to the commit description, below are screenshots of what I used for comparison.

--help

image

Man

image

@aparcar
Copy link
Author

aparcar commented Oct 21, 2024

@epage hey Ed I covered your comments as good as I could and think this is ready

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Differences between flattened --help and the flattened man:

  • --help prints the description at the very beginning while Man uses a
    section called DESCRIPTION below the USAGE.
  • --help prints placeholder [OPTIONS] while Man prints all options
  • --help prints positional options (aka arguments) in its own section
    called arguments while Man prints them at the end of OPTIONS.
  • --help prints subcommands as their own section after the options
    section while Man uses an extra section called SUBCOMMANDS
  • --help prints after_long_help at the very end while Man uses the
    section EXTRA which comes before the sections VERSION and AUTHORS

Most of these differences sound unrelated to flattening

@aparcar aparcar force-pushed the flat-man branch 2 times, most recently from 26f3ad8 to 004e815 Compare October 23, 2024 10:18
Comment on lines +116 to +144
#[test]
fn flatten_help_true() {
let name = "my-app";
let cmd = common::basic_command(name).flatten_help(true);
common::assert_matches(snapbox::file!["../snapshots/flatten_help.roff"], cmd);
}

#[test]
fn flatten_help_true_subcommand_required_true() {
let name = "my-app";
let cmd = common::basic_command(name)
.flatten_help(true)
.subcommand_required(true);
common::assert_matches(
snapbox::file!["../snapshots/flatten_help_subcommand_required.roff"],
cmd,
);
}

#[test]
fn flatten_help_true_subcommand_args_conflicts_with_subcommands() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these tests still needed with the addition of the --help tests?

@aparcar aparcar force-pushed the flat-man branch 3 times, most recently from c8d9c77 to d5c2884 Compare February 10, 2025 20:59
@pksunkara pksunkara requested a review from epage June 19, 2025 00:03
aparcar added 4 commits July 10, 2025 10:31
This test shows that the output stays the same independent of the value
of `flatten_help`. Further commits may implement `flatten_help` for
`clap_mangen`.

Signed-off-by: Paul Spooren <[email protected]>
Add all tests from tests/builder/help.rs to mangen, too.

Signed-off-by: Paul Spooren <[email protected]>
The function prints the usage of a (sub)command.

Signed-off-by: Paul Spooren <[email protected]>
The `flatten_help` argument combines all subcommands on a single page.
Until now this wasn't supported for `mangen`. With this command the
sections `SYNOPSIS` as well as `SUBCOMMANDS` are changed to imitate the
style of `git stash --help`.

Differences between flattened `--help` and the flattened man:
* `--help` prints the description at the very beginning while Man uses a
  section called DESCRIPTION below the USAGE.
* `--help` prints placeholder `[OPTIONS]` while Man prints all options
* `--help` prints positional options (aka arguments) in its own section
  called arguments while Man prints them at the end of OPTIONS.
* `--help` prints subcommands as their own section after the options
  section while Man uses an extra section called SUBCOMMANDS
* `--help` prints `after_long_help` at the very end while Man uses the
  section EXTRA which comes before the sections VERSION and AUTHORS

Signed-off-by: Paul Spooren <[email protected]>
Comment on lines +50 to +58
if flatten {
let mut ord_v = Vec::new();
for subcommand in cmd.get_subcommands() {
ord_v.push((
subcommand.get_display_order(),
subcommand.get_bin_name().unwrap_or_else(|| cmd.get_name()),
subcommand,
));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't recursive but should be (same with flat_subcommands)

@epage
Copy link
Member

epage commented Jul 10, 2025

I've cleaned up the commits and rebased but this is still lacking recursion, there is a question on whether tests are redundant, and the PR description seems to be describing more than just flattening.

Copy link
Member

@epage epage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants