sysmonk/squire/
parser.rs

1use std::env;
2use std::process::exit;
3
4use crate::constant;
5
6/// Parses and returns the command-line arguments.
7///
8/// # Returns
9///
10/// A String notion of the argument, `env_file` if present.
11pub fn arguments(metadata: &constant::MetaData) -> String {
12    let args: Vec<String> = env::args().collect();
13
14    let mut version = false;
15    let mut env_file = String::new();
16
17    // Loop through the command-line arguments and parse them.
18    let mut i = 1; // Start from the second argument (args[0] is the program name).
19    while i < args.len() {
20        match args[i].as_str() {
21            "-h" | "--help" => {
22                let helper = "SysMonk takes the arguments, --env_file and --version/-v\n\n\
23                --env_file: Custom filename to load the environment variables. Defaults to '.env'\n\
24                --version: Get the package version.\n".to_string();
25                println!("Usage: {} [OPTIONS]\n\n{}", args[0], helper);
26                exit(0)
27            }
28            "-V" | "-v" | "--version" => {
29                version = true;
30            }
31            "--env_file" => {
32                i += 1; // Move to the next argument.
33                if i < args.len() {
34                    env_file = args[i].clone();
35                } else {
36                    println!("--env_file requires a value.");
37                    exit(1)
38                }
39            }
40            _ => {
41                println!("Unknown argument: {}", args[i]);
42                exit(1)
43            }
44        }
45        i += 1;
46    }
47    if version {
48        println!("{} {}", &metadata.pkg_name, &metadata.pkg_version);
49        exit(0)
50    }
51    env_file
52}