Skip to content

Commit

Permalink
feat: add support for argument aliases in markdown output
Browse files Browse the repository at this point in the history
Currently it adds the alias after the current options before the help doc. Open to other suggestions.
  • Loading branch information
willemneal authored and gitbutler-client committed Dec 2, 2024
1 parent 1e1ae46 commit 5c8eaf6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/examples/complex-app-custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ An example command-line tool

###### **Options:**

* `-c`, `--config <FILE>` — Sets a custom config file
* `-c`, `--config <FILE>` Aliases: `configuration` — Sets a custom config file
* `--target <TARGET>`

Default value: `local`
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/complex-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ An example command-line tool

###### **Options:**

* `-c`, `--config <FILE>` — Sets a custom config file
* `-c`, `--config <FILE>` Aliases: `configuration` — Sets a custom config file
* `--target <TARGET>`

Default value: `local`
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/complex_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Cli {
name: Option<String>,

/// Sets a custom config file
#[arg(short, long, value_name = "FILE")]
#[arg(short, long, value_name = "FILE", visible_alias = "configuration")]
config: Option<PathBuf>,

#[arg(long, default_value = "local")]
Expand Down
16 changes: 14 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@ fn write_arg_markdown(buffer: &mut String, arg: &clap::Arg) -> fmt::Result {
},
(None, Some(long)) => {
if arg.get_action().takes_values() {
write!(buffer, "`--{} <{value_name}>`", long)?
write!(buffer, "`--{long} <{value_name}>`")?
} else {
write!(buffer, "`--{}`", long)?
write!(buffer, "`--{long}`")?
}
},
(None, None) => {
Expand All @@ -471,6 +471,18 @@ fn write_arg_markdown(buffer: &mut String, arg: &clap::Arg) -> fmt::Result {
},
}

if let Some(aliases) = arg.get_visible_aliases() {
if !aliases.is_empty() {
write!(buffer, " Aliases: ")?;
for (i, alias) in aliases.into_iter().enumerate() {
if i > 0 {
write!(buffer, ", ")?;
}
write!(buffer, "`{alias}`")?;
}
}
}

if let Some(help) = arg.get_long_help() {
// TODO: Parse formatting in the string
buffer.push_str(&indent(&help.to_string(), " — ", " "))
Expand Down

0 comments on commit 5c8eaf6

Please sign in to comment.