From 2353c575a9b0e401c20e370f372159cdf1ac785b Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Wed, 16 Jun 2021 11:56:21 +0200 Subject: [PATCH] Add fancy cmd-line parsing --- Cargo.lock | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 39 +++++++++++++--------- 3 files changed, 119 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 705b51e..2034a92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,26 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -30,10 +50,26 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "chip8-rs" version = "0.1.0" dependencies = [ + "clap", "rand", "sdl2", ] +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "getrandom" version = "0.2.2" @@ -45,6 +81,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -127,6 +172,33 @@ dependencies = [ "version-compare", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version-compare" version = "0.0.10" @@ -138,3 +210,25 @@ name = "wasi" version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 21f0de4..9635bc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] rand = "0.8.3" +clap = "2.33.3" [dependencies.sdl2] version = "0.34.5" diff --git a/src/main.rs b/src/main.rs index c4e86c7..4dfec73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,4 @@ -extern crate rand; -extern crate sdl2; - -use std::env; +use clap::{App, Arg}; mod cartridge; mod display; @@ -18,20 +15,32 @@ const SCREEN_WIDTH: usize = 64; const SCREEN_HEIGHT: usize = 32; const SCREEN_SCALE: usize = 20; -fn main() { - let game_file = env::args().nth(1); - match game_file { - Some(_) => println!("Found a cartridge file! Trying to load..."), - None => { - println!("No cartridge file found! Exiting!"); - return; - } - }; +#[derive(Debug)] +enum ChipError { + CartridgeNotFound, +} + +fn main() -> Result<(), ChipError> { + let app = App::new("chip8-rs") + .version("0.1.0") + .author("Marco Thomas ") + .about("A small chip8 emulator, written in Rust") + .arg( + Arg::with_name("cartridge") + .short("c") + .takes_value(true) + .help("A cartridge binary file"), + ) + .get_matches(); + + let game_file = app + .value_of("cartridge") + .ok_or(ChipError::CartridgeNotFound)?; let mut processor = Processor::new(); - // load cartridge file - let cartridge = Cartridge::new(&game_file.unwrap()); + let cartridge = Cartridge::new(&game_file); processor.start(&cartridge.rom); + Ok(()) }