@@ -98,74 +98,8 @@ impl<'a, 'b> App<'a, 'b> {
98
98
/// // continued logic goes here, such as `app.get_matches()` etc.
99
99
/// ```
100
100
#[ cfg( feature = "yaml" ) ]
101
- pub fn from_yaml < ' y > ( mut yaml : & ' y Yaml ) -> App < ' y , ' y > {
102
- use args:: SubCommand ;
103
- // We WANT this to panic on error...so expect() is good.
104
- let mut is_sc = None ;
105
- let mut a = if let Some ( name) = yaml[ "name" ] . as_str ( ) {
106
- App :: new ( name)
107
- } else {
108
- let yaml_hash = yaml. as_hash ( ) . unwrap ( ) ;
109
- let sc_key = yaml_hash. keys ( ) . nth ( 0 ) . unwrap ( ) ;
110
- is_sc = Some ( yaml_hash. get ( sc_key) . unwrap ( ) ) ;
111
- App :: new ( sc_key. as_str ( ) . unwrap ( ) )
112
- } ;
113
- yaml = if let Some ( sc) = is_sc {
114
- sc
115
- } else {
116
- yaml
117
- } ;
118
- if let Some ( v) = yaml[ "version" ] . as_str ( ) {
119
- a = a. version ( v) ;
120
- }
121
- if let Some ( v) = yaml[ "author" ] . as_str ( ) {
122
- a = a. author ( v) ;
123
- }
124
- if let Some ( v) = yaml[ "bin_name" ] . as_str ( ) {
125
- a = a. bin_name ( v) ;
126
- }
127
- if let Some ( v) = yaml[ "about" ] . as_str ( ) {
128
- a = a. about ( v) ;
129
- }
130
- if let Some ( v) = yaml[ "after_help" ] . as_str ( ) {
131
- a = a. after_help ( v) ;
132
- }
133
- if let Some ( v) = yaml[ "usage" ] . as_str ( ) {
134
- a = a. usage ( v) ;
135
- }
136
- if let Some ( v) = yaml[ "help" ] . as_str ( ) {
137
- a = a. help ( v) ;
138
- }
139
- if let Some ( v) = yaml[ "help_short" ] . as_str ( ) {
140
- a = a. help_short ( v) ;
141
- }
142
- if let Some ( v) = yaml[ "version_short" ] . as_str ( ) {
143
- a = a. version_short ( v) ;
144
- }
145
- if let Some ( v) = yaml[ "settings" ] . as_vec ( ) {
146
- for ys in v {
147
- if let Some ( s) = ys. as_str ( ) {
148
- a = a. setting ( s. parse ( ) . ok ( ) . expect ( "unknown AppSetting found in YAML file" ) ) ;
149
- }
150
- }
151
- }
152
- if let Some ( v) = yaml[ "args" ] . as_vec ( ) {
153
- for arg_yaml in v {
154
- a = a. arg ( Arg :: from_yaml ( & arg_yaml. as_hash ( ) . unwrap ( ) ) ) ;
155
- }
156
- }
157
- if let Some ( v) = yaml[ "subcommands" ] . as_vec ( ) {
158
- for sc_yaml in v {
159
- a = a. subcommand ( SubCommand :: from_yaml ( & sc_yaml) ) ;
160
- }
161
- }
162
- if let Some ( v) = yaml[ "groups" ] . as_vec ( ) {
163
- for ag_yaml in v {
164
- a = a. group ( ArgGroup :: from_yaml ( & ag_yaml. as_hash ( ) . unwrap ( ) ) ) ;
165
- }
166
- }
167
-
168
- a
101
+ pub fn from_yaml ( yaml : & ' a Yaml ) -> App < ' a , ' a > {
102
+ App :: from ( yaml)
169
103
}
170
104
171
105
/// Sets a string of author(s) that will be displayed to the user when they request the help
@@ -810,3 +744,76 @@ impl<'a, 'b> App<'a, 'b> {
810
744
e. exit ( )
811
745
}
812
746
}
747
+
748
+ #[ cfg( feature = "yaml" ) ]
749
+ impl < ' a > From < & ' a Yaml > for App < ' a , ' a > {
750
+ fn from ( mut yaml : & ' a Yaml ) -> Self {
751
+ use args:: SubCommand ;
752
+ // We WANT this to panic on error...so expect() is good.
753
+ let mut is_sc = None ;
754
+ let mut a = if let Some ( name) = yaml[ "name" ] . as_str ( ) {
755
+ App :: new ( name)
756
+ } else {
757
+ let yaml_hash = yaml. as_hash ( ) . unwrap ( ) ;
758
+ let sc_key = yaml_hash. keys ( ) . nth ( 0 ) . unwrap ( ) ;
759
+ is_sc = Some ( yaml_hash. get ( sc_key) . unwrap ( ) ) ;
760
+ App :: new ( sc_key. as_str ( ) . unwrap ( ) )
761
+ } ;
762
+ yaml = if let Some ( sc) = is_sc {
763
+ sc
764
+ } else {
765
+ yaml
766
+ } ;
767
+ if let Some ( v) = yaml[ "version" ] . as_str ( ) {
768
+ a = a. version ( v) ;
769
+ }
770
+ if let Some ( v) = yaml[ "author" ] . as_str ( ) {
771
+ a = a. author ( v) ;
772
+ }
773
+ if let Some ( v) = yaml[ "bin_name" ] . as_str ( ) {
774
+ a = a. bin_name ( v) ;
775
+ }
776
+ if let Some ( v) = yaml[ "about" ] . as_str ( ) {
777
+ a = a. about ( v) ;
778
+ }
779
+ if let Some ( v) = yaml[ "after_help" ] . as_str ( ) {
780
+ a = a. after_help ( v) ;
781
+ }
782
+ if let Some ( v) = yaml[ "usage" ] . as_str ( ) {
783
+ a = a. usage ( v) ;
784
+ }
785
+ if let Some ( v) = yaml[ "help" ] . as_str ( ) {
786
+ a = a. help ( v) ;
787
+ }
788
+ if let Some ( v) = yaml[ "help_short" ] . as_str ( ) {
789
+ a = a. help_short ( v) ;
790
+ }
791
+ if let Some ( v) = yaml[ "version_short" ] . as_str ( ) {
792
+ a = a. version_short ( v) ;
793
+ }
794
+ if let Some ( v) = yaml[ "settings" ] . as_vec ( ) {
795
+ for ys in v {
796
+ if let Some ( s) = ys. as_str ( ) {
797
+ a = a. setting ( s. parse ( ) . ok ( ) . expect ( "unknown AppSetting found in YAML file" ) ) ;
798
+ }
799
+ }
800
+ }
801
+ if let Some ( v) = yaml[ "args" ] . as_vec ( ) {
802
+ for arg_yaml in v {
803
+ a = a. arg ( Arg :: from_yaml ( & arg_yaml. as_hash ( ) . unwrap ( ) ) ) ;
804
+ }
805
+ }
806
+ if let Some ( v) = yaml[ "subcommands" ] . as_vec ( ) {
807
+ for sc_yaml in v {
808
+ a = a. subcommand ( SubCommand :: from_yaml ( & sc_yaml) ) ;
809
+ }
810
+ }
811
+ if let Some ( v) = yaml[ "groups" ] . as_vec ( ) {
812
+ for ag_yaml in v {
813
+ a = a. group ( ArgGroup :: from ( ag_yaml. as_hash ( ) . unwrap ( ) ) ) ;
814
+ }
815
+ }
816
+
817
+ a
818
+ }
819
+ }
0 commit comments