summaryrefslogtreecommitdiff
path: root/oryxc/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'oryxc/src/main.rs')
-rw-r--r--oryxc/src/main.rs124
1 files changed, 50 insertions, 74 deletions
diff --git a/oryxc/src/main.rs b/oryxc/src/main.rs
index 7a0b0a8..7320c19 100644
--- a/oryxc/src/main.rs
+++ b/oryxc/src/main.rs
@@ -7,101 +7,77 @@ mod parser;
mod size;
mod unicode;
-use std::borrow::Cow;
use std::ffi::OsString;
-use std::{
- env,
- process,
- thread,
-};
+use std::thread;
-use lexopt;
+use clap::{
+ CommandFactory,
+ FromArgMatches,
+ Parser,
+};
#[derive(Clone, Copy, Default)]
pub struct Flags {
pub debug_lexer: bool,
pub debug_parser: bool,
- pub help: bool,
pub threads: usize,
pub error_style: errors::ErrorStyle,
}
-impl Flags {
- fn parse() -> Result<(Flags, Vec<OsString>), lexopt::Error> {
- use lexopt::prelude::*;
+#[derive(Parser)]
+struct Args {
+ #[arg(short = 'l', long)]
+ debug_lexer: bool,
+
+ #[arg(short = 'p', long)]
+ debug_parser: bool,
+
+ #[arg(short = 's', long, default_value = "standard")]
+ error_style: errors::ErrorStyle,
+
+ #[arg(short = 't', long)]
+ threads: Option<usize>,
- 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);
+ files: Vec<OsString>,
+}
- while let Some(arg) = parser.next()? {
- match arg {
- Short('h') | Long("help") => flags.help = true,
- 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()),
- }
- }
+fn main() {
+ let args = Args::from_arg_matches(
+ &Args::command().override_usage(usage()).get_matches(),
+ )
+ .unwrap_or_else(|e| e.exit());
- if flags.threads == 0 {
- flags.threads = thread::available_parallelism().map_or_else(
- |e| {
- warn!(e, "failed to get thread count");
- 1
- },
- |x| x.get(),
- );
- }
+ 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(),
+ )
+ });
- return Ok((flags, rest));
+ 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 _ = errors::ERROR_STYLE.set(flags.error_style);
+ compiler::start(args.files, flags);
}
-fn usage() {
- eprintln!(
+fn usage() -> String {
+ format!(
concat!(
"Usage: {0} [-lp] [-s oneline|standard] [-t threads]\n",
" {0} -h",
),
errors::progname().display()
- );
-}
-
-fn main() {
- let (flags, rest) = match Flags::parse() {
- Ok(v) => v,
- Err(e) => {
- warn!(e);
- usage();
- process::exit(1);
- },
- };
-
- if flags.help {
- usage();
- process::exit(0);
- }
-
- let _ = errors::ERROR_STYLE.set(flags.error_style);
- compiler::start(rest, flags);
+ )
}