From 327ec9c62f97d3bd828e3e3963cbda72aaed8067 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 21 Oct 2022 00:16:25 -0400 Subject: [PATCH] Added Clap module for more advanced CLI command/argument handling --- Cargo.toml | 1 + src/main.rs | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index edffc7a..bb2a8ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ license = "BSD 2-Clause" authors = ["Gregory Ballantine "] [dependencies] +clap = { version = "4.0", features = ['derive'] } config = { version = "0.13", features = ['toml'] } log = "0.4" log4rs = "1.1" diff --git a/src/main.rs b/src/main.rs index a544455..2756268 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use clap::{Parser, Subcommand}; use log4rs; use config::Config; use transcoder::repository::Repository; @@ -7,15 +8,39 @@ mod settings; mod transcoder; mod util; +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand)] +enum Commands { + /// displays version info about this program + ToDo {}, +} + fn main() { // initialize the log4rs logger log4rs::init_file("./log4rs.yaml", Default::default()).unwrap(); - // create and initialize our config and repository objects - let c: Config = settings::load_config(); - let r: Repository = Repository::new(&shellexpand::tilde(&c.get_string("transcoder.repository").unwrap())); + // initialize the clap CLI + let cli = Cli::parse(); - // create and start the video transcoder object - let t: Transcoder = Transcoder::new(c, r); - t.start(); + match &cli.command { + // sub-commands will be handled here + Some(_) => todo!(), + + // run the main program without any commands + None => { + // create and initialize our config and repository objects + let c: Config = settings::load_config(); + let r: Repository = Repository::new(&shellexpand::tilde(&c.get_string("transcoder.repository").unwrap())); + + // create and start the video transcoder object + let t: Transcoder = Transcoder::new(c, r); + t.start(); + }, + } }