args better typed

This commit is contained in:
Alex Janka 2023-10-15 10:05:36 +11:00
parent 4128a93f54
commit 5078712142
2 changed files with 30 additions and 23 deletions

View file

@ -2,7 +2,7 @@
#[cfg(feature = "camera")]
use camera::Webcam;
use clap::{ArgGroup, Parser};
use clap::{ArgGroup, Parser, ValueEnum};
use debug::Debugger;
use gb_emu_lib::{
config::ConfigManager,
@ -32,30 +32,43 @@ compile_error!("select only one rendering backend!");
#[cfg(all(not(feature = "vulkan"), not(feature = "pixels")))]
compile_error!("select one rendering backend!");
#[derive(ValueEnum, Debug, Clone, Copy)]
enum SerialTargetOption {
None,
Ascii,
Hex,
}
impl From<SerialTargetOption> for SerialTarget {
fn from(value: SerialTargetOption) -> Self {
match value {
SerialTargetOption::None => Self::None,
SerialTargetOption::Ascii => Self::Stdout(StdoutType::Ascii),
SerialTargetOption::Hex => Self::Stdout(StdoutType::Hex),
}
}
}
/// Gameboy (DMG/CGB) emulator
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
#[command(group(ArgGroup::new("saves").args(["save","no_save"])))]
#[command(group(ArgGroup::new("link").args(["ascii","hex"])))]
struct Args {
/// Path to ROM
rom: String,
#[arg(value_hint = clap::ValueHint::FilePath)]
rom: PathBuf,
/// Save path
#[arg(long)]
save: Option<String>,
#[arg(long, value_hint = clap::ValueHint::FilePath)]
save: Option<PathBuf>,
/// Skip save file
#[arg(long)]
no_save: bool,
/// Output link port to stdout as ASCII
#[arg(long)]
ascii: bool,
/// Output link port to stdout as hex values
#[arg(long)]
hex: bool,
/// Output link port to stdout as either ASCII or hex
#[arg(long, value_enum, default_value_t = SerialTargetOption::None)]
serial: SerialTargetOption,
/// Show tile window
#[arg(long)]
@ -131,7 +144,7 @@ fn run(args: Args) -> ! {
let config = config_manager.load_or_create_base_config();
let standalone_config: StandaloneConfig = config_manager.load_or_create_config("standalone");
let rom_file = RomFile::Path(PathBuf::from(args.rom));
let rom_file = RomFile::Path(args.rom);
let (rom, camera) = rom_file
.load(
@ -203,13 +216,7 @@ fn run(args: Args) -> ! {
let options =
EmulatorOptions::new_with_config(config, config_manager.dir(), window, rom, output)
.with_serial_target(if args.ascii {
SerialTarget::Stdout(StdoutType::Ascii)
} else if args.hex {
SerialTarget::Stdout(StdoutType::Hex)
} else {
SerialTarget::None
})
.with_serial_target(args.serial.into())
.with_no_save(args.no_save)
.with_tile_window(tile_window)
.with_layer_window(layer_window);

View file

@ -45,7 +45,7 @@ impl RomFile {
match self {
RomFile::Path(path) => {
let save_location = match save {
SramType::File(path) => Some(SaveDataLocation::File(PathBuf::from(path))),
SramType::File(path) => Some(SaveDataLocation::File(path)),
SramType::RawBuffer(buf) => Some(SaveDataLocation::Raw(buf)),
SramType::Auto => Some(SaveDataLocation::File(path.with_extension("sav"))),
SramType::None => None,
@ -55,7 +55,7 @@ impl RomFile {
}
RomFile::Raw(data) => {
let save_location = match save {
SramType::File(path) => Some(SaveDataLocation::File(PathBuf::from(path))),
SramType::File(path) => Some(SaveDataLocation::File(path)),
SramType::RawBuffer(buf) => Some(SaveDataLocation::Raw(buf)),
SramType::Auto => None,
SramType::None => None,
@ -213,7 +213,7 @@ where
#[derive(Debug)]
pub enum SramType {
File(String),
File(PathBuf),
RawBuffer(Arc<RwLock<Vec<u8>>>),
Auto,
None,