diff options
Diffstat (limited to 'oryxc')
| -rw-r--r-- | oryxc/Cargo.toml | 2 | ||||
| -rw-r--r-- | oryxc/src/errors.rs | 3 | ||||
| -rw-r--r-- | oryxc/src/main.rs | 106 |
3 files changed, 63 insertions, 48 deletions
diff --git a/oryxc/Cargo.toml b/oryxc/Cargo.toml index 4230086..23ae95b 100644 --- a/oryxc/Cargo.toml +++ b/oryxc/Cargo.toml @@ -5,9 +5,9 @@ edition = "2024" [dependencies] boxcar = "0.2.14" -clap = { version = "4", features = ["derive"] } crossbeam-deque = "0.8.6" dashmap = "6.1.0" +lexopt = "0.3.2" phf = { version = "0.13.1", features = ["macros"] } soa-rs = "0.9.1" unicode-normalization = "0.1.25" diff --git a/oryxc/src/errors.rs b/oryxc/src/errors.rs index 4ad88d6..2c8f268 100644 --- a/oryxc/src/errors.rs +++ b/oryxc/src/errors.rs @@ -28,9 +28,8 @@ use crate::unicode; const TAB_AS_SPACES: &'static str = " "; const TABSIZE: usize = TAB_AS_SPACES.len(); -#[derive(Clone, Copy, Default, Eq, PartialEq, clap::ValueEnum)] +#[derive(Clone, Copy, Default, Eq, PartialEq)] pub enum ErrorStyle { - #[value(name = "oneline")] OneLine, #[default] Standard, diff --git a/oryxc/src/main.rs b/oryxc/src/main.rs index 8d38ab1..d46648f 100644 --- a/oryxc/src/main.rs +++ b/oryxc/src/main.rs @@ -10,77 +10,93 @@ mod size; mod unicode; mod unistr; +use std::borrow::Cow; +use std::env; use std::ffi::OsString; +use std::process; use std::thread; -use clap::{ - CommandFactory, - FromArgMatches, - Parser, -}; +use lexopt; #[derive(Clone, Copy, Default)] pub struct Flags { pub debug_lexer: bool, pub debug_parser: bool, - pub threads: usize, pub error_style: errors::ErrorStyle, + pub threads: usize, } -#[derive(Parser)] -struct Args { - #[arg(long)] - debug_lexer: bool, +impl Flags { + fn parse() -> Result<(Flags, Vec<OsString>), lexopt::Error> { + use lexopt::prelude::*; - #[arg(long)] - debug_parser: bool, + let mut rest = Vec::with_capacity(env::args().len()); + let mut flags = Flags::default(); + let mut parser = lexopt::Parser::from_env(); + parser.set_short_equals(false); - #[arg(short = 's', long, default_value = "standard")] - error_style: errors::ErrorStyle, + while let Some(arg) = parser.next()? { + match arg { + Short('h') | Long("help") => usage(0), + Short('l') | Long("debug-lexer") => flags.debug_lexer = true, + Short('p') | Long("debug-parser") => flags.debug_parser = true, + Short('s') | Long("error-style") => { + flags.error_style = match parser.value()?.to_string_lossy() + { + Cow::Borrowed("oneline") => errors::ErrorStyle::OneLine, + Cow::Borrowed("standard") => { + errors::ErrorStyle::Standard + }, + s => Err(format!( + "{s}: invalid value for -s/--error-style" + ))?, + }; + }, + Short('t') | Long("threads") => { + flags.threads = parser.value()?.parse()?; + if flags.threads == 0 { + err!("thread count must be greater than 0"); + } + }, + Value(v) => rest.push(v), + _ => return Err(arg.unexpected()), + } + } - #[arg(short = 't', long)] - threads: Option<usize>, + if flags.threads == 0 { + flags.threads = thread::available_parallelism().map_or_else( + |e| { + warn!(e, "failed to get thread count"); + 1 + }, + |x| x.get(), + ); + } - files: Vec<OsString>, + return Ok((flags, rest)); + } } fn main() { - let args = Args::from_arg_matches( - &Args::command().override_usage(usage()).get_matches(), - ) - .unwrap_or_else(|e| e.exit()); - - let threads = args.threads.unwrap_or_else(|| { - thread::available_parallelism().map_or_else( - |e| { - warn!(e, "failed to get thread count"); - 1 - }, - |x| x.get(), - ) - }); - - if threads == 0 { - err!("thread count must be greater than 0"); - } - - let flags = Flags { - debug_lexer: args.debug_lexer, - debug_parser: args.debug_parser, - threads, - error_style: args.error_style, + let (flags, rest) = match Flags::parse() { + Ok(v) => v, + Err(e) => { + warn!(e); + usage(1); + }, }; let _ = errors::ERROR_STYLE.set(flags.error_style); - compiler::start(args.files, flags); + compiler::start(rest, flags); } -fn usage() -> String { - format!( +fn usage(n: i32) -> ! { + eprintln!( concat!( "Usage: {0} [-s oneline|standard] [-t threads]\n", " {0} -h", ), errors::progname().display() - ) + ); + process::exit(n); } |