      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334

    #[cfg(feature = "yaml")]
    use std::collections::BTreeMap;
    use std::fmt::{Debug, Formatter, Result};

    #[cfg(feature = "yaml")]
    use yaml_rust::Yaml;

    /// `ArgGroup`s are a family of related arguments and way for you to say, "Any of these arguments".
    /// By placing arguments in a logical group, you can make easier requirement and exclusion rules
    /// instead of having to list each individually, or when you want a rule to apply "any but not all"
    /// arguments.
    ///
    /// For instance, you can make an entire ArgGroup required, this means that one (and *only* one)
    /// argument. from that group must be present. Using more than one argument from an ArgGroup causes
    /// a failure (graceful exit).
    ///
    /// You can also do things such as name an ArgGroup as a confliction or requirement, meaning any
    /// of the arguments that belong to that group will cause a failure if present, or must present
    /// respectively.
    ///
    /// Perhaps the most common use of `ArgGroup`s is to require one and *only* one argument to be
    /// present out of a given set. Imagine that you had multiple arguments, and you want one of them
    /// to be required, but making all of them required isn't feasible because perhaps they conflict
    /// with each other. For example, lets say that you were building an application where one could
    /// set a given version number by supplying a string with an option argument, i.e.
    /// `--set-ver v1.2.3`, you also wanted to support automatically using a previous version number
    /// and simply incrementing one of the three numbers. So you create three flags `--major`,
    /// `--minor`, and `--patch`. All of these arguments shouldn't be used at one time but you want to
    /// specify that *at least one* of them is used. For this, you can create a group.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use clap::{App, ArgGroup};
    /// let _ = App::new("app")
    /// .args_from_usage("--set-ver [ver] 'set the version manually'
    ///                   --major         'auto increase major'
    ///                   --minor         'auto increase minor'
    ///                   --patch         'auto increase patch")
    /// .arg_group(ArgGroup::with_name("vers")
    ///                     .add_all(&["ver", "major", "minor","patch"])
    ///                     .required(true))
    /// # .get_matches();
    pub struct ArgGroup<'n, 'ar> {
        #[doc(hidden)]
        pub name: &'n str,
        #[doc(hidden)]
        pub args: Vec<&'ar str>,
        #[doc(hidden)]
        pub required: bool,
        #[doc(hidden)]
        pub requires: Option<Vec<&'ar str>>,
        #[doc(hidden)]
        pub conflicts: Option<Vec<&'ar str>>,
    }

    impl<'n, 'ar> ArgGroup<'n, 'ar> {
        /// Creates a new instance of `ArgGroup` using a unique string name.
        /// The name will only be used by the library consumer and not displayed to the use.
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// ArgGroup::with_name("config")
        /// # ).get_matches();
        pub fn with_name(n: &'n str) -> Self {
            ArgGroup {
                name: n,
                required: false,
                args: vec![],
                requires: None,
                conflicts: None,
            }
        }

        /// Creates a new instance of `ArgGroup` from a .yml (YAML) file.
        ///
        /// # Examples
        ///
        /// ```ignore
        /// # use clap::ArgGroup;
        /// let yml = load_yaml!("group.yml");
        /// let ag = ArgGroup::from_yaml(yml);
        /// ```
        #[cfg(feature = "yaml")]
        pub fn from_yaml<'y>(y: &'y BTreeMap<Yaml, Yaml>) -> ArgGroup<'y, 'y> {
            // We WANT this to panic on error...so expect() is good.
            let name_yml = y.keys().nth(0).unwrap();
            let name_str = name_yml.as_str().unwrap();
            let mut a = ArgGroup::with_name(name_str);
            let group_settings = y.get(name_yml).unwrap().as_hash().unwrap();

            for (k, v) in group_settings.iter() {
                a = match k.as_str().unwrap() {
                    "required" => a.required(v.as_bool().unwrap()),
                    "args" => {
                        for ys in v.as_vec().unwrap() {
                            if let Some(s) = ys.as_str() {
                                a = a.add(s);
                            }
                        }
                        a
                    }
                    "requires" => {
                        for ys in v.as_vec().unwrap() {
                            if let Some(s) = ys.as_str() {
                                a = a.requires(s);
                            }
                        }
                        a
                    }
                    "conflicts_with" => {
                        for ys in v.as_vec().unwrap() {
                            if let Some(s) = ys.as_str() {
                                a = a.conflicts_with(s);
                            }
                        }
                        a
                    }
                    s => panic!("Unknown ArgGroup setting '{}' in YAML file for \
                                 ArgGroup '{}'",
                                s,
                                name_str),
                }
            }

            a
        }

        /// Adds an argument to this group by name
        ///
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// # ArgGroup::with_name("config")
        /// .add("config")
        /// # ).get_matches();
        pub fn add(mut self, n: &'ar str) -> Self {
            assert!(self.name != n,
                    "ArgGroup '{}' can not have same name as arg inside it",
                    self.name);
            self.args.push(n);
            self
        }

        /// Adds multiple arguments to this group by name
        ///
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// # ArgGroup::with_name("config")
        /// .add_all(&["config", "input", "output"])
        /// # ).get_matches();
        pub fn add_all(mut self, ns: &[&'ar str]) -> Self {
            for n in ns {
                self = self.add(n);
            }
            self
        }

        /// Sets the requirement of this group. A required group will be displayed in the usage string
        /// of the application in the format `[arg|arg2|arg3]`. A required `ArgGroup` simply states
        /// that one, and only one argument from this group *must* be present at runtime (unless
        /// conflicting with another argument).
        ///
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// # ArgGroup::with_name("config")
        /// .required(true)
        /// # ).get_matches();
        pub fn required(mut self, r: bool) -> Self {
            self.required = r;
            self
        }

        /// Sets the requirement rules of this group. This is not to be confused with a required group.
        /// Requirement rules function just like argument requirement rules, you can name other
        /// arguments or groups that must be present when one of the arguments from this group is used.
        ///
        /// **NOTE:** The name provided may be an argument, or group name
        ///
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// # ArgGroup::with_name("config")
        /// .requires("config")
        /// # ).get_matches();
        pub fn requires(mut self, n: &'ar str) -> Self {
            if let Some(ref mut reqs) = self.requires {
                reqs.push(n);
            } else {
                self.requires = Some(vec![n]);
            }
            self
        }

        /// Sets the requirement rules of this group. This is not to be confused with a required group.
        /// Requirement rules function just like argument requirement rules, you can name other
        /// arguments or groups that must be present when one of the arguments from this group is used.
        ///
        /// **NOTE:** The names provided may be an argument, or group name
        ///
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// # ArgGroup::with_name("config")
        /// .requires_all(&["config", "input"])
        /// # ).get_matches();
        pub fn requires_all(mut self, ns: &[&'ar str]) -> Self {
            for n in ns {
                self = self.requires(n);
            }
            self
        }

        /// Sets the exclusion rules of this group. Exclusion rules function just like argument
        /// exclusion rules, you can name other arguments or groups that must not be present when one
        /// of the arguments from this group are used.
        ///
        /// **NOTE:** The name provided may be an argument, or group name
        ///
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// # ArgGroup::with_name("config")
        /// .conflicts_with("config")
        /// # ).get_matches();
        pub fn conflicts_with(mut self, n: &'ar str) -> Self {
            if let Some(ref mut confs) = self.conflicts {
                confs.push(n);
            } else {
                self.conflicts = Some(vec![n]);
            }
            self
        }

        /// Sets the exclusion rules of this group. Exclusion rules function just like argument
        /// exclusion rules, you can name other arguments or groups that must not be present when one
        /// of the arguments from this group are used.
        ///
        /// **NOTE:** The names provided may be an argument, or group name
        ///
        ///
        /// # Examples
        ///
        /// ```no_run
        /// # use clap::{App, ArgGroup};
        /// # let matches = App::new("myprog")
        /// #                 .arg_group(
        /// # ArgGroup::with_name("config")
        /// .conflicts_with_all(&["config", "input"])
        /// # ).get_matches();
        pub fn conflicts_with_all(mut self, ns: &[&'ar str]) -> Self {
            for n in ns {
                self = self.conflicts_with(n);
            }
            self
        }
    }

    impl<'n, 'ar> Debug for ArgGroup<'n, 'ar> {
        fn fmt(&self, f: &mut Formatter) -> Result {
            write!(f,
                   "{{
                name:{:?},
                args: {:?},
                required: {:?},
                requires: {:?},
                conflicts: {:?},
    }}",
                   self.name,
                   self.args,
                   self.required,
                   self.requires,
                   self.conflicts)
        }
    }

    #[cfg(test)]
    mod test {
        use super::ArgGroup;

        #[test]
        fn groups() {
            let g = ArgGroup::with_name("test")
                        .add("a1")
                        .add_all(&["a2", "a3"])
                        .add("a4")
                        .required(true)
                        .conflicts_with("c1")
                        .conflicts_with_all(&["c2", "c3"])
                        .conflicts_with("c4")
                        .requires("r1")
                        .requires_all(&["r2", "r3"])
                        .requires("r4");

            let args = vec!["a1", "a2", "a3", "a4"];
            let reqs = vec!["r1", "r2", "r3", "r4"];
            let confs = vec!["c1", "c2", "c3", "c4"];

            assert_eq!(g.args, args);
            assert_eq!(g.requires.unwrap(), reqs);
            assert_eq!(g.conflicts.unwrap(), confs);

        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)