diff --git a/Cargo.lock b/Cargo.lock index f9b452c..feec668 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,26 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -62,6 +82,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "digest" version = "0.8.1" @@ -92,6 +127,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "inkwell" version = "0.1.0" @@ -334,6 +378,12 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "syn" version = "1.0.80" @@ -345,10 +395,20 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "theforce" version = "0.1.0" dependencies = [ + "clap", "inkwell", "pest", "pest_derive", @@ -366,12 +426,24 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + [[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index c895769..6b9dafc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,12 @@ name = "theforce" version = "0.1.0" edition = "2018" +description = "A Star Wars inspired programming language." +homepage = "https://github.com/mirdaki/theforce" +repository = "https://github.com/mirdaki/theforce" +license = "MIT" +keywords = ["star-wars", "programming-language"] +categories = ["compilers", "command-line-utilities"] [features] llvm = ["inkwell"] @@ -10,3 +16,4 @@ llvm = ["inkwell"] inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm10-0", optional = true } pest = "2.1.3" pest_derive = "2.1.0" +clap = "2.33.3" diff --git a/src/cli.rs b/src/cli.rs index 1fc7b22..343f10f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,22 +1,28 @@ -use std::fs; - -pub struct Args { - pub path: std::path::PathBuf, -} +extern crate clap; +use clap::{App, Arg, ArgMatches}; -pub fn parse_arguments() -> Result { - let path = match std::env::args().nth(1) { - Some(path) => path, - None => return Err("Args could not be parsed".to_string()), - }; +use std::fs; - Ok(Args { - path: std::path::PathBuf::from(path), - }) +pub fn parse_arguments() -> ArgMatches<'static> { + App::new(env!("CARGO_PKG_NAME")) + .version(env!("CARGO_PKG_VERSION")) + .author(env!("CARGO_PKG_AUTHORS")) + .about(env!("CARGO_PKG_DESCRIPTION")) + .after_help(&*format!( + "For more information, please visit {}", + env!("CARGO_PKG_HOMEPAGE") + )) + .arg( + Arg::with_name("PATH") + .help("The path to a `.force` file to run.") + .required(true) + .index(1), + ) + .get_matches() } -pub fn read_source(args: Args) -> Result { - match fs::read_to_string(args.path) { +pub fn read_source(args: ArgMatches) -> Result { + match fs::read_to_string(args.value_of("PATH").unwrap()) { Ok(content) => Ok(content), Err(_) => Err("File could not be read".to_string()), } diff --git a/src/main.rs b/src/main.rs index 18bc1bc..ecfa69c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,9 @@ mod parser; mod compiler; fn main() -> Result<(), String> { - let config = cli::parse_arguments()?; + let args = cli::parse_arguments(); - let source = cli::read_source(config)?; + let source = cli::read_source(args)?; let ast = parser::parse(source.as_str()); interpreter::evaluate(ast.unwrap(), io::stdin().lock(), io::stdout())