Skip to content

Commit

Permalink
Merge pull request #3913 from emersonford/expose-non-visible-arg-aliases
Browse files Browse the repository at this point in the history
feat: Add method to get non-visible arg aliases
  • Loading branch information
epage authored Jul 13, 2022
2 parents 9b6321a + e39156e commit e8374e3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4316,6 +4316,16 @@ impl<'help> Arg<'help> {
}
}

/// Get *all* short aliases for this argument, if any, both visible and hidden.
#[inline]
pub fn get_all_short_aliases(&self) -> Option<Vec<char>> {
if self.short_aliases.is_empty() {
None
} else {
Some(self.short_aliases.iter().map(|(s, _)| s).copied().collect())
}
}

/// Get the short option name and its visible aliases, if any
#[inline]
pub fn get_short_and_visible_aliases(&self) -> Option<Vec<char>> {
Expand Down Expand Up @@ -4351,6 +4361,16 @@ impl<'help> Arg<'help> {
}
}

/// Get *all* aliases for this argument, if any, both visible and hidden.
#[inline]
pub fn get_all_aliases(&self) -> Option<Vec<&'help str>> {
if self.aliases.is_empty() {
None
} else {
Some(self.aliases.iter().map(|(s, _)| s).copied().collect())
}
}

/// Get the long option name and its visible aliases, if any
#[inline]
pub fn get_long_and_visible_aliases(&self) -> Option<Vec<&'help str>> {
Expand Down
31 changes: 31 additions & 0 deletions tests/builder/arg_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ fn multiple_aliases_of_option() {
);
}

#[test]
fn get_aliases() {
let a = Arg::new("aliases")
.long("aliases")
.takes_value(true)
.help("multiple aliases")
.aliases(&["alias1", "alias2", "alias3"])
.short_aliases(&['a', 'b', 'c'])
.visible_aliases(&["alias4", "alias5", "alias6"])
.visible_short_aliases(&['d', 'e', 'f']);

assert!(a.get_short_and_visible_aliases().is_none());
assert_eq!(
a.get_long_and_visible_aliases().unwrap(),
&["aliases", "alias4", "alias5", "alias6"]
);
assert_eq!(
a.get_visible_aliases().unwrap(),
&["alias4", "alias5", "alias6"]
);
assert_eq!(
a.get_all_aliases().unwrap(),
&["alias1", "alias2", "alias3", "alias4", "alias5", "alias6"]
);
assert_eq!(a.get_visible_short_aliases().unwrap(), vec!['d', 'e', 'f']);
assert_eq!(
a.get_all_short_aliases().unwrap(),
vec!['a', 'b', 'c', 'd', 'e', 'f']
);
}

#[test]
fn single_alias_of_flag() {
let a = Command::new("test")
Expand Down

0 comments on commit e8374e3

Please sign in to comment.