summaryrefslogtreecommitdiff
path: root/oryxc/src/main.rs
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2026-03-30 01:35:09 +0200
committerThomas Voss <mail@thomasvoss.com> 2026-03-30 01:35:09 +0200
commit15ddca0982d2f7fb5a46894733573d3040a71bc7 (patch)
tree64ad24cc5a4e6d9349e58efed22a9ca9b72e9ff7 /oryxc/src/main.rs
parentdb11ea02d777a33fedb6af4ee056e85f52fbb008 (diff)
Clap my beloved
Diffstat (limited to 'oryxc/src/main.rs')
-rw-r--r--oryxc/src/main.rs106
1 files changed, 61 insertions, 45 deletions
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);
}