From e4940247f3540eff10cbb6eac3ee85580a6c55e1 Mon Sep 17 00:00:00 2001 From: David Mak Date: Wed, 6 Sep 2023 16:34:08 +0800 Subject: [PATCH] standalone: Implement command-line parser using clap In preparation for adding more command-line options. --- Cargo.lock | 113 +++++++++++++++++++++++++++++++++++++ nac3standalone/Cargo.toml | 4 ++ nac3standalone/src/main.rs | 19 ++++++- 3 files changed, 133 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 67cd46461..b604a8531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,54 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "ascii-canvas" version = "3.0.0" @@ -94,6 +142,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a13b88d2c62ff462f88e4a121f17a82c1af05693a2f192b5c38d14de73c19f6" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.29", +] + +[[package]] +name = "clap_lex" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "console" version = "0.15.7" @@ -313,6 +407,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.2" @@ -618,6 +718,7 @@ dependencies = [ name = "nac3standalone" version = "0.1.0" dependencies = [ + "clap", "inkwell", "nac3core", "nac3parser", @@ -1079,6 +1180,12 @@ dependencies = [ "precomputed-hash", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.109" @@ -1288,6 +1395,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" diff --git a/nac3standalone/Cargo.toml b/nac3standalone/Cargo.toml index 79a25b370..d6d6ebf26 100644 --- a/nac3standalone/Cargo.toml +++ b/nac3standalone/Cargo.toml @@ -9,6 +9,10 @@ parking_lot = "0.12" nac3parser = { path = "../nac3parser" } nac3core = { path = "../nac3core" } +[dependencies.clap] +version = "4.4" +features = ["derive"] + [dependencies.inkwell] version = "0.2" default-features = false diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index cc50fb638..0faedc856 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -1,3 +1,4 @@ +use clap::Parser; use inkwell::{ memory_buffer::MemoryBuffer, passes::{PassManager, PassManagerBuilder}, @@ -5,7 +6,7 @@ use inkwell::{ OptimizationLevel, }; use parking_lot::{Mutex, RwLock}; -use std::{borrow::Borrow, collections::HashMap, env, fs, path::Path, sync::Arc}; +use std::{borrow::Borrow, collections::HashMap, fs, path::Path, sync::Arc}; use nac3core::{ codegen::{ @@ -30,6 +31,18 @@ use nac3parser::{ mod basic_symbol_resolver; use basic_symbol_resolver::*; +/// Command-line argument parser definition. +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct CommandLineArgs { + /// The name of the input file. + file_name: String, + + /// The number of threads allocated to processing the source file. + #[arg(default_value_t = 1)] + threads: u32, +} + fn handle_typevar_definition( var: &Expr, resolver: &(dyn SymbolResolver + Send + Sync), @@ -155,8 +168,8 @@ fn handle_assignment_pattern( } fn main() { - let file_name = env::args().nth(1).unwrap(); - let threads: u32 = env::args().nth(2).map(|s| str::parse(&s).unwrap()).unwrap_or(1); + let cli = CommandLineArgs::parse(); + let CommandLineArgs { file_name, threads } = cli; Target::initialize_all(&InitializationConfig::default());