Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add method to get non-visible arg aliases #3913

Merged
merged 1 commit into from Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/builder/arg.rs
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
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