Skip to content

Commit 521f01d

Browse files
Eric GumbaEric Gumba
authored andcommitted
fix(clap_mangen): Take into consideration display_order
In #3362 we have an issue where when we configure an arg via .display_order(int), and then generate a manpage, the synposis and options will render the order the args were provided to the App rather than the order they were configured e.g Command::new(name) arg(Arg::new("few").short('b').display_order(2)) arg(Arg::new("bar").short('a').display_order(1)) will show ... SYNOPSIS <name> [-b] [-a] ... ... OPTIONS -b -a instead of ... SYNOPSIS <name> [-a] [-b] ... ... OPTIONS -a -b and so on. This fix adds sorting in the synopsis and options functions responsible for generating the corresponding synopsis and options sections of the manpage.
1 parent 1d261e4 commit 521f01d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

clap_mangen/src/render.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
3333
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
3434
let mut line = vec![bold(name), roman(" ")];
3535

36-
for opt in cmd.get_arguments().filter(|i| !i.is_hide_set()) {
36+
let mut opts: Vec<_> = cmd.get_arguments().filter(|i| !i.is_hide_set()).collect();
37+
38+
opts.sort_by_key(|opt| opt.get_display_order());
39+
40+
for opt in opts {
3741
let (lhs, rhs) = option_markers(opt);
3842
match (opt.get_short(), opt.get_long()) {
3943
(Some(short), Some(long)) => {
@@ -89,7 +93,10 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
8993
}
9094

9195
pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
92-
for opt in items.iter().filter(|a| !a.is_positional()) {
96+
let mut sorted_items = items.to_vec();
97+
sorted_items.sort_by_key(|opt| opt.get_display_order());
98+
99+
for opt in sorted_items.iter().filter(|a| !a.is_positional()) {
93100
let mut header = match (opt.get_short(), opt.get_long()) {
94101
(Some(short), Some(long)) => {
95102
vec![short_option(short), roman(", "), long_option(long)]
@@ -131,7 +138,7 @@ pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
131138
}
132139
}
133140

134-
for pos in items.iter().filter(|a| a.is_positional()) {
141+
for pos in sorted_items.iter().filter(|a| a.is_positional()) {
135142
let mut header = vec![];
136143
let (lhs, rhs) = option_markers(pos);
137144
header.push(roman(lhs));

0 commit comments

Comments
 (0)