Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions clap_mangen/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
let mut line = vec![bold(name), roman(" ")];

for opt in cmd.get_arguments().filter(|i| !i.is_hide_set()) {
let mut opts: Vec<_> = cmd.get_arguments().filter(|i| !i.is_hide_set()).collect();

opts.sort_by_key(|opt| opt.get_display_order());

for opt in opts {
let (lhs, rhs) = option_markers(opt);
match (opt.get_short(), opt.get_long()) {
(Some(short), Some(long)) => {
Expand Down Expand Up @@ -89,7 +93,10 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
}

pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
for opt in items.iter().filter(|a| !a.is_positional()) {
let mut sorted_items = items.to_vec();
sorted_items.sort_by_key(|opt| opt.get_display_order());

for opt in sorted_items.iter().filter(|a| !a.is_positional()) {
let mut header = match (opt.get_short(), opt.get_long()) {
(Some(short), Some(long)) => {
vec![short_option(short), roman(", "), long_option(long)]
Expand Down Expand Up @@ -131,7 +138,7 @@ pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
}
}

for pos in items.iter().filter(|a| a.is_positional()) {
for pos in sorted_items.iter().filter(|a| a.is_positional()) {
let mut header = vec![];
let (lhs, rhs) = option_markers(pos);
header.push(roman(lhs));
Expand Down
33 changes: 33 additions & 0 deletions clap_mangen/tests/snapshots/configured_display_order_args.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH my-app 1 "my-app "
.SH NAME
my\-app
.SH SYNOPSIS
\fBmy\-app\fR [\fB\-O\fR|\fB\-\-first\fR] [\fB\-P\fR|\fB\-\-second\fR] [\fB\-Q\fR|\fB\-\-third\fR] [\fB\-\-fourth\fR] [\fB\-h\fR|\fB\-\-help\fR] [\fI1st\fR] [\fI2nd\fR] [\fI3rd\fR]
.SH DESCRIPTION
.SH OPTIONS
.TP
\fB\-O\fR, \fB\-\-first\fR
Should be 1st
.TP
\fB\-P\fR, \fB\-\-second\fR
Should be 2nd
.TP
\fB\-Q\fR, \fB\-\-third\fR
Should be 3rd
.TP
\fB\-\-fourth\fR
Should be 4th
.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.TP
[\fI1st\fR]
1st
.TP
[\fI2nd\fR]
2nd
.TP
[\fI3rd\fR]
3rd
37 changes: 37 additions & 0 deletions clap_mangen/tests/testsuite/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,43 @@ pub(crate) fn value_name_without_arg(name: &'static str) -> clap::Command {
)
}


pub(crate) fn configured_display_order_args(name: &'static str) -> clap::Command {
clap::Command::new(name)
.arg(clap::Arg::new("1st").help("1st"))
.arg(clap::Arg::new("2nd").help("2nd"))
.arg(clap::Arg::new("3rd").help("3rd").last(true))
.arg(
clap::Arg::new("c")
.long("third")
.short('Q')
.display_order(3)
.help("Should be 3rd"),
)
.arg(
clap::Arg::new("d")
.long("fourth")
.display_order(4)
.help("Should be 4th"),
)
.arg(
clap::Arg::new("a")
.long("first")
.short('O')
.display_order(1)
.help("Should be 1st"),
)
.arg(
clap::Arg::new("b")
.long("second")
.short('P')
.display_order(2)
.help("Should be 2nd"),
)

}


pub(crate) fn help_headings(name: &'static str) -> clap::Command {
clap::Command::new(name)
.arg(
Expand Down
11 changes: 11 additions & 0 deletions clap_mangen/tests/testsuite/roff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,14 @@ fn value_name_without_arg() {
cmd,
);
}

#[test]
fn configured_display_order_args() {
let name = "my-app";
let cmd = common::configured_display_order_args(name);

common::assert_matches(
snapbox::file!["../snapshots/configured_display_order_args.roff"],
cmd,
);
}
Loading