From 680ee8adeb02bb94097a6e1f0a38e2f9ae017138 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Tue, 10 Dec 2024 18:45:46 +0200 Subject: [PATCH] add command line arguments via clap crate --- Cargo.lock | 43 ++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 18 ++++++++++++++---- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f394490..1337e03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,12 +1,13 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "Chip8Emulator" version = "0.1.0" dependencies = [ "anyhow", + "clap", "crossterm", "env_logger", "log", @@ -124,6 +125,46 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + [[package]] name = "colorchoice" version = "1.0.3" diff --git a/Cargo.toml b/Cargo.toml index aed2421..4e31099 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ anyhow = "1.0.93" ratatui = "0.29.0" crossterm = "0.28.1" rand = "0.8.5" +clap = { version = "4.5.23", features = ["derive"] } [dev-dependencies] pretty_assertions = "1.4.1" diff --git a/src/main.rs b/src/main.rs index 689c7e3..7e8ab23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,8 @@ use crate::display::RatatuiDisplay; use crate::emulator::Emulator; use crate::input::CrossTermInput; use crate::sound::TerminalSound; +use clap::Parser; use env_logger; -use std::env; -use std::path::PathBuf; mod display; mod emulator; @@ -13,12 +12,23 @@ mod instruction; mod sound; mod stack; +#[derive(Parser, Debug)] +#[command( + version = "1.0", + about = "A Chip8 emulator.", + long_about = "A program which emulates the Chip8 system." +)] +struct CliArgs { + /// The path to the ROM file to emulate. + rom_path: String, +} + fn main() -> Result<(), anyhow::Error> { env_logger::init(); - let rom_path = PathBuf::from(env::args().skip(1).next().expect("rom path not provided.")); + let args = CliArgs::parse(); let mut emulator = Emulator::new(RatatuiDisplay::new(), TerminalSound, CrossTermInput::new()); - emulator.emulate(rom_path.to_str().unwrap())?; + emulator.emulate(args.rom_path)?; Ok(()) }