autosway 0.3.0

Automation program
Documentation
use crate::common::*;
use clap::{self, arg, ArgAction, Command};

impl State {
    pub fn run_cli(&mut self) {
        let cmd = clap::Command::new("autosway")
            .about("System automation program")
            .arg(arg!(-v --verbose "Set verbose mode").action(ArgAction::SetTrue))
            .subcommand_required(true)
            .subcommand(Command::new("list").about("List knifes"))
            .subcommand(
                Command::new("run")
                    .about("Run a knife")
                    .arg(arg!(<KNIFE> "The knife to run"))
                    .arg_required_else_help(true),
            );

        let matches = cmd.get_matches();

        self.options.verbose = matches.get_flag("verbose");

        match matches.subcommand() {
            Some(("list", _)) => {
                for knife in &self.knifes {
                    if !self.options.verbose {
                        println!("{}: {}", knife.name, knife.path.display());
                    } else {
                        println!("{}: {} - {}", knife.name, knife.path.display(), knife.init);
                    }
                }
            }
            Some(("run", sm)) => {
                let s = match sm.get_one::<String>("KNIFE") {
                    Some(s) => s,
                    None => {
                        log::log!(log::Error, "No argument supplied");
                        std::process::exit(1)
                    }
                };

                if let Err(e) = self.run_knife(s) {
                    log::log!(log::Error, "Failed to run knife: {e}");
                }
            }
            _ => {}
        }
    }
}