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
use crate::Action;

use crate::parser;
use crate::Context;
use crate::Flag;
use crate::Vector;
use std::collections::VecDeque;
use std::path::PathBuf;

#[derive(Clone, Default)]
pub struct Command {
    pub name: String,
    pub action: Option<Action>,
    pub authors: String,
    pub copyright: String,
    pub description: Option<String>,
    pub usage: String,
    pub l_flags: Vector<Flag>,
    pub c_flags: Vector<Flag>,
    pub alias: Vector<String>,
    pub version: String,
    pub sub: Vector<Command>,
    pub opt_values: Vector<KeyValuePair>,
}

pub type KeyValuePair = (String, String);

impl Command {
    pub fn new() -> Command {
        Command::default()
    }

    #[allow(clippy::too_many_arguments)]
    pub fn build_new(
        name: String,
        action: Option<Action>,
        authors: String,
        copyright: String,
        description: Option<String>,
        usage: String,
        local_flags: Vector<Flag>,
        common_flags: Vector<Flag>,
        alias: Vector<String>,
        version: String,
        sub: Vector<Command>,
        opt_values: Vector<(String, String)>,
    ) -> Command {
        Command {
            name,
            action,
            authors,
            copyright,
            description,
            usage,
            l_flags: local_flags,
            c_flags: common_flags,
            alias,
            version,
            sub,
            opt_values,
        }
    }

    pub fn run_with_auto_arg_collect(self) {
        //let args: Vec<String> = std::env::args().collect();
        //self.run(args);
        self.run(std::env::args().collect::<Vec<String>>());
    }

    pub fn single_run(self, args: Vec<String>) {
        println!("{:?}", args);
        // match self.action {
        //     Some(action) => {
        //         action(&Context::new(args,Vector::new(None),self.c_flags);
        //     }
        //     None => {
        //         self.show_help();
        //     }
        // }
    }

    pub fn show_help(self) {}

    pub fn name<T: Into<String>>(mut self, name: T) -> Command {
        self.name = name.into();
        self
    }

    pub fn usage<T: Into<String>>(mut self, usage: T) -> Self {
        self.usage = usage.into();
        self
    }

    pub fn action(mut self, action: Action) -> Self {
        self.action = Some(action);
        self
    }

    pub fn authors<T: Into<String>>(mut self, authors: T) -> Self {
        self.authors = authors.into();
        self
    }

    pub fn copyright<T: Into<String>>(mut self, copyright: T) -> Self {
        self.copyright = copyright.into();
        self
    }

    pub fn local_flag(mut self, flag: Flag) -> Self {
        self.l_flags.push(flag);
        self
    }

    pub fn common_flag(mut self, flag: Flag) -> Self {
        self.c_flags.push(flag);
        self
    }

    pub fn desctiption<T: Into<String>>(mut self, description: T) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn version<T: Into<String>>(mut self, version: T) -> Self {
        self.version = version.into();
        self
    }

    pub fn sub_command(mut self, sub_command: Command) -> Self {
        self.sub.push(sub_command);
        self
    }

    pub fn alias<T: Into<String>>(mut self, a: T) -> Self {
        self.alias.push(a.into());
        self
    }

    pub fn add_opt_prop(mut self, opt_prop: KeyValuePair) -> Self {
        self.opt_values.push(opt_prop);
        self
    }

    pub fn is(&self, name_or_alias: &str) -> bool {
        if name_or_alias == self.name {
            true
        } else {
            match self.alias.inner() {
                None => false,
                Some(inner) => inner.iter().any(|a| a == name_or_alias),
            }
        }
    }

    pub fn find_sub(&self, name: &str) -> Option<&Command> {
        match &self.sub {
            Vector(None) => None,
            Vector(Some(inner)) => inner.iter().find(|c| c.is(name)),
        }
    }
}

impl From<String> for Command {
    fn from(name: String) -> Self {
        Command {
            name,
            action: None,
            authors: String::default(),
            copyright: String::default(),
            description: None,
            usage: String::default(),
            l_flags: Vector::default(),
            c_flags: Vector::default(),
            alias: Vector::default(),
            version: String::default(),
            sub: Vector::default(),
            opt_values: Vector::default(),
        }
    }
}

pub trait Run<T> {
    fn run(self, args: T);
}

impl Run<Vec<String>> for Command {
    fn run(self, args: Vec<String>) {
        self.run_from_args(args);
    }
}

impl Run<Context> for Command {
    fn run(self, c: Context) {
        self.run_in_context(c);
    }
}

impl Command {
    pub fn run_from_args(self, raw_args: Vec<String>) {
        println!("{:?}, len: {}", &raw_args, &raw_args.len());
        if raw_args.len() < 2 {
            match self.action {
                Some(action) => {
                    action(&Context::from(raw_args));
                }
                None => {
                    println!("args: {:?}", raw_args);
                }
            }
        } else {
            let mut args = VecDeque::from(raw_args.clone());
            let current_path = args.pop_front().unwrap();
            let flag_prefix = "-";
            let long_flag_prefix = "--";
            //get before first non-flag arg with parsing flags
            //parser::parse_until_not_flag_args_or_end_args(args, self.c_flags, self.l_flags);
            match args.pop_front().unwrap() {
                arg if arg.starts_with(long_flag_prefix) => {
                    //long flag
                    let c = Context::new(raw_args, args, self.c_flags, self.l_flags, &current_path);
                    parser::parse_flags_starts_with_long_flag(arg, c);
                }
                arg if arg.starts_with(flag_prefix) => {
                    //short flag
                    let c = Context::new(raw_args, args, self.c_flags, self.l_flags, &current_path);
                    parser::parse_flags_starts_with_short_flag(arg, c);
                }
                arg => {
                    //non-flag (normal) arg
                    println!("arg: {}", &arg);
                    match self.find_sub(&arg) {
                        None => match self.action {
                            None => println!("{} does not have its own action.", self.name),
                            Some(action) => {
                                let c = Context::new(
                                    raw_args,
                                    args,
                                    self.c_flags,
                                    self.l_flags,
                                    &current_path,
                                );
                                let context = parser::parse_until_end_args(c);
                                action(&context);
                            }
                        },
                        Some(_) => {}
                    }
                }
            }
        }
    }

    pub fn run_in_context(self, context: Context) {
        println!("{:?}", context);
    }
}