Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Commit

Permalink
Refactored bakervm cli to use StructOpt
Browse files Browse the repository at this point in the history
  • Loading branch information
sphinxc0re committed Mar 1, 2018
1 parent 3bc7ac6 commit 5c9a63c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
53 changes: 23 additions & 30 deletions bakervm/main.rs
Original file line number Original file line Diff line number Diff line change
@@ -1,26 +1,28 @@
extern crate rmp_serde; extern crate core;
extern crate clap;
#[macro_use] #[macro_use]
extern crate error_chain; extern crate error_chain;
extern crate core;
extern crate sdl2;
extern crate rand; extern crate rand;
extern crate rmp_serde;
extern crate sdl2;
extern crate serde;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate serde; #[macro_use]
extern crate structopt;


mod vm; mod vm;
mod io; mod io;


use clap::{App, Arg};
use core::Program; use core::Program;
use core::error::*; use core::error::*;
use core::typedef::*; use core::typedef::*;
use rmp_serde::Deserializer; use rmp_serde::Deserializer;
use serde::Deserialize; use serde::Deserialize;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::sync::{Arc, Barrier, mpsc}; use std::path::PathBuf;
use std::sync::{mpsc, Arc, Barrier};
use structopt::StructOpt;


fn main() { fn main() {
if let Err(ref e) = run() { if let Err(ref e) = run() {
Expand All @@ -40,29 +42,22 @@ fn main() {
} }
} }


#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(parse(from_os_str), help = "Sets the image file to use. Uses a standard image if nothing is specified.")]
input: Option<PathBuf>,
#[structopt(help = "Sets the scale for the display. If not specified, the default scale set by the image will be used.")]
scale: Option<f64>,
}

fn run() -> Result<()> { fn run() -> Result<()> {
let matches = App::new("bakerVM") let opt = Opt::from_args();
.version(env!("CARGO_PKG_VERSION"))
.author("Julian Laubstein <contact@julianlaubstein.de>") let program: Program = if let Some(input) = opt.input {
.about("A virtual machine for building and running retro games")
.arg(
Arg::with_name("input")
.index(1)
.help("Sets the image file to use. Uses a standard image if nothing is specified.")
)
.arg(
Arg::with_name("scale")
.short("s")
.long("scale")
.takes_value(true)
.value_name("SCALE")
.help("Sets the scale for the display. If not specified, the default scale set by the image will be used."))
.get_matches();

let program: Program = if let Some(input) = matches.value_of("input") {
let mut file = File::open(input).chain_err(|| "unable to open file")?; let mut file = File::open(input).chain_err(|| "unable to open file")?;
let mut buf: ImageData = ImageData::new(); let mut buf: ImageData = ImageData::new();
file.read_to_end(&mut buf).chain_err(|| "unable to read from file")?; file.read_to_end(&mut buf)
.chain_err(|| "unable to read from file")?;


let mut de = Deserializer::new(&buf[..]); let mut de = Deserializer::new(&buf[..]);


Expand All @@ -77,9 +72,7 @@ fn run() -> Result<()> {


let mut config = program.config.clone(); let mut config = program.config.clone();


if let Some(scale) = matches.value_of("scale") { config.display.default_scale = opt.scale.unwrap_or(core::DEFAULT_SCALE);
config.display.default_scale = scale.parse().chain_err(|| "unable to parse scale")?;
}


if config.display.default_scale < 1.0 { if config.display.default_scale < 1.0 {
bail!("Display scale can't be less than 1"); bail!("Display scale can't be less than 1");
Expand Down
4 changes: 3 additions & 1 deletion core/config.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


use typedef::*; use typedef::*;


pub const DEFAULT_SCALE: f64 = 4.0;

#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DisplayConfig { pub struct DisplayConfig {
#[serde(default)] #[serde(default)]
Expand All @@ -15,7 +17,7 @@ impl Default for DisplayConfig {
fn default() -> Self { fn default() -> Self {
DisplayConfig { DisplayConfig {
resolution: Default::default(), resolution: Default::default(),
default_scale: 4.0, default_scale: DEFAULT_SCALE,
hide_cursor: true, hide_cursor: true,
} }
} }
Expand Down
1 change: 0 additions & 1 deletion hudson/main.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ mod basm;
mod beast; mod beast;
mod mnemonic; mod mnemonic;


// use clap::{App, AppSettings, Arg, SubCommand};
use commands::{Lang, PackingType}; use commands::{Lang, PackingType};
use core::error::*; use core::error::*;
use std::path::PathBuf; use std::path::PathBuf;
Expand Down

0 comments on commit 5c9a63c

Please sign in to comment.