clean_cli 0.1.2

In programm command line parser using for repl
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use super::command::*;
use super::context::*;
use super::error::*;
use super::parameter::*;

use std::{
    borrow::BorrowMut,
    fmt::Debug,
    rc::Rc,
    str::FromStr,
    collections::{
        HashMap,
        VecDeque
    }
};

///  __Cli__ is a central unit that contains all possible commands, arguments and handlers.
/// To create instance using build pattern. Generic parameter using for return values from handlers.
/// 
/// # Example
///
/// ```rust
/// use clean_cli::*;
///
/// let cli = Cli::<bool>::builder()
///      .command(CommandBuilder::with_name("cmd")
///          .handler(|ctx| {
///              // some logic
///              return true;
///          })
///      )
///      .build();
///
/// assert!(cli.exec_line("cmd").unwrap());
/// ```
#[derive(Debug)]
pub struct Cli<R: Default> {
    commands: HashMap<String, Rc<Command<R>>>,
    print_error: bool,
    print_help: bool,
}

enum ParseState {
    ReadFirst,
    ReadNext,
    ParametersReaded{ params: VecDeque<Rc<Parameter>> },
}

impl<R: Default> Cli<R> {
    /// Create builder
    pub fn builder() -> CliBuilder<R> {
        CliBuilder::default()
    }

    /// Execute _line_ 
    pub fn exec_line(&self, line: &str) -> Result<R, Error> {
        let mut ctx = Context::default();
        let mut state = ParseState::ReadFirst;
        let mut pos = 0;
        let args = split_line(line);

        for arg in args.iter() {
            match state {
                ParseState::ReadFirst => {
                    if arg.starts_with("--") || arg.starts_with("-") {
                        return self.make_error(Kind::CantExecuteParameter, arg.to_string());
                    } else if let Some(cmd) = self.commands.get(*arg) {
                        ctx.units.push(ContextUnit {
                            command: (arg, cmd.clone()),
                            parameters: Default::default(),
                            value: None,
                        });
                        state = ParseState::ReadNext;
                    } else {
                        return self.make_error(Kind::NotCommand, arg.to_string());
                    }
                }

                ParseState::ReadNext => {
                    let last_unit = &mut ctx.units[pos];
                    let cmd = last_unit.command.1.clone();
                    let mut new_state: Option<ParseState> = None;

                    if arg.starts_with("--") {
                        let arg = &arg[2..];
                        if let Some(p) = cmd.parameters.get(arg) {
                            if let ArgType::Bool = p.value_type {
                                if let Some(p) = last_unit.command.1.parameters.get(arg) {
                                    last_unit.parameters.insert(p.name.clone(), (p.clone(), ArgValue::Bool(true)));
                                }
                            } else {
                                let mut params = VecDeque::with_capacity(1);
                                params.push_back(p.clone());
                                new_state = Some(ParseState::ParametersReaded{ params });
                            }
                        } else {
                            return self.make_error(Kind::NotParameter, arg.to_string());
                        }
                    } else if arg.starts_with("-") {
                        let arg = &arg[1..];
                        let mut params = VecDeque::with_capacity(arg.len());
                        for a in arg.chars() {
                            let s = a.to_string();
                            if let Some(p) = cmd.parameters.get(&s) {
                                if let ArgType::Bool = p.value_type {
                                    if let Some(p) = last_unit.command.1.parameters.get(&s) {
                                        last_unit.parameters.insert(p.name.clone(), (p.clone(), ArgValue::Bool(true)));
                                    }
                                }
                                else {
                                    params.push_back(p.clone());
                                }
                            } else {
                                return self.make_error(Kind::NotParameter, arg.to_string());
                            }
                        }

                        if !params.is_empty() {
                            new_state = Some(ParseState::ParametersReaded{
                                params
                            });
                        }
                    } else if let Some(v) = cmd.value.as_ref() {
                        match parse_arg(v.clone(), arg) {
                            Ok(value) => {
                                last_unit.value = Some(value);
                            }
                            Err(details) => return self.make_error(Kind::ValueParseFailed, details)
                        };
                    } else if let Some(sub) = cmd.subcommands.get(*arg) {
                        ctx.units.push(ContextUnit {
                            command: (arg, sub.clone()),
                            parameters: Default::default(),
                            value: None,
                        });
                        pos += 1;
                        new_state = Some(ParseState::ReadNext);
                    } else {
                        return self.make_error(Kind::NotCommand, arg.to_string());
                    }

                    if let Some(s) = new_state {
                        state = s;
                    }
                }

                ParseState::ParametersReaded{ mut params} => {
                    let last_unit = &mut ctx.units[pos];

                    let param= params.pop_front().unwrap();
                    match parse_arg(param.value_type.clone(), arg) {
                        Ok(value) => {
                            last_unit.parameters.insert(param.name.clone(), (param.clone(), value));
                            if params.is_empty() {
                                state = ParseState::ReadNext;
                            } else {
                                state = ParseState::ParametersReaded{ params };
                            }
                        }
                        Err(details) => {
                            return self.make_error(Kind::ValueParseFailed, details);
                        }
                    };
                }
            }
        }

        if let ParseState::ParametersReaded{mut params} = state {
            if params.len() > 1 {
                return self.make_error(Kind::ParserError, format!("Wrong params value: {}", params.len()));
            }
            let param = params.pop_back().unwrap();
            match param.value_type {
                ArgType::Bool => {}
                _ => return self.make_error(Kind::ParameterValueMissed, format!("parametr \"{}\" has no value", param.name))
            }
        };

        if let Some(cmd) = ctx.units.last() {
            let cmd = cmd.command.1.clone();
            if let Some(f) = &cmd.exec {
                return Ok(f.borrow_mut()(ctx));
            } else {
                return self.make_error(Kind::CantExecuteCommand, format!(""));
            }
        }

        Ok(Default::default())
    }

    fn make_error(&self, kind: Kind, details: String) -> Result<R, Error> {
        let error = Error { kind, details };
        if self.print_error {
            println!("{}", error)
        }
        if self.print_help {
            println!("HELP TEXT UNIMPLEMENTED")
        }
        Err(error)
    }
}

/// **CliBuilder** is a helper using for build **Cli**.
#[derive(Default, Debug)]
pub struct CliBuilder<R> {
    commands: HashMap<String, Rc<Command<R>>>,
    print_error: bool,
    print_help: bool,
}

impl<R: Default> CliBuilder<R> {
    /// Add command
    pub fn command(mut self, cmd: CommandBuilder<R>) -> Self {
        add_command(self.commands.borrow_mut(), cmd);
        self
    }

    /// Switch output error message to stdout.
    pub fn print_error(mut self, enable: bool) -> Self {
        self.print_error = enable;
        self
    }

    /// Switch output help message to stdout.
    pub fn print_help(mut self, enable: bool) -> Self {
        self.print_help = enable;
        self
    }

    /// Build and return **Cli** object.
    pub fn build(self) -> Cli<R> {
        Cli {
            commands: self.commands,
            print_error: self.print_error,
            print_help: self.print_help,
        }
    }
}

enum LineParseState {
    EndWord,
    StartWord{start: usize, quote: Option<char>}
}

fn split_line(line: &str) -> Vec<&str> {
    let mut state = LineParseState::EndWord;
    let line_len = line.len();
    let result = line.char_indices()
        .map(move |(i, c)| {
            let mut result: Option<_> = None;
            if !c.is_whitespace() {
                match state {
                    LineParseState::EndWord => {
                        let has_quote = c == '\'' || c == '\"';
                        state = LineParseState::StartWord{
                            start: if has_quote { i+1 } else { i }, 
                            quote: if has_quote { Some(c) } else { None }
                        }
                    }
                    LineParseState::StartWord { start, quote } => {
                        if let Some(q) = quote {
                            if q == c {
                                result = Some(&line[start..i]);
                                state = LineParseState::EndWord;
                            }
                        }
                    },
                }

                if line_len == i+1 {
                    if let LineParseState::StartWord { start, quote: _ } = state {
                        result = Some(&line[start..]);
                    }
                }
            } else {
                match state {
                    LineParseState::EndWord => {},
                    LineParseState::StartWord { start, quote } => {
                        if quote.is_none() {
                            result = Some(&line[start..i]);
                            state = LineParseState::EndWord;
                        }
                    },
                }
            }
            result
        })
        .filter(|x| x.is_some())
        .map(|s| s.unwrap())
        .collect::<Vec<&str>>();
    result
}

fn parse_arg(arg_type: ArgType, arg: &str) -> std::result::Result<ArgValue, String> {
    if arg.starts_with("-") && !(arg_type != ArgType::Int || arg_type != ArgType::Float) {
        return Err(format!("Seems {} is not a value", arg));
    }

    let value: ArgValue = match arg_type {
        ArgType::Bool => match arg {
            "true" | "yes" | "1" | "on"  => ArgValue::Bool(true),
            "false" | "no" | "0" | "off" => ArgValue::Bool(false),
            _ => return Err(format!("\"{}\" is not a boolean value. \
                                    Use \"1\", \"true\", \"yes\", \"on\" for true, \
                                    and \"0\", \"false\", \"no\", \"off\" for false", arg))
        }

        ArgType::Int => {
            match i64::from_str(arg) {
                Ok(i) => ArgValue::Int(i),
                Err(e) => return Err(format!("Parse int error: {}", e))
            }
        }

        ArgType::Float => {
            match f64::from_str(arg) {
                Ok(f) => ArgValue::Float(f),
                Err(e) => return Err(format!("Parse float error: {}", e))
            }
        }

        ArgType::String => ArgValue::String(arg.to_string()),
    };

    Ok(value)
}

#[cfg(test)]
mod test {
    use crate::{ArgType, ArgValue};

    #[test]
    fn split_line() {
        let line = "one two three";
        let v = super::split_line(line);
        assert_eq!(v[0], "one");
        assert_eq!(v[1], "two");
        assert_eq!(v[2], "three");
    }
    
    #[test]
    fn split_line_with_quotes() {
        let line = "one \"two\" three";
        let v = super::split_line(line);
        assert_eq!(v[0], "one");
        assert_eq!(v[1], "two");
        assert_eq!(v[2], "three");

        let line = "one \"two; two and half\" three";
        let v = super::split_line(line);
        assert_eq!(v[0], "one");
        assert_eq!(v[1], "two; two and half");
        assert_eq!(v[2], "three");

        let line = "one two \"three\"";
        let v = super::split_line(line);
        assert_eq!(v[0], "one");
        assert_eq!(v[1], "two");
        assert_eq!(v[2], "three");
    }

    #[test]
    fn split_line_with_bad_quotes() {
        let line = "one two \"three";
        let v = super::split_line(line);
        assert_eq!(v[0], "one");
        assert_eq!(v[1], "two");
        assert_eq!(v[2], "three");
    }

    #[test]
    fn parse_arg_bool() {
        let f = |arg, state| {
            let result = super::parse_arg(ArgType::Bool, arg);
            match result {
                Ok(arg_value) => match arg_value {
                    ArgValue::Bool(v) => assert_eq!(v, state),
                    _ => panic!("bad type")
                }
                Err(err) => panic!("{:?}", err)
            }
        };
         let true_args = ["true", "1", "yes", "on"];
        for arg in true_args.iter() { f(arg, true); }
         let false_args = ["false", "0", "no", "off"];
        for arg in false_args.iter() { f(arg, false); }
    }
     #[test]
    fn parse_arg_bool_error() {
        let result = super::parse_arg(ArgType::Bool, "not_a_bool");
        match result {
            Ok(arg_value) => panic!("error expected, but got this: {:?}", arg_value),
            Err(err) => assert!(!err.is_empty())
        }
    }

    #[test]
    fn parse_arg_int() {
        use rand::prelude::*;

        let mut numbers = Vec::<(String,i64)>::with_capacity(12);
        numbers.push((i64::MIN.to_string(), i64::MIN));
        numbers.push((i64::MAX.to_string(), i64::MAX));
        for _ in 0..100 {
            let num: i64 = random();
            numbers.push((num.to_string(), num));
        }
        for (arg, state) in numbers.iter() {
            let result = super::parse_arg(ArgType::Int, arg.as_str());
            match result {
                Ok(arg_value) => match arg_value {
                    ArgValue::Int(v) => assert_eq!(v, *state),
                    _ => panic!("bad type")
                }
                Err(err) => panic!("{:?}", err)
            }
        }
    }

    #[test]
    fn parse_arg_int_error() {
        let result = super::parse_arg(ArgType::Int, "not_int");
        match result {
            Ok(arg_value) => panic!("error expected, but got this: {:?}", arg_value),
            Err(err) => assert!(!err.is_empty())
        }
    }

    #[test]
    fn parse_arg_float() {
        use rand::prelude::*;

        let mut numbers = Vec::<(String,f64)>::with_capacity(12);
        numbers.push((f64::MIN.to_string(), f64::MIN));
        numbers.push((f64::MAX.to_string(), f64::MAX));

        let mut rnd = thread_rng();
        for _ in 0..100 {
            let num: f64 = rnd.gen_range(f64::MIN .. f64::MAX);
            numbers.push((num.to_string(), num));
        }
        for (arg, state) in numbers.iter() {
            let result = super::parse_arg(ArgType::Float, arg.as_str());
            match result {
                Ok(arg_value) => match arg_value {
                    ArgValue::Float(v) => {
                        println!("arg: {} == v: {}", arg, v);
                        assert_eq!(v, *state)
                    }
                    _ => panic!("bad type")
                }
                Err(err) => panic!("{:?}", err)
            }
        }
    }

    #[test]
    fn parse_arg_float_error() {
        let result = super::parse_arg(ArgType::Float, "not_float");
        match result {
            Ok(arg_value) => panic!("error expected, but got this: {:?}", arg_value),
            Err(err) => assert!(!err.is_empty())
        }
    }
}