      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

    use std::fmt::{Display, Formatter, Result};
    use std::result::Result as StdResult;
    use std::rc::Rc;

    use vec_map::VecMap;

    use Arg;
    use args::{AnyArg, DispOrder};
    use args::settings::{ArgFlags, ArgSettings};

    #[allow(missing_debug_implementations)]
    #[doc(hidden)]
    pub struct PosBuilder<'n, 'e> {
        pub name: &'n str,
        pub help: Option<&'e str>,
        pub requires: Option<Vec<&'e str>>,
        pub blacklist: Option<Vec<&'e str>>,
        pub possible_vals: Option<Vec<&'e str>>,
        pub index: u64,
        pub num_vals: Option<u64>,
        pub max_vals: Option<u64>,
        pub min_vals: Option<u64>,
        pub val_names: Option<VecMap<&'e str>>,
        pub validator: Option<Rc<Fn(String) -> StdResult<(), String>>>,
        pub overrides: Option<Vec<&'e str>>,
        pub settings: ArgFlags,
        pub val_delim: Option<char>,
        pub default_val: Option<&'n str>,
        pub disp_ord: usize,
        pub r_unless: Option<Vec<&'e str>>,
    }

    impl<'n, 'e> Default for PosBuilder<'n, 'e> {
        fn default() -> Self {
            PosBuilder {
                name: "",
                help: None,
                requires: None,
                blacklist: None,
                possible_vals: None,
                index: 0,
                num_vals: None,
                min_vals: None,
                max_vals: None,
                val_names: None,
                validator: None,
                overrides: None,
                settings: ArgFlags::new(),
                val_delim: Some(','),
                default_val: None,
                disp_ord: 999,
                r_unless: None,
            }
        }
    }

    impl<'n, 'e> PosBuilder<'n, 'e> {
        pub fn new(name: &'n str, idx: u64) -> Self {
            PosBuilder {
                name: name,
                index: idx,
                ..Default::default()
            }
        }

        pub fn from_arg(a: &Arg<'n, 'e>, idx: u64, reqs: &mut Vec<&'e str>) -> Self {
            debug_assert!(a.short.is_none() || a.long.is_none(),
                format!("Argument \"{}\" has conflicting requirements, both index() and short(), \
                    or long(), were supplied", a.name));

            // Create the Positional Argument Builder with each HashSet = None to only
            // allocate
            // those that require it
            let mut pb = PosBuilder {
                name: a.name,
                index: idx,
                num_vals: a.num_vals,
                min_vals: a.min_vals,
                max_vals: a.max_vals,
                val_names: a.val_names.clone(),
                blacklist: a.blacklist.clone(),
                overrides: a.overrides.clone(),
                requires: a.requires.clone(),
                possible_vals: a.possible_vals.clone(),
                help: a.help,
                val_delim: a.val_delim,
                settings: a.settings,
                default_val: a.default_val,
                disp_ord: a.disp_ord,
                r_unless: a.r_unless.clone(),
                ..Default::default()
            };
            if a.max_vals.is_some()
                || a.min_vals.is_some()
                || (a.num_vals.is_some() && a.num_vals.unwrap() > 1) {
                pb.settings.set(ArgSettings::Multiple);
            }
            if let Some(ref p) = a.validator {
                pb.validator = Some(p.clone());
            }
            // If the arg is required, add all it's requirements to master required list
            if a.is_set(ArgSettings::Required) {
                if let Some(ref areqs) = a.requires {
                    for r in areqs { reqs.push(*r); }
                }
            }
            pb
        }

    }

    impl<'n, 'e> Display for PosBuilder<'n, 'e> {
        fn fmt(&self, f: &mut Formatter) -> Result {
            if self.settings.is_set(ArgSettings::Required) {
                if let Some(ref names) = self.val_names {
                    try!(write!(f, "{}", names.values().map(|n| format!("<{}>", n)).collect::<Vec<_>>().join(" ")));
                } else {
                    try!(write!(f, "<{}>", self.name));
                }
            } else {
                if let Some(ref names) = self.val_names {
                    try!(write!(f, "{}", names.values().map(|n| format!("[{}]", n)).collect::<Vec<_>>().join(" ")));
                } else {
                    try!(write!(f, "[{}]", self.name));
                }
            }
            if self.settings.is_set(ArgSettings::Multiple) && self.val_names.is_none() {
                try!(write!(f, "..."));
            }

            Ok(())
        }
    }

    impl<'n, 'e> Clone for PosBuilder<'n, 'e> {
        fn clone(&self) -> Self {
            PosBuilder {
                name: self.name,
                help: self.help,
                blacklist: self.blacklist.clone(),
                overrides: self.overrides.clone(),
                requires: self.requires.clone(),
                settings: self.settings,
                disp_ord: self.disp_ord,
                num_vals: self.num_vals,
                min_vals: self.min_vals,
                max_vals: self.max_vals,
                val_names: self.val_names.clone(),
                val_delim: self.val_delim,
                possible_vals: self.possible_vals.clone(),
                default_val: self.default_val,
                validator: self.validator.clone(),
                r_unless: self.r_unless.clone(),
                index: self.index,
            }
        }
    }

    impl<'n, 'e> AnyArg<'n, 'e> for PosBuilder<'n, 'e> {
        fn name(&self) -> &'n str { self.name }
        fn overrides(&self) -> Option<&[&'e str]> { self.overrides.as_ref().map(|o| &o[..]) }
        fn requires(&self) -> Option<&[&'e str]> { self.requires.as_ref().map(|o| &o[..]) }
        fn blacklist(&self) -> Option<&[&'e str]> { self.blacklist.as_ref().map(|o| &o[..]) }
        fn required_unless(&self) -> Option<&[&'e str]> { self.r_unless.as_ref().map(|o| &o[..]) }
        fn val_names(&self) -> Option<&VecMap<&'e str>> { self.val_names.as_ref() }
        fn is_set(&self, s: ArgSettings) -> bool { self.settings.is_set(s) }
        fn set(&mut self, s: ArgSettings) { self.settings.set(s) }
        fn has_switch(&self) -> bool { false }
        fn max_vals(&self) -> Option<u64> { self.max_vals }
        fn num_vals(&self) -> Option<u64> { self.num_vals }
        fn possible_vals(&self) -> Option<&[&'e str]> { self.possible_vals.as_ref().map(|o| &o[..]) }
        fn validator(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> {
            self.validator.as_ref()
        }
        fn min_vals(&self) -> Option<u64> { self.min_vals }
        fn short(&self) -> Option<char> { None }
        fn long(&self) -> Option<&'e str> { None }
        fn val_delim(&self) -> Option<char> { self.val_delim }
        fn takes_value(&self) -> bool { true }
        fn help(&self) -> Option<&'e str> { self.help }
        fn default_val(&self) -> Option<&'n str> { self.default_val }
        fn longest_filter(&self) -> bool { true }
    }

    impl<'n, 'e> DispOrder for PosBuilder<'n, 'e> {
        fn disp_ord(&self) -> usize { self.disp_ord }
    }

    #[cfg(test)]
    mod test {
        use super::PosBuilder;
        use args::settings::ArgSettings;
        use vec_map::VecMap;

        #[test]
        fn display_mult() {
            let mut p = PosBuilder::new("pos", 1);
            p.settings.set(ArgSettings::Multiple);

            assert_eq!(&*format!("{}", p), "[pos]...");
        }

        #[test]
        fn display_required() {
            let mut p2 = PosBuilder::new("pos", 1);
            p2.settings.set(ArgSettings::Required);

            assert_eq!(&*format!("{}", p2), "<pos>");
        }

        #[test]
        fn display_val_names() {
            let mut p2 = PosBuilder::new("pos", 1);
            let mut vm = VecMap::new();
            vm.insert(0, "file1");
            vm.insert(1, "file2");
            p2.val_names = Some(vm);

            assert_eq!(&*format!("{}", p2), "[file1] [file2]");
        }

        #[test]
        fn display_val_names_req() {
            let mut p2 = PosBuilder::new("pos", 1);
            p2.settings.set(ArgSettings::Required);
            let mut vm = VecMap::new();
            vm.insert(0, "file1");
            vm.insert(1, "file2");
            p2.val_names = Some(vm);

            assert_eq!(&*format!("{}", p2), "<file1> <file2>");
        }
    }

# 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`)