      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

    // use std::collections::HashSet;
    use std::fmt::{Display, Formatter, Result};
    use std::convert::From;
    use std::rc::Rc;
    use std::result::Result as StdResult;

    use vec_map::VecMap;

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

    #[derive(Debug)]
    #[doc(hidden)]
    pub struct FlagBuilder<'n, 'e> {
        pub name: &'n str,
        pub long: Option<&'e str>,
        pub help: Option<&'e str>,
        pub blacklist: Option<Vec<&'e str>>,
        pub requires: Option<Vec<&'e str>>,
        pub short: Option<char>,
        pub overrides: Option<Vec<&'e str>>,
        pub settings: ArgFlags,
        pub disp_ord: usize,
    }

    impl<'n, 'e> Default for FlagBuilder<'n, 'e> {
        fn default() -> Self {
            FlagBuilder {
                name: "",
                long: None,
                help: None,
                blacklist: None,
                requires: None,
                short: None,
                overrides: None,
                settings: ArgFlags::new(),
                disp_ord: 999,
            }
        }
    }

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

    impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for FlagBuilder<'a, 'b> {
        fn from(a: &'z Arg<'a, 'b>) -> Self {
            assert!(a.validator.is_none(),
                format!("The argument '{}' has a validator set, yet was parsed as a flag. Ensure \
                    .takes_value(true) or .index(u64) is set.", a.name));
            assert!(a.possible_vals.is_none(),
                format!("The argument '{}' cannot have a specific value set because it doesn't \
                    have takes_value(true) set",
                       a.name));
            assert!(!a.is_set(ArgSettings::Required),
                format!("The argument '{}' cannot be required because it's a flag, perhaps you forgot \
                    takes_value(true)?", a.name));
            // No need to check for index() or takes_value() as that is handled above

            FlagBuilder {
                name: a.name,
                short: a.short,
                long: a.long,
                help: a.help,
                blacklist: a.blacklist.clone(),
                overrides: a.overrides.clone(),
                requires: a.requires.clone(),
                settings: a.settings,
                disp_ord: a.disp_ord,
            }
        }
    }

    impl<'n, 'e> Display for FlagBuilder<'n, 'e> {
        fn fmt(&self, f: &mut Formatter) -> Result {
            if let Some(l) = self.long {
                write!(f, "--{}", l)
            } else {
                write!(f, "-{}", self.short.unwrap())
            }
        }
    }

    impl<'n, 'e> Clone for FlagBuilder<'n, 'e> {
        fn clone(&self) -> Self {
            FlagBuilder {
                name: self.name,
                short: self.short,
                long: self.long,
                help: self.help,
                blacklist: self.blacklist.clone(),
                overrides: self.overrides.clone(),
                requires: self.requires.clone(),
                settings: self.settings,
                disp_ord: self.disp_ord,
            }
        }
    }

    impl<'n, 'e> AnyArg<'n, 'e> for FlagBuilder<'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]> { None }
        fn is_set(&self, s: ArgSettings) -> bool { self.settings.is_set(s) }
        fn has_switch(&self) -> bool { true }
        fn takes_value(&self) -> bool { false }
        fn set(&mut self, s: ArgSettings) { self.settings.set(s) }
        fn max_vals(&self) -> Option<u64> { None }
        fn val_names(&self) -> Option<&VecMap<&'e str>> { None }
        fn num_vals(&self) -> Option<u64> { None }
        fn possible_vals(&self) -> Option<&[&'e str]> { None }
        fn validator(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> { None }
        fn min_vals(&self) -> Option<u64> { None }
        fn short(&self) -> Option<char> { self.short }
        fn long(&self) -> Option<&'e str> { self.long }
        fn val_delim(&self) -> Option<char> { None }
        fn help(&self) -> Option<&'e str> { self.help }
        fn default_val(&self) -> Option<&'n str> { None }
        fn longest_filter(&self) -> bool { self.long.is_some() }
    }

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

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

        #[test]
        fn flagbuilder_display() {
            let mut f = FlagBuilder::new("flg");
            f.settings.set(ArgSettings::Multiple);
            f.long = Some("flag");

            assert_eq!(&*format!("{}", f), "--flag");

            let mut f2 = FlagBuilder::new("flg");
            f2.short = Some('f');

            assert_eq!(&*format!("{}", f2), "-f");
        }
    }

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