1
+ use anstyle:: Style ;
2
+
1
3
use crate :: builder:: IntoResettable ;
2
4
use crate :: builder:: Str ;
3
5
use crate :: builder:: StyledStr ;
@@ -37,6 +39,7 @@ pub struct PossibleValue {
37
39
name : Str ,
38
40
help : Option < StyledStr > ,
39
41
aliases : Vec < Str > , // (name, visible)
42
+ visible_aliases : Vec < Str > ,
40
43
hide : bool ,
41
44
}
42
45
@@ -130,6 +133,20 @@ impl PossibleValue {
130
133
self
131
134
}
132
135
136
+ /// Add a _visible_ alias to this [PossibleValue].
137
+ ///
138
+ /// Unlike [PossibleValue::alias], these aliases will show in help output.
139
+ #[ must_use]
140
+ pub fn visible_alias ( mut self , name : impl IntoResettable < Str > ) -> Self {
141
+ if let Some ( name) = name. into_resettable ( ) . into_option ( ) {
142
+ self . visible_aliases . push ( name) ;
143
+ } else {
144
+ self . visible_aliases . clear ( ) ;
145
+ }
146
+
147
+ self
148
+ }
149
+
133
150
/// Sets multiple *hidden* aliases for this argument value.
134
151
///
135
152
/// # Examples
@@ -146,6 +163,16 @@ impl PossibleValue {
146
163
self . aliases . extend ( names. into_iter ( ) . map ( |a| a. into ( ) ) ) ;
147
164
self
148
165
}
166
+
167
+ /// Add multiple _visible_ aliases to this [PossibleValue].
168
+ ///
169
+ /// Unlike [PossibleValue::aliases], these aliases will show in help output.
170
+ #[ must_use]
171
+ pub fn visible_aliases ( mut self , names : impl IntoIterator < Item = impl Into < Str > > ) -> Self {
172
+ self . visible_aliases
173
+ . extend ( names. into_iter ( ) . map ( |a| a. into ( ) ) ) ;
174
+ self
175
+ }
149
176
}
150
177
151
178
/// Reflection
@@ -156,6 +183,42 @@ impl PossibleValue {
156
183
self . name . as_str ( )
157
184
}
158
185
186
+ /// Get the visible aliases for this argument
187
+ pub fn get_visible_aliases ( & self ) -> & Vec < Str > {
188
+ & self . visible_aliases
189
+ }
190
+
191
+ /// Render help text for this [PossibleValue] with all of its visible aliases included.
192
+ ///
193
+ /// If there are no visible aliases, this will simply emit the formatted name, if there are visible aliases, these
194
+ /// will be appended like this: `name [aliases: a, b, c]`.
195
+ pub fn render_help_prefix_long ( & self , literal : & Style ) -> String {
196
+ const ALIASES_PREFIX : & str = " [aliases: " ;
197
+ const ALIASES_POSTFIX : & str = "]" ;
198
+ const ALIASES_JOINER : & str = ", " ;
199
+
200
+ let name = self . get_name ( ) ;
201
+ let aliases = if self . get_visible_aliases ( ) . is_empty ( ) {
202
+ "" . to_string ( )
203
+ } else {
204
+ // [aliases: a, b, c]
205
+ format ! (
206
+ "{ALIASES_PREFIX}{}{ALIASES_POSTFIX}" ,
207
+ self . get_visible_aliases( )
208
+ . iter( )
209
+ . map( |s| { format!( "{}{s}{}" , literal. render( ) , literal. render_reset( ) ) } )
210
+ . collect:: <Vec <_>>( )
211
+ . join( ALIASES_JOINER )
212
+ )
213
+ } ;
214
+
215
+ format ! (
216
+ "{}{name}{}{aliases}" ,
217
+ literal. render( ) ,
218
+ literal. render_reset( )
219
+ )
220
+ }
221
+
159
222
/// Get the help specified for this argument, if any
160
223
#[ inline]
161
224
pub fn get_help ( & self ) -> Option < & StyledStr > {
@@ -192,7 +255,9 @@ impl PossibleValue {
192
255
///
193
256
/// Namely the name and all aliases.
194
257
pub fn get_name_and_aliases ( & self ) -> impl Iterator < Item = & str > + ' _ {
195
- std:: iter:: once ( self . get_name ( ) ) . chain ( self . aliases . iter ( ) . map ( |s| s. as_str ( ) ) )
258
+ std:: iter:: once ( self . get_name ( ) )
259
+ . chain ( self . aliases . iter ( ) . map ( |s| s. as_str ( ) ) )
260
+ . chain ( self . visible_aliases . iter ( ) . map ( |s| s. as_str ( ) ) )
196
261
}
197
262
198
263
/// Tests if the value is valid for this argument value
0 commit comments