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
use std::str::FromStr;

use crate::{
    input::{Completor, Input, InputType},
    parser::{CliResult, ParseError},
};

#[derive(Default)]
pub struct Arg<'a, T: FromStr> {
    pub name: String,
    pub description: Option<String>,
    pub value: Option<T>,
    pub completor: Option<Completor<'a>>,
}

impl<'a, T> Arg<'a, T>
where
    T: FromStr,
{
    pub fn name(name: &str) -> Self {
        Self {
            name: name.to_string(),
            description: None,
            value: None,
            completor: None,
        }
    }

    pub fn description(mut self, description: &str) -> Self {
        self.description = Some(description.to_string());
        self
    }

    pub fn get(&self) -> &T {
        self.value.as_ref().unwrap()
    }

    pub fn completor<F>(mut self, completor: F) -> Self
    where
        F: FnMut(&str) -> Vec<String> + 'a,
    {
        self.completor = Some(Box::new(completor));
        self
    }
}

impl<'a> Arg<'a, String> {
    pub fn str(name: &str) -> Self {
        Self::name(name)
    }
}

impl<'a> Arg<'a, i32> {
    pub fn i32(name: &str) -> Self {
        Self::name(name)
    }
}

impl<'a, T: FromStr> Input for Arg<'a, T> {
    fn parse(&mut self, token: &str) -> CliResult<bool> {
        if token.len() > 2 && &token[0..1] == "-" && &token[0..2] == "--" {
            eprintln!(
                "unexpected flag \"{}\" found while looking for argument \"{}\"",
                token, self.name
            );
            return Err(ParseError::UnexpectedToken);
        }
        self.value = Some(token.parse().map_err(|_| {
            eprintln!("{} cannot be parsed for {}.", token, self.name);
            ParseError::FromStrFailure
        })?);

        Ok(true)
    }

    fn display_name(&self) -> String {
        self.name.clone()
    }

    fn type_name(&self) -> InputType {
        InputType::Arg
    }

    fn parsed(&self) -> bool {
        self.value.is_some()
    }

    fn complete(&mut self, value: &str) -> Vec<String> {
        if let Some(completor) = &mut self.completor {
            completor(value)
        } else {
            vec![]
        }
    }

    fn is_bool_flag(&self) -> bool {
        false
    }

    fn description(&self) -> Option<String> {
        self.description.clone()
    }
}