|
1 | 1 | use std::iter::IntoIterator;
|
2 | 2 | use std::collections::HashSet;
|
| 3 | +#[cfg(feature = "yaml")] |
| 4 | +use std::collections::BTreeMap; |
3 | 5 | use std::rc::Rc;
|
4 | 6 |
|
| 7 | +#[cfg(feature = "yaml")] |
| 8 | +use yaml_rust::Yaml; |
| 9 | + |
5 | 10 | use usageparser::{UsageParser, UsageToken};
|
6 | 11 |
|
7 | 12 | /// The abstract representation of a command line argument used by the consumer of the library.
|
@@ -143,6 +148,87 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
|
143 | 148 | }
|
144 | 149 | }
|
145 | 150 |
|
| 151 | + /// Creates a new instace of `App` from a .yml (YAML) file. |
| 152 | + /// |
| 153 | + /// # Example |
| 154 | + /// |
| 155 | + /// ```ignore |
| 156 | + /// # use clap::App; |
| 157 | + /// let yml = load_yaml!("app.yml"); |
| 158 | + /// let app = App::from_yaml(yml); |
| 159 | + /// ``` |
| 160 | + #[cfg(feature = "yaml")] |
| 161 | + pub fn from_yaml<'y>(y: &'y BTreeMap<Yaml, Yaml>) -> Arg<'y, 'y, 'y, 'y, 'y, 'y> { |
| 162 | + debugln!("arg_yaml={:#?}", y); |
| 163 | + // We WANT this to panic on error...so expect() is good. |
| 164 | + let name_yml = y.keys().nth(0).unwrap(); |
| 165 | + let name_str = name_yml.as_str().unwrap(); |
| 166 | + let mut a = Arg::with_name(name_str); |
| 167 | + let arg_settings = y.get(name_yml).unwrap().as_hash().unwrap(); |
| 168 | + |
| 169 | + for (k, v) in arg_settings.iter() { |
| 170 | + a = match k.as_str().unwrap() { |
| 171 | + "short" => a.short(v.as_str().unwrap()), |
| 172 | + "long" => a.long(v.as_str().unwrap()), |
| 173 | + "help" => a.help(v.as_str().unwrap()), |
| 174 | + "required" => a.required(v.as_bool().unwrap()), |
| 175 | + "takes_value" => a.takes_value(v.as_bool().unwrap()), |
| 176 | + "index" => a.index(v.as_i64().unwrap() as u8), |
| 177 | + "global" => a.global(v.as_bool().unwrap()), |
| 178 | + "multiple" => a.multiple(v.as_bool().unwrap()), |
| 179 | + "empty_values" => a.empty_values(v.as_bool().unwrap()), |
| 180 | + "group" => a.group(v.as_str().unwrap()), |
| 181 | + "number_of_values" => a.number_of_values(v.as_i64().unwrap() as u8), |
| 182 | + "max_values" => a.max_values(v.as_i64().unwrap() as u8), |
| 183 | + "min_values" => a.min_values(v.as_i64().unwrap() as u8), |
| 184 | + "value_name" => a.value_name(v.as_str().unwrap()), |
| 185 | + "value_names" => { |
| 186 | + for ys in v.as_vec().unwrap() { |
| 187 | + if let Some(s) = ys.as_str() { |
| 188 | + a = a.value_name(s); |
| 189 | + } |
| 190 | + } |
| 191 | + a |
| 192 | + }, |
| 193 | + "requires" => { |
| 194 | + for ys in v.as_vec().unwrap() { |
| 195 | + if let Some(s) = ys.as_str() { |
| 196 | + a = a.requires(s); |
| 197 | + } |
| 198 | + } |
| 199 | + a |
| 200 | + }, |
| 201 | + "conflicts_with" => { |
| 202 | + for ys in v.as_vec().unwrap() { |
| 203 | + if let Some(s) = ys.as_str() { |
| 204 | + a = a.conflicts_with(s); |
| 205 | + } |
| 206 | + } |
| 207 | + a |
| 208 | + }, |
| 209 | + "mutually_overrides_with" => { |
| 210 | + for ys in v.as_vec().unwrap() { |
| 211 | + if let Some(s) = ys.as_str() { |
| 212 | + a = a.mutually_overrides_with(s); |
| 213 | + } |
| 214 | + } |
| 215 | + a |
| 216 | + }, |
| 217 | + "possible_values" => { |
| 218 | + for ys in v.as_vec().unwrap() { |
| 219 | + if let Some(s) = ys.as_str() { |
| 220 | + a = a.possible_value(s); |
| 221 | + } |
| 222 | + } |
| 223 | + a |
| 224 | + }, |
| 225 | + s => panic!("Unknown Arg setting '{}' in YAML file for arg '{}'", s, name_str) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + a |
| 230 | + } |
| 231 | + |
146 | 232 | /// Creates a new instace of `Arg` from a usage string. Allows creation of basic settings
|
147 | 233 | /// for Arg (i.e. everything except relational rules). The syntax is flexible, but there are
|
148 | 234 | /// some rules to follow.
|
@@ -675,6 +761,31 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
|
675 | 761 | self
|
676 | 762 | }
|
677 | 763 |
|
| 764 | + /// Specifies a possible value for this argument. At runtime, clap verifies that only |
| 765 | + /// one of the specified values was used, or fails with a usage string. |
| 766 | + /// |
| 767 | + /// **NOTE:** This setting only applies to options and positional arguments |
| 768 | + /// |
| 769 | + /// |
| 770 | + /// # Example |
| 771 | + /// |
| 772 | + /// ```no_run |
| 773 | + /// # use clap::{App, Arg}; |
| 774 | + /// # let matches = App::new("myprog") |
| 775 | + /// # .arg( |
| 776 | + /// # Arg::with_name("debug").index(1) |
| 777 | + /// .possible_value("fast") |
| 778 | + /// .possible_value("slow") |
| 779 | + /// # ).get_matches(); |
| 780 | + pub fn possible_value(mut self, name: &'p str) -> Self { |
| 781 | + if let Some(ref mut vec) = self.possible_vals { |
| 782 | + vec.push(name); |
| 783 | + } else { |
| 784 | + self.possible_vals = Some(vec![name]); |
| 785 | + } |
| 786 | + self |
| 787 | + } |
| 788 | + |
678 | 789 | /// Specifies the name of the group the argument belongs to.
|
679 | 790 | ///
|
680 | 791 | ///
|
|
0 commit comments