Add structopt for arg parsing
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
27
src/main.rs
27
src/main.rs
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user