Skip to content

Commit 6cda032

Browse files
committed
wip: Add show_aliases option + change where aliases appear
1 parent 6bf1c39 commit 6cda032

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

docs/examples/complex-app.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ does testing things
5050

5151
**Usage:** `complex-app test [OPTIONS]`
5252

53+
**Command Alias:** `tester`
54+
5355
###### **Options:**
5456

5557
* `-l`, `--list` — lists test values

src/lib.rs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ mod test_readme {
1212
#![doc = include_str!("../README.md")]
1313
}
1414

15+
mod utils;
16+
1517
use std::fmt::{self, Write};
1618

1719
use clap::builder::PossibleValue;
1820

21+
use utils::pluralize;
22+
1923
//======================================
2024
// Public API types
2125
//======================================
@@ -28,6 +32,7 @@ pub struct MarkdownOptions {
2832
title: Option<String>,
2933
show_footer: bool,
3034
show_table_of_contents: bool,
35+
show_aliases: bool,
3136
}
3237

3338
impl MarkdownOptions {
@@ -37,6 +42,7 @@ impl MarkdownOptions {
3742
title: None,
3843
show_footer: true,
3944
show_table_of_contents: true,
45+
show_aliases: true,
4046
};
4147
}
4248

@@ -60,6 +66,13 @@ impl MarkdownOptions {
6066

6167
return self;
6268
}
69+
70+
/// Whether to show aliases for arguments and commands.
71+
pub fn show_aliases(mut self, show: bool) -> Self {
72+
self.show_aliases = show;
73+
74+
return self;
75+
}
6376
}
6477

6578
impl Default for MarkdownOptions {
@@ -166,7 +179,7 @@ fn write_help_markdown(
166179
// Write the commands/subcommands sections
167180
//----------------------------------------
168181

169-
build_command_markdown(buffer, Vec::new(), command, 0).unwrap();
182+
build_command_markdown(buffer, Vec::new(), command, 0, options).unwrap();
170183

171184
//-----------------
172185
// Write the footer
@@ -278,6 +291,7 @@ fn build_command_markdown(
278291
parent_command_path: Vec<String>,
279292
command: &clap::Command,
280293
depth: usize,
294+
options: &MarkdownOptions,
281295
) -> std::fmt::Result {
282296
// Don't document commands marked with `clap(hide = true)` (which includes
283297
// `print-all-help`).
@@ -307,12 +321,7 @@ fn build_command_markdown(
307321
)
308322
}
309323
*/
310-
let aliases = command.get_visible_aliases().collect::<Vec<&str>>();
311-
let aliases = get_alias_str(&aliases)
312-
.map(|s| format!(" {s}"))
313-
.unwrap_or_default();
314-
315-
writeln!(buffer, "## `{}`{aliases}\n", command_path.join(" "),)?;
324+
writeln!(buffer, "## `{}`\n", command_path.join(" "))?;
316325

317326
if let Some(long_about) = command.get_long_about() {
318327
writeln!(buffer, "{}\n", long_about)?;
@@ -343,6 +352,17 @@ fn build_command_markdown(
343352
.replace("Usage: ", "")
344353
)?;
345354

355+
if options.show_aliases {
356+
let aliases = command.get_visible_aliases().collect::<Vec<&str>>();
357+
if let Some(aliases_str) = get_alias_string(&aliases) {
358+
writeln!(
359+
buffer,
360+
"**{}:** {aliases_str}\n",
361+
pluralize(aliases.len(), "Command Alias", "Command Aliases")
362+
)?;
363+
}
364+
}
365+
346366
if let Some(help) = command.get_after_long_help() {
347367
writeln!(buffer, "{}\n", help)?;
348368
} else if let Some(help) = command.get_after_help() {
@@ -362,7 +382,8 @@ fn build_command_markdown(
362382
}
363383

364384
let title_name = get_canonical_name(subcommand);
365-
let aliases = subcommand.get_visible_aliases().collect::<Vec<&str>>();
385+
let aliases =
386+
subcommand.get_visible_aliases().collect::<Vec<&str>>();
366387
let aliases = get_alias_str(&aliases)
367388
.map(|s| format!(" {s}"))
368389
.unwrap_or_default();
@@ -425,6 +446,7 @@ fn build_command_markdown(
425446
command_path.clone(),
426447
subcommand,
427448
depth + 1,
449+
options,
428450
)?;
429451
}
430452

@@ -474,8 +496,12 @@ fn write_arg_markdown(buffer: &mut String, arg: &clap::Arg) -> fmt::Result {
474496
}
475497

476498
if let Some(aliases) = arg.get_visible_aliases().as_deref() {
477-
if let Some(aliases) = get_alias_str(aliases) {
478-
write!(buffer, " {aliases}")?;
499+
if let Some(aliases_str) = get_alias_string(aliases) {
500+
write!(
501+
buffer,
502+
" [{}: {aliases_str}]",
503+
pluralize(aliases.len(), "alias", "aliases")
504+
)?;
479505
}
480506
}
481507

@@ -605,30 +631,16 @@ fn indent(s: &str, first: &str, rest: &str) -> String {
605631
result
606632
}
607633

608-
fn wrap_with(s: &str, wrapper: &str) -> String {
609-
format!("{wrapper}{s}{wrapper}")
610-
}
611-
612-
fn wrap_with_backticks(s: &str) -> String {
613-
wrap_with(s, "`")
614-
}
615-
616-
fn get_alias_str(aliases: &[&str]) -> Option<String> {
634+
fn get_alias_string(aliases: &[&str]) -> Option<String> {
617635
if aliases.is_empty() {
618636
return None;
619637
}
620638

621-
let prefix = if aliases.len() == 1 {
622-
"alias"
623-
} else {
624-
"aliases"
625-
};
626-
627639
Some(format!(
628-
"[{prefix}: {}]",
640+
"{}",
629641
aliases
630642
.iter()
631-
.map(|s| wrap_with_backticks(s))
643+
.map(|alias| format!("`{alias}`"))
632644
.collect::<Vec<_>>()
633645
.join(", ")
634646
))

src/utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub fn pluralize<T>(len: usize, singular: T, plural: T) -> T {
2+
match len {
3+
1 => singular,
4+
_ => plural,
5+
}
6+
}

tests/test_examples.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn test_example_complex_app() {
2020
.title("Some Custom Title for Complex App".to_string())
2121
.show_footer(false)
2222
.show_table_of_contents(false)
23+
.show_aliases(false)
2324
),
2425
include_str!("../docs/examples/complex-app-custom.md"),
2526
"Mismatch testing CUSTOM Markdown output"

0 commit comments

Comments
 (0)