separating the cli parsing
This commit is contained in:
parent
8b6d9b5de3
commit
1d04287532
|
@ -4,7 +4,7 @@ use {
|
||||||
parser::{self, Ast},
|
parser::{self, Ast},
|
||||||
},
|
},
|
||||||
alloc::{string::String, vec::Vec},
|
alloc::{string::String, vec::Vec},
|
||||||
core::fmt::Write,
|
core::{fmt::Write, num::NonZeroUsize},
|
||||||
hashbrown::hash_map,
|
hashbrown::hash_map,
|
||||||
std::{
|
std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
|
@ -39,6 +39,33 @@ pub struct Options {
|
||||||
pub extra_threads: usize,
|
pub extra_threads: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Options {
|
||||||
|
pub fn from_args(args: &[&str]) -> std::io::Result<Self> {
|
||||||
|
if args.contains(&"--help") || args.contains(&"-h") {
|
||||||
|
log::error!("Usage: hbc [OPTIONS...] <FILE>");
|
||||||
|
log::error!(include_str!("../command-help.txt"));
|
||||||
|
return Err(std::io::ErrorKind::Other.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Options {
|
||||||
|
fmt: args.contains(&"--fmt"),
|
||||||
|
fmt_stdout: args.contains(&"--fmt-stdout"),
|
||||||
|
dump_asm: args.contains(&"--dump-asm"),
|
||||||
|
extra_threads: args
|
||||||
|
.iter()
|
||||||
|
.position(|&a| a == "--threads")
|
||||||
|
.map(|i| {
|
||||||
|
args[i + 1].parse::<NonZeroUsize>().map_err(|e| {
|
||||||
|
std::io::Error::other(format!("--threads expects non zero integer: {e}"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?
|
||||||
|
.map_or(1, NonZeroUsize::get)
|
||||||
|
- 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn format_to(ast: &parser::Ast, source: &str, out: &mut String) -> core::fmt::Result {
|
pub fn format_to(ast: &parser::Ast, source: &str, out: &mut String) -> core::fmt::Result {
|
||||||
for (i, expr) in ast.exprs().iter().enumerate() {
|
for (i, expr) in ast.exprs().iter().enumerate() {
|
||||||
parser::Formatter::new(&ast.file).fmt(expr, out)?;
|
parser::Formatter::new(&ast.file).fmt(expr, out)?;
|
||||||
|
|
|
@ -1,33 +1,15 @@
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
use std::{io::Write, num::NonZeroUsize};
|
use std::io::Write;
|
||||||
|
|
||||||
log::set_logger(&hblang::Logger).unwrap();
|
log::set_logger(&hblang::Logger).unwrap();
|
||||||
|
|
||||||
let args = std::env::args().collect::<Vec<_>>();
|
let args = std::env::args().collect::<Vec<_>>();
|
||||||
let args = args.iter().map(String::as_str).collect::<Vec<_>>();
|
let args = args.iter().map(String::as_str).collect::<Vec<_>>();
|
||||||
|
let opts = hblang::Options::from_args(&args)?;
|
||||||
if args.contains(&"--help") || args.contains(&"-h") {
|
let file = args.iter().filter(|a| !a.starts_with('-')).nth(1).copied().unwrap_or("main.hb");
|
||||||
println!("Usage: hbc [OPTIONS...] <FILE>");
|
|
||||||
println!(include_str!("../command-help.txt"));
|
|
||||||
return Err(std::io::ErrorKind::Other.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
hblang::run_compiler(
|
hblang::run_compiler(file, opts, &mut out)?;
|
||||||
args.iter().filter(|a| !a.starts_with('-')).nth(1).copied().unwrap_or("main.hb"),
|
|
||||||
hblang::Options {
|
|
||||||
fmt: args.contains(&"--fmt"),
|
|
||||||
fmt_stdout: args.contains(&"--fmt-stdout"),
|
|
||||||
dump_asm: args.contains(&"--dump-asm"),
|
|
||||||
extra_threads: args
|
|
||||||
.iter()
|
|
||||||
.position(|&a| a == "--threads")
|
|
||||||
.map(|i| args[i + 1].parse::<NonZeroUsize>().expect("--threads expects integer"))
|
|
||||||
.map_or(1, NonZeroUsize::get)
|
|
||||||
- 1,
|
|
||||||
},
|
|
||||||
&mut out,
|
|
||||||
)?;
|
|
||||||
std::io::stdout().write_all(&out)
|
std::io::stdout().write_all(&out)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue