Add structopt for arg parsing

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-02-25 14:19:12 -05:00
parent bc4f59ecad
commit 07abfc96b5
3 changed files with 120 additions and 13 deletions

View File

@@ -3,21 +3,21 @@
mod common;
mod vm;
use std::{env, fs, io, process};
use structopt::StructOpt;
use std::{fs, io, process, path::{Path, PathBuf}};
#[derive(StructOpt, Debug)]
struct Options {
// maybe some other options:
// * debug
#[structopt(name = "FILE", parse(from_os_str))]
input: PathBuf,
}
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn get_input_string() -> io::Result<String> {
let args: Vec<String> = env::args().collect();
if args.len() <= 1 {
println!(
"usage: {} file.asm",
args.get(0).map(String::as_str).unwrap_or("rasp")
);
process::exit(1);
} else {
fs::read_to_string(&args[1])
}
fn get_input_string(path: impl AsRef<Path>) -> io::Result<String> {
fs::read_to_string(path.as_ref())
}
fn main() -> Result<()> {
@@ -28,7 +28,8 @@ fn main() -> Result<()> {
syn::{lexer, parser},
}
};
let text = get_input_string()?;
let opt = Options::from_args();
let text = get_input_string(&opt.input)?;
let lexerdef = lexer::lexerdef();
let lexer = lexerdef.lexer(&text);
let (res, errors) = parser::parse(&lexer);