Skip to content

Commit bbfb5bb

Browse files
Eric GumbaEric Gumba
authored andcommitted
fix(clap_mangen): Mangen should 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 859bbc6 commit bbfb5bb

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

clap_mangen/src/render.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ 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
37+
.get_arguments()
38+
.filter(|i| !i.is_hide_set())
39+
.collect();
40+
41+
opts.sort_by_key(|opt| opt.get_display_order());
42+
43+
for opt in opts {
3744
let (lhs, rhs) = option_markers(opt);
3845
match (opt.get_short(), opt.get_long()) {
3946
(Some(short), Some(long)) => {
@@ -89,7 +96,11 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
8996
}
9097

9198
pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
92-
for opt in items.iter().filter(|a| !a.is_positional()) {
99+
100+
let mut sorted_items = items.to_vec();
101+
sorted_items.sort_by_key(|opt| opt.get_display_order());
102+
103+
for opt in sorted_items.iter().filter(|a| !a.is_positional()) {
93104
let mut header = match (opt.get_short(), opt.get_long()) {
94105
(Some(short), Some(long)) => {
95106
vec![short_option(short), roman(", "), long_option(long)]
@@ -131,7 +142,7 @@ pub(crate) fn options(roff: &mut Roff, items: &[&Arg]) {
131142
}
132143
}
133144

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

0 commit comments

Comments
 (0)