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
//! # parser
//!
//! Parsers arguments line based on given spec and returned parsed data.
//!

#[cfg(test)]
#[path = "./parser_test.rs"]
mod parser_test;

use crate::types::{
    Argument, ArgumentOccurrence, ArgumentValueType, CliParsed, CliSpec, Command, ParserError,
};
use std::env;
use std::ffi::{OsStr, OsString};
use std::path::Path;

/// Parsers the given command line based on the given spec and returns the result.<br>
/// In case of invalid input or the provided spec does not match the command line, an error will be returned.
pub(crate) fn parse(command_line: &Vec<&str>, spec: &CliSpec) -> Result<CliParsed, ParserError> {
    if let Err(error) = validate_input(command_line, spec) {
        return Err(error);
    }

    // fetch the command
    let (valid, args_start_index) = parse_command(&command_line, spec);
    if !valid {
        return Err(ParserError::InvalidCommandLine(
            format!(
                "Command does not match spec, command line: {:?}",
                command_line
            )
            .to_string(),
        ));
    }

    let mut cli_parsed = CliParsed::new();
    if args_start_index > command_line.len() {
        return Ok(cli_parsed);
    }

    let arguments_line = &command_line[args_start_index..];
    match parse_arguments(arguments_line, spec, &mut cli_parsed) {
        Ok(_) => {
            insert_default_values(spec, &mut cli_parsed);
            Ok(cli_parsed)
        }
        Err(error) => Err(error),
    }
}

/// Parsers the current process command line based on the given spec and returns the result.<br>
/// In case of invalid input or the provided spec does not match the command line, an error will be returned.
pub(crate) fn parse_process(spec: &CliSpec) -> Result<CliParsed, ParserError> {
    let command_line_strings = get_process_command_line();
    let command_line = command_line_strings
        .iter()
        .map(|value| value.as_str())
        .collect();
    parse(&command_line, spec)
}

/// Parsers the given command line based on the given specs and returns the result.<br>
/// In case of invalid input or none of the provided specs do not match the command line, an error will be returned.
pub(crate) fn parse_any(
    command_line: &Vec<&str>,
    specs: Vec<&CliSpec>,
) -> Result<(usize, CliParsed), ParserError> {
    let mut index = 0;
    for spec in specs {
        let result = parse(command_line, spec);
        if let Ok(cli_parsed) = result {
            return Ok((index, cli_parsed));
        }

        index = index + 1;
    }

    Err(ParserError::InvalidCommandLine(
        "Command does not match any spec".to_string(),
    ))
}

/// Parsers the current process command line based on the given specs and returns the result.<br>
/// In case of invalid input or none of the provided specs do not match the command line, an error will be returned.
pub(crate) fn parse_process_any(specs: Vec<&CliSpec>) -> Result<(usize, CliParsed), ParserError> {
    let command_line_strings = get_process_command_line();
    let command_line = command_line_strings
        .iter()
        .map(|value| value.as_str())
        .collect();
    parse_any(&command_line, specs)
}

#[cfg(not(test))]
fn get_process_command_line() -> Vec<String> {
    env::args().collect()
}

#[cfg(test)]
fn get_process_command_line() -> Vec<String> {
    env::var("TEST_CLI_PARSER_COMMAND_LINE")
        .unwrap_or("".to_string())
        .split(" ")
        .map(|value| value.to_string())
        .collect()
}

fn insert_default_values(spec: &CliSpec, cli_parsed: &mut CliParsed) {
    for argument_spec in &spec.arguments {
        if !cli_parsed.arguments.contains(&argument_spec.name) {
            if argument_spec.value_type != ArgumentValueType::None {
                if let Some(ref default_value) = argument_spec.default_value {
                    cli_parsed
                        .argument_values
                        .insert(argument_spec.name.clone(), vec![default_value.to_string()]);
                }
            }
        }
    }
}

fn get_filename_without_extension(command: &str) -> Option<OsString> {
    let unix_path = command.replace("\\", "/");
    let path_value = Path::new(&unix_path);
    match path_value.file_stem() {
        Some(filename) => Some(filename.to_os_string()),
        None => None,
    }
}

fn parse_command(command_line: &Vec<&str>, spec: &CliSpec) -> (bool, usize) {
    if spec.command.is_empty() {
        return (true, 0);
    }

    let root_command_with_path = command_line[0];
    match get_filename_without_extension(root_command_with_path) {
        Some(root_command_file_name) => {
            for command in &spec.command {
                match command {
                    Command::Command(command_string) => {
                        if OsStr::new(command_string) == root_command_file_name {
                            return (true, 1);
                        }
                        ()
                    }
                    Command::SubCommand(command_strings) => {
                        if command_strings.len() <= command_line.len()
                            && OsStr::new(&command_strings[0]) == root_command_file_name
                        {
                            let mut found = true;
                            for index in 1..command_strings.len() {
                                if command_strings[index] != command_line[index] {
                                    found = false;
                                    break;
                                }
                            }

                            if found {
                                return (true, command_strings.len());
                            }
                        }

                        ()
                    }
                };
            }

            (false, 0)
        }
        None => (false, 0),
    }
}

fn parse_arguments(
    arguments_line: &[&str],
    spec: &CliSpec,
    cli_parsed: &mut CliParsed,
) -> Result<(), ParserError> {
    if arguments_line.is_empty() {
        return Ok(());
    }

    if spec.arguments.is_empty() && spec.positional_argument.is_none() {
        // we have arguments on the command line but we do not support arguments at all
        return Err(ParserError::InvalidCommandLine(
            "Positional arguments found but not allowed per spec".to_string(),
        ));
    }

    let positional_argument_name = match &spec.positional_argument {
        Some(positional_argument_spec) => Some(positional_argument_spec.name.clone()),
        None => None,
    };

    let mut argument_spec_in_scope: Option<Argument> = None;
    let mut started_positional = false;
    for argument_raw in arguments_line {
        if started_positional {
            insert_argument_value(
                cli_parsed,
                &positional_argument_name.clone().unwrap(),
                &argument_raw,
            );
        } else {
            // we found an argument key of single value type in previous index, now we get its value
            if let Some(ref argument_spec) = argument_spec_in_scope {
                if argument_spec.value_type == ArgumentValueType::Single {
                    insert_argument_value(cli_parsed, &argument_spec.name, &argument_raw);

                    argument_spec_in_scope = None;
                    continue;
                }
            }

            // The -- is a special case that defines start of positional arguments
            if *argument_raw == "--" {
                started_positional = true;
                continue;
            }

            // search for argument in arguments spec
            let mut argument_spec_found = None;
            let argument_key_value_parts = argument_raw.split_once("=");
            let mut argument_with_value = false;
            for argument_spec in &spec.arguments {
                for key in &argument_spec.key {
                    if key == argument_raw {
                        argument_spec_found = Some(argument_spec);
                        break;
                    } else {
                        if argument_spec.value_type == ArgumentValueType::Single {
                            match argument_key_value_parts {
                                Some(ref parts) => {
                                    let (key_prefix, _) = parts;
                                    if key == key_prefix {
                                        argument_spec_found = Some(argument_spec);
                                        argument_with_value = true;
                                        break;
                                    }
                                }
                                None => (),
                            }
                        }
                    }
                }
            }

            match argument_spec_in_scope {
                Some(ref current_argument_spec) => match argument_spec_found {
                    Some(found_argument_spec) => {
                        if cli_parsed.arguments.contains(&found_argument_spec.name)
                            && found_argument_spec.argument_occurrence
                                != ArgumentOccurrence::Multiple
                        {
                            return Err(ParserError::InvalidCommandLine(
                                format!("Duplicate argument {} found", &found_argument_spec.name)
                                    .to_string(),
                            ));
                        }

                        cli_parsed
                            .arguments
                            .insert(found_argument_spec.name.clone());

                        match found_argument_spec.value_type {
                            ArgumentValueType::Single | ArgumentValueType::Multiple => {
                                argument_spec_in_scope = Some(found_argument_spec.clone());
                            }
                            ArgumentValueType::None => (),
                        }

                        if argument_with_value {
                            let (_, value_part) = argument_key_value_parts.unwrap();
                            insert_argument_value(
                                cli_parsed,
                                &found_argument_spec.name,
                                value_part,
                            );
                            argument_spec_in_scope = None;
                        }
                    }
                    None => {
                        // current value is not a new argument key and we are currently in multi value key so
                        // lets add it as an additional value
                        if current_argument_spec.value_type == ArgumentValueType::Multiple {
                            insert_argument_value(
                                cli_parsed,
                                &current_argument_spec.name,
                                &argument_raw,
                            );
                        } else {
                            return Err(ParserError::InternalError(
                                "Non multiple argument found in scope".to_string(),
                            ));
                        }
                    }
                },
                None => match argument_spec_found {
                    Some(found_argument_spec) => {
                        if cli_parsed.arguments.contains(&found_argument_spec.name)
                            && found_argument_spec.argument_occurrence
                                != ArgumentOccurrence::Multiple
                        {
                            return Err(ParserError::InvalidCommandLine(
                                format!("Duplicate argument {} found", &found_argument_spec.name)
                                    .to_string(),
                            ));
                        }

                        cli_parsed
                            .arguments
                            .insert(found_argument_spec.name.clone());

                        match found_argument_spec.value_type {
                            ArgumentValueType::Single | ArgumentValueType::Multiple => {
                                argument_spec_in_scope = Some(found_argument_spec.clone());
                            }
                            ArgumentValueType::None => (),
                        }

                        if argument_with_value {
                            let (_, value_part) = argument_key_value_parts.unwrap();
                            insert_argument_value(
                                cli_parsed,
                                &found_argument_spec.name,
                                value_part,
                            );
                            argument_spec_in_scope = None;
                        }
                    }
                    None => match positional_argument_name {
                        // current value is not a new argument key and we are not in a scope of some argument
                        // so it must be a positional argument
                        Some(ref name) => {
                            started_positional = true;
                            insert_argument_value(cli_parsed, name, &argument_raw)
                        }
                        None => {
                            return Err(ParserError::InvalidCommandLine(
                                "Positional arguments found but not allowed per spec".to_string(),
                            ));
                        }
                    },
                },
            }
        }
    }

    Ok(())
}

fn validate_input(command_line: &Vec<&str>, spec: &CliSpec) -> Result<(), ParserError> {
    if command_line.is_empty() && !spec.command.is_empty() {
        return Err(ParserError::InvalidCommandLine(
            "Empty command line provided".to_string(),
        ));
    }

    if spec.command.is_empty() && spec.arguments.is_empty() {
        return Err(ParserError::InvalidCliSpec(
            "Empty cli spec provided".to_string(),
        ));
    }

    for argument in &spec.arguments {
        if argument.key.is_empty() {
            return Err(ParserError::InvalidCliSpec(
                "Argument key not defined".to_string(),
            ));
        }
    }

    for command in &spec.command {
        match command {
            Command::Command(command_string) => {
                if command_string.is_empty() {
                    return Err(ParserError::InvalidCliSpec(
                        "Command string is empty".to_string(),
                    ));
                }
                ()
            }
            Command::SubCommand(command_strings) => {
                if command_strings.len() < 2 {
                    return Err(ParserError::InvalidCliSpec(
                        "Sub command strings count must be at least 2".to_string(),
                    ));
                }

                for command_string in command_strings {
                    if command_string.is_empty() {
                        return Err(ParserError::InvalidCliSpec(
                            "Sub command string is empty".to_string(),
                        ));
                    }
                }
            }
        };
    }

    Ok(())
}

fn insert_argument_value(cli_parsed: &mut CliParsed, name: &str, value: &str) {
    cli_parsed.arguments.insert(name.to_string());

    match cli_parsed.argument_values.remove(name) {
        Some(mut values) => {
            values.push(value.to_string());
            cli_parsed.argument_values.insert(name.to_string(), values);
        }
        None => {
            cli_parsed
                .argument_values
                .insert(name.to_string(), vec![value.to_string()]);
        }
    }
}