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,38 @@ 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
+ let name = self . get_name ( ) ;
197
+ let aliases = if self . get_visible_aliases ( ) . is_empty ( ) {
198
+ "" . to_string ( )
199
+ } else {
200
+ // [aliases: a, b, c]
201
+ format ! (
202
+ "[aliases: {}]" ,
203
+ self . get_visible_aliases( )
204
+ . iter( )
205
+ . map( |s| { format!( "{}{s}{}" , literal. render( ) , literal. render_reset( ) ) } )
206
+ . collect:: <Vec <_>>( )
207
+ . join( ", " )
208
+ )
209
+ } ;
210
+
211
+ format ! (
212
+ "{}{name}{}{aliases}" ,
213
+ literal. render( ) ,
214
+ literal. render_reset( )
215
+ )
216
+ }
217
+
159
218
/// Get the help specified for this argument, if any
160
219
#[ inline]
161
220
pub fn get_help ( & self ) -> Option < & StyledStr > {
@@ -173,26 +232,13 @@ impl PossibleValue {
173
232
!self . hide && self . help . is_some ( )
174
233
}
175
234
176
- /// Get the name if argument value is not hidden, `None` otherwise,
177
- /// but wrapped in quotes if it contains whitespace
178
- #[ cfg( feature = "help" ) ]
179
- pub ( crate ) fn get_visible_quoted_name ( & self ) -> Option < std:: borrow:: Cow < ' _ , str > > {
180
- if !self . hide {
181
- Some ( if self . name . contains ( char:: is_whitespace) {
182
- format ! ( "{:?}" , self . name) . into ( )
183
- } else {
184
- self . name . as_str ( ) . into ( )
185
- } )
186
- } else {
187
- None
188
- }
189
- }
190
-
191
235
/// Returns all valid values of the argument value.
192
236
///
193
237
/// Namely the name and all aliases.
194
238
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 ( ) ) )
239
+ std:: iter:: once ( self . get_name ( ) )
240
+ . chain ( self . aliases . iter ( ) . map ( |s| s. as_str ( ) ) )
241
+ . chain ( self . visible_aliases . iter ( ) . map ( |s| s. as_str ( ) ) )
196
242
}
197
243
198
244
/// Tests if the value is valid for this argument value
0 commit comments