diff --git a/clap_builder/src/output/help_template.rs b/clap_builder/src/output/help_template.rs index 3b3fc3d9840..b968ad97d61 100644 --- a/clap_builder/src/output/help_template.rs +++ b/clap_builder/src/output/help_template.rs @@ -6,6 +6,7 @@ // Std use std::borrow::Cow; use std::cmp; +use std::collections::BTreeMap; // Internal use crate::builder::PossibleValue; @@ -467,7 +468,7 @@ impl HelpTemplate<'_, '_> { debug!("HelpTemplate::write_args {_category}"); // The shortest an arg can legally be is 2 (i.e. '-x') let mut longest = 2; - let mut ord_v = Vec::new(); + let mut ord_v = BTreeMap::new(); // Determine the longest for &arg in args.iter().filter(|arg| { @@ -486,9 +487,8 @@ impl HelpTemplate<'_, '_> { } let key = (sort_key)(arg); - ord_v.push((key, arg)); + ord_v.insert(key, arg); } - ord_v.sort_by(|a, b| a.0.cmp(&b.0)); let next_line_help = self.will_args_wrap(args, longest); @@ -858,19 +858,17 @@ impl HelpTemplate<'_, '_> { use std::fmt::Write as _; let header = &self.styles.get_header(); - let mut ord_v = Vec::new(); + let mut ord_v = BTreeMap::new(); for subcommand in cmd .get_subcommands() .filter(|subcommand| should_show_subcommand(subcommand)) { - ord_v.push(( - subcommand.get_display_order(), - subcommand.get_name(), + ord_v.insert( + (subcommand.get_display_order(), subcommand.get_name()), subcommand, - )); + ); } - ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1))); - for (_, _, subcommand) in ord_v { + for (_, subcommand) in ord_v { if !*first { self.writer.push_str("\n\n"); } @@ -915,7 +913,7 @@ impl HelpTemplate<'_, '_> { // The shortest an arg can legally be is 2 (i.e. '-x') let mut longest = 2; - let mut ord_v = Vec::new(); + let mut ord_v = BTreeMap::new(); for subcommand in cmd .get_subcommands() .filter(|subcommand| should_show_subcommand(subcommand)) @@ -930,19 +928,18 @@ impl HelpTemplate<'_, '_> { let _ = write!(styled, ", {literal}--{long}{literal:#}",); } longest = longest.max(styled.display_width()); - ord_v.push((subcommand.get_display_order(), styled, subcommand)); + ord_v.insert((subcommand.get_display_order(), styled), subcommand); } - ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1))); debug!("HelpTemplate::write_subcommands longest = {longest}"); let next_line_help = self.will_subcommands_wrap(cmd.get_subcommands(), longest); - for (i, (_, sc_str, sc)) in ord_v.into_iter().enumerate() { + for (i, (sc_str, sc)) in ord_v.into_iter().enumerate() { if 0 < i { self.writer.push_str("\n"); } - self.write_subcommand(sc_str, sc, next_line_help, longest); + self.write_subcommand(sc_str.1, sc, next_line_help, longest); } }