Skip to content

Commit 96d8729

Browse files
committed
fix(usage_md): detect and reject visible aliases that duplicate their own flag name
A visible_alias matching the argument's own --long name (or a visible_short_alias matching its -short flag) is a coding mistake that produces redundant output in USAGE.md. The new validation runs at the start of render_usage_md() and exits with code 1 if any such redundancy is found. https://claude.ai/code/session_01TSnvxN1HBwwzhxfFShWAQ5
1 parent 50ba05e commit 96d8729

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/usage_md.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::borrow::Cow;
77
/// Renders a Markdown reference page for `pdu`'s CLI.
88
pub fn render_usage_md() -> String {
99
let mut command: Command = Args::command();
10+
validate_no_redundant_visible_aliases(&command);
1011
let mut out = String::new();
1112

1213
let usage = command.render_usage().to_string();
@@ -232,3 +233,41 @@ fn ensure_ends_with_punctuation(line: &str) -> Cow<'_, str> {
232233
Cow::Owned(format!("{line}."))
233234
}
234235
}
236+
237+
/// Checks that no argument has a visible alias that duplicates its own primary flag name.
238+
///
239+
/// A `visible_alias` matching the argument's own `--long` name, or a `visible_short_alias`
240+
/// matching its own `-short` flag, is a coding mistake that produces redundant output in
241+
/// USAGE.md. This function detects such mistakes and terminates the process.
242+
fn validate_no_redundant_visible_aliases(command: &Command) {
243+
let mut errors = Vec::<String>::new();
244+
245+
for arg in command.get_arguments() {
246+
if let Some(primary_long) = arg.get_long() {
247+
for alias in arg.get_visible_aliases().unwrap_or_default() {
248+
if alias == primary_long {
249+
errors.push(format!(
250+
"--{primary_long} has visible_alias \"{alias}\" that duplicates its own flag name",
251+
));
252+
}
253+
}
254+
}
255+
256+
if let Some(primary_short) = arg.get_short() {
257+
for alias in arg.get_visible_short_aliases().unwrap_or_default() {
258+
if alias == primary_short {
259+
errors.push(format!(
260+
"-{primary_short} has visible_short_alias '{alias}' that duplicates its own flag name",
261+
));
262+
}
263+
}
264+
}
265+
}
266+
267+
if !errors.is_empty() {
268+
for error in &errors {
269+
eprintln!("error: {error}");
270+
}
271+
std::process::exit(1);
272+
}
273+
}

0 commit comments

Comments
 (0)