add command line arguments via clap crate

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-10 18:45:46 +02:00
parent 2f0e22c79c
commit 680ee8adeb
3 changed files with 57 additions and 5 deletions

43
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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(())
}