@@ -12,22 +12,26 @@ use validates::ValidationResult;
1212
1313pub struct Registry < R > {
1414 map : HashMap < & ' static str , ( usize , Box < Fn ( & [ & str ] ) -> ValidationResult < R > + Send + Sync > ) > ,
15+ aliaseses : Vec < Vec < & ' static str > > ,
1516}
1617
1718impl < R > Default for Registry < R > {
1819 fn default ( ) -> Self {
1920 return Registry {
2021 map : HashMap :: new ( ) ,
22+ aliaseses : Vec :: new ( ) ,
2123 } ;
2224 }
2325}
2426
2527impl < R : ' static > Registry < R > {
2628 pub fn add < I : Registrant < R > + ' static > ( & mut self ) {
27- for name in I :: names ( ) {
29+ let names = I :: names ( ) ;
30+ for name in names. iter ( ) {
2831 let prev = self . map . insert ( name, ( I :: argct ( ) , Box :: new ( I :: init) ) ) ;
2932 assert ! ( prev. is_none( ) , "registry collision for {}" , name) ;
3033 }
34+ self . aliaseses . push ( names) ;
3135 }
3236
3337 pub fn find ( & self , name : & str , args : & [ & str ] ) -> ValidationResult < R > {
@@ -103,6 +107,22 @@ impl<R: 'static> Registry<R> {
103107 } , help) ;
104108 return opt;
105109 }
110+
111+ pub fn help_options < X : ' static > ( & ' static self , type_name : & str ) -> OptionsPile < X > {
112+ let mut opt = OptionsPile :: < X > :: new ( ) ;
113+ let aliaseses = & self . aliaseses ;
114+ opt. match_zero ( & [ & format ! ( "list-{}" , type_name) ] , move |_p| {
115+ return ValidationError :: help ( aliaseses. iter ( ) . map ( |aliases| {
116+ let ( first, rest) = aliases. split_first ( ) . unwrap ( ) ;
117+ let mut line = first. to_string ( ) ;
118+ if !rest. is_empty ( ) {
119+ line. push_str ( & format ! ( " [{}]" , rest. join( ", " ) ) ) ;
120+ }
121+ return line;
122+ } ) . collect ( ) ) ;
123+ } , format ! ( "list {}s" , type_name) ) ;
124+ return opt;
125+ }
106126}
107127
108128#[ macro_export]
0 commit comments