diff --git a/clap_mangen/tests/snapshots/flatten_arg_required.roff b/clap_mangen/tests/snapshots/flatten_arg_required.roff new file mode 100644 index 00000000000..4e3016bba1c --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_arg_required.roff @@ -0,0 +1,27 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR <\fB\-\-parent\fR> [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent test\fR <\fB\-\-child\fR> [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBtest\fR <\fB\-\-child\fR> [\fB\-h\fR|\fB\-\-help\fR] +test command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_basic.roff b/clap_mangen/tests/snapshots/flatten_basic.roff new file mode 100644 index 00000000000..be64adbdd50 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_basic.roff @@ -0,0 +1,27 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent test\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBtest\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +test command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_help_cmd.roff b/clap_mangen/tests/snapshots/flatten_help_cmd.roff new file mode 100644 index 00000000000..23028212879 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_help_cmd.roff @@ -0,0 +1,27 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent test\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR +bar +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help (see a summary with \*(Aq\-h\*(Aq) +.SH SUBCOMMANDS +.TP +\fBtest\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +long some +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_hidden_command.roff b/clap_mangen/tests/snapshots/flatten_hidden_command.roff new file mode 100644 index 00000000000..21ef1ab791f --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_hidden_command.roff @@ -0,0 +1,34 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBchild1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR] +child1 command +.TP +\fBchild2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR] +child2 command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_not_recursive.roff b/clap_mangen/tests/snapshots/flatten_not_recursive.roff new file mode 100644 index 00000000000..c72e34138bb --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_not_recursive.roff @@ -0,0 +1,37 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBchild1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR] +child1 command +.TP +\fBchild2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR] +child2 command +.TP +\fBchild3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR] +child3 command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_recursive.roff b/clap_mangen/tests/snapshots/flatten_recursive.roff new file mode 100644 index 00000000000..21ef1ab791f --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_recursive.roff @@ -0,0 +1,34 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child3\fR [\fB\-\-child3\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBchild1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR] +child1 command +.TP +\fBchild2\fR [\fB\-\-child2\fR] [\fB\-h\fR|\fB\-\-help\fR] +child2 command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_single_hidden_command.roff b/clap_mangen/tests/snapshots/flatten_single_hidden_command.roff new file mode 100644 index 00000000000..aef2e4c39c3 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_single_hidden_command.roff @@ -0,0 +1,24 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent child1\fR [\fB\-\-child1\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_with_args_conflicts_with_subcommands.roff b/clap_mangen/tests/snapshots/flatten_with_args_conflicts_with_subcommands.roff new file mode 100644 index 00000000000..be64adbdd50 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_with_args_conflicts_with_subcommands.roff @@ -0,0 +1,27 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent test\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBtest\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +test command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_with_external_subcommand.roff b/clap_mangen/tests/snapshots/flatten_with_external_subcommand.roff new file mode 100644 index 00000000000..be64adbdd50 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_with_external_subcommand.roff @@ -0,0 +1,27 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent test\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBtest\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +test command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_with_global.roff b/clap_mangen/tests/snapshots/flatten_with_global.roff new file mode 100644 index 00000000000..e9e273518f2 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_with_global.roff @@ -0,0 +1,27 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent test\fR [\fB\-\-child\fR] [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBtest\fR [\fB\-\-child\fR] [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +test command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_with_subcommand_required.roff b/clap_mangen/tests/snapshots/flatten_with_subcommand_required.roff new file mode 100644 index 00000000000..c6802089606 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_with_subcommand_required.roff @@ -0,0 +1,25 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent test\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +.br +\fBparent help\fR +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help +.SH SUBCOMMANDS +.TP +\fBtest\fR [\fB\-\-child\fR] [\fB\-h\fR|\fB\-\-help\fR] +test command +.TP +\fBhelp\fR +Print this message or the help of the given subcommand(s) diff --git a/clap_mangen/tests/snapshots/flatten_without_subcommands.roff b/clap_mangen/tests/snapshots/flatten_without_subcommands.roff new file mode 100644 index 00000000000..d2855bfb428 --- /dev/null +++ b/clap_mangen/tests/snapshots/flatten_without_subcommands.roff @@ -0,0 +1,16 @@ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.TH parent 1 "parent " +.SH NAME +parent \- parent command +.SH SYNOPSIS +\fBparent\fR [\fB\-\-parent\fR] [\fB\-h\fR|\fB\-\-help\fR] +.SH DESCRIPTION +parent command +.SH OPTIONS +.TP +\fB\-\-parent\fR + +.TP +\fB\-h\fR, \fB\-\-help\fR +Print help diff --git a/clap_mangen/tests/testsuite/roff.rs b/clap_mangen/tests/testsuite/roff.rs index 7831968047c..d7e65fe5943 100644 --- a/clap_mangen/tests/testsuite/roff.rs +++ b/clap_mangen/tests/testsuite/roff.rs @@ -1,4 +1,5 @@ use crate::common; +use clap::{Arg, Command}; #[test] fn basic() { @@ -141,3 +142,323 @@ fn flatten_help_true_subcommand_args_conflicts_with_subcommands() { .args_conflicts_with_subcommands(false); common::assert_matches(snapbox::file!["../snapshots/flatten_help.roff"], cmd); } + +#[test] +fn flatten_basic() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("test") + .about("test command") + .arg(Arg::new("child").long("child")), + ); + + common::assert_matches(snapbox::file!["../snapshots/flatten_basic.roff"], cmd); +} + +#[test] +fn flatten_help_cmd() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg( + Arg::new("parent") + .long("parent") + .help("foo") + .long_help("bar"), + ) + .subcommand( + Command::new("test") + .about("test command") + .long_about("long some") + .arg(Arg::new("child").long("child").help("foo").long_help("bar")), + ); + + common::assert_matches(snapbox::file!["../snapshots/flatten_help_cmd.roff"], cmd); +} + +#[test] +fn flatten_with_global() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent").global(true)) + .subcommand( + Command::new("test") + .about("test command") + .arg(Arg::new("child").long("child")), + ); + + common::assert_matches(snapbox::file!["../snapshots/flatten_with_global.roff"], cmd); +} + +#[test] +fn flatten_arg_required() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent").required(true)) + .subcommand( + Command::new("test") + .about("test command") + .arg(Arg::new("child").long("child").required(true)), + ); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_arg_required.roff"], + cmd, + ); +} + +#[test] +fn flatten_with_external_subcommand() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .allow_external_subcommands(true) + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("test") + .about("test command") + .arg(Arg::new("child").long("child")), + ); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_with_external_subcommand.roff"], + cmd, + ); +} + +#[test] +fn flatten_without_subcommands() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent")); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_without_subcommands.roff"], + cmd, + ); +} + +#[test] +fn flatten_with_subcommand_required() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .subcommand_required(true) + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("test") + .about("test command") + .arg(Arg::new("child").long("child")), + ); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_with_subcommand_required.roff"], + cmd, + ); +} + +#[test] +fn flatten_with_args_conflicts_with_subcommands() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .subcommand_required(true) + .args_conflicts_with_subcommands(true) + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("test") + .about("test command") + .arg(Arg::new("child").long("child")), + ); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_with_args_conflicts_with_subcommands.roff"], + cmd, + ); +} + +#[test] +fn flatten_single_hidden_command() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("child1") + .hide(true) + .about("child1 command") + .arg(Arg::new("child").long("child1")), + ); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_single_hidden_command.roff"], + cmd, + ); +} + +#[test] +fn flatten_hidden_command() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("child1") + .about("child1 command") + .arg(Arg::new("child").long("child1")), + ) + .subcommand( + Command::new("child2") + .about("child2 command") + .arg(Arg::new("child").long("child2")), + ) + .subcommand( + Command::new("child3") + .hide(true) + .about("child3 command") + .arg(Arg::new("child").long("child3")), + ); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_hidden_command.roff"], + cmd, + ); +} + +#[test] +fn flatten_recursive() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("child1") + .flatten_help(true) + .about("child1 command") + .arg(Arg::new("child").long("child1")) + .subcommand( + Command::new("grandchild1") + .flatten_help(true) + .about("grandchild1 command") + .arg(Arg::new("grandchild").long("grandchild1")) + .subcommand( + Command::new("greatgrandchild1") + .about("greatgrandchild1 command") + .arg(Arg::new("greatgrandchild").long("greatgrandchild1")), + ) + .subcommand( + Command::new("greatgrandchild2") + .about("greatgrandchild2 command") + .arg(Arg::new("greatgrandchild").long("greatgrandchild2")), + ) + .subcommand( + Command::new("greatgrandchild3") + .about("greatgrandchild3 command") + .arg(Arg::new("greatgrandchild").long("greatgrandchild3")), + ), + ) + .subcommand( + Command::new("grandchild2") + .about("grandchild2 command") + .arg(Arg::new("grandchild").long("grandchild2")), + ) + .subcommand( + Command::new("grandchild3") + .about("grandchild3 command") + .arg(Arg::new("grandchild").long("grandchild3")), + ), + ) + .subcommand( + Command::new("child2") + .about("child2 command") + .arg(Arg::new("child").long("child2")), + ) + .subcommand( + Command::new("child3") + .hide(true) + .about("child3 command") + .arg(Arg::new("child").long("child3")) + .subcommand( + Command::new("grandchild1") + .flatten_help(true) + .about("grandchild1 command") + .arg(Arg::new("grandchild").long("grandchild1")) + .subcommand( + Command::new("greatgrandchild1") + .about("greatgrandchild1 command") + .arg(Arg::new("greatgrandchild").long("greatgrandchild1")), + ) + .subcommand( + Command::new("greatgrandchild2") + .about("greatgrandchild2 command") + .arg(Arg::new("greatgrandchild").long("greatgrandchild2")), + ) + .subcommand( + Command::new("greatgrandchild3") + .about("greatgrandchild3 command") + .arg(Arg::new("greatgrandchild").long("greatgrandchild3")), + ), + ) + .subcommand( + Command::new("grandchild2") + .about("grandchild2 command") + .arg(Arg::new("grandchild").long("grandchild2")), + ) + .subcommand( + Command::new("grandchild3") + .about("grandchild3 command") + .arg(Arg::new("grandchild").long("grandchild3")), + ), + ); + + common::assert_matches(snapbox::file!["../snapshots/flatten_recursive.roff"], cmd); +} + +#[test] +fn flatten_not_recursive() { + let cmd = Command::new("parent") + .flatten_help(true) + .about("parent command") + .arg(Arg::new("parent").long("parent")) + .subcommand( + Command::new("child1") + .about("child1 command") + .arg(Arg::new("child").long("child1")) + .subcommand( + Command::new("grandchild1") + .about("grandchild1 command") + .arg(Arg::new("grandchild").long("grandchild1")), + ) + .subcommand( + Command::new("grandchild2") + .about("grandchild2 command") + .arg(Arg::new("grandchild").long("grandchild2")), + ) + .subcommand( + Command::new("grandchild3") + .about("grandchild3 command") + .arg(Arg::new("grandchild").long("grandchild3")), + ), + ) + .subcommand( + Command::new("child2") + .about("child2 command") + .arg(Arg::new("child").long("child2")), + ) + .subcommand( + Command::new("child3") + .about("child3 command") + .arg(Arg::new("child").long("child3")), + ); + + common::assert_matches( + snapbox::file!["../snapshots/flatten_not_recursive.roff"], + cmd, + ); +}