From e39156e0b9b33820b97d6e6357d306e7fbd25c5a Mon Sep 17 00:00:00 2001 From: Emerson Ford Date: Tue, 12 Jul 2022 11:23:22 -0700 Subject: [PATCH] feat: Add method to get non-visible arg aliases --- src/builder/arg.rs | 20 ++++++++++++++++++++ tests/builder/arg_aliases.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/builder/arg.rs b/src/builder/arg.rs index 63e88e80..e9403d0b 100644 --- a/src/builder/arg.rs +++ b/src/builder/arg.rs @@ -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> { + 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> { @@ -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> { + 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> { diff --git a/tests/builder/arg_aliases.rs b/tests/builder/arg_aliases.rs index 7b216dbc..3cc8dbf5 100644 --- a/tests/builder/arg_aliases.rs +++ b/tests/builder/arg_aliases.rs @@ -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")