leottaro_commands/
lib.rs

1use std::{collections::HashMap, env::args, path::PathBuf, process::exit};
2
3pub struct Inputs {
4    pub name: String,
5    pub arguments: Vec<String>,
6    pub options: HashMap<String, String>,
7    pub contains_help: bool,
8}
9
10impl Inputs {
11    pub fn parse() -> Self {
12        let mut args = args();
13
14        let name = args
15            .next()
16            .unwrap_or_else(|| panic!("Unable to get first arg from Args {:?}", args));
17        let mut arguments = Vec::new();
18        let mut options = HashMap::new();
19        let mut contains_help = false;
20
21        for arg in args {
22            if arg == "--help" || arg == "-h" {
23                contains_help = true;
24                continue;
25            }
26
27            let arg_chars = arg.chars();
28            if arg.starts_with("--") {
29                let arg = arg_chars.skip(2).collect::<String>();
30                let splits: Vec<&str> = arg.split('=').collect();
31                if splits.len() > 2 {
32                    eprintln!(
33                        "Error while parsing options: \nSyntax error in arg {}: splits = {:?}",
34                        arg, splits
35                    );
36                    exit(1);
37                }
38                options.insert(
39                    splits[0].to_string(),
40                    splits.get(1).unwrap_or(&"").to_string(),
41                );
42            } else if arg.starts_with("-") {
43                options.insert(arg_chars.skip(1).collect(), "".to_string());
44            } else {
45                arguments.push(arg);
46            }
47        }
48
49        Self {
50            name,
51            arguments,
52            options,
53            contains_help,
54        }
55    }
56}
57
58impl std::fmt::Display for Inputs {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(
61            f,
62            "Name: {}\nArguments: {:?}\nOptions: {:?}\nContains Help: {}",
63            self.name, self.arguments, self.options, self.contains_help
64        )
65    }
66}
67
68pub fn match_path(path1: &PathBuf, path2: &PathBuf) -> bool {
69    if path1.eq(path2) {
70        return true;
71    }
72
73    if let (Some(path1), Some(path2)) = (path1.to_str(), path2.to_str()) {
74        if let Ok(pattern) = glob::Pattern::new(path1) {
75            return pattern.matches(path2);
76        }
77    }
78
79    false
80}