From be31f093c07edc10a00ce7ab144ad7dc97c7eb4a Mon Sep 17 00:00:00 2001 From: generic-github-user <40661852+generic-github-user@users.noreply.github.com> Date: Mon, 26 Sep 2022 09:25:27 -0400 Subject: [PATCH] parse command-line arguments --- shell-1/Cargo.lock | 154 ++++++++++++++++++++++++++++++++++++++++++ shell-1/Cargo.toml | 1 + shell-1/src/main.rs | 23 ++++++- shell-1/src/parser.rs | 2 +- 4 files changed, 177 insertions(+), 3 deletions(-) diff --git a/shell-1/Cargo.lock b/shell-1/Cargo.lock index f284930d..a268b457 100644 --- a/shell-1/Cargo.lock +++ b/shell-1/Cargo.lock @@ -11,12 +11,29 @@ dependencies = [ "libc", ] +[[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 = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bumpalo" version = "3.11.0" @@ -44,12 +61,72 @@ dependencies = [ "winapi", ] +[[package]] +name = "clap" +version = "3.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "core-foundation-sys" version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "iana-time-zone" version = "0.1.50" @@ -63,6 +140,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "js-sys" version = "0.3.60" @@ -112,6 +199,36 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.44" @@ -135,8 +252,15 @@ name = "shell-1" version = "0.1.0" dependencies = [ "chrono", + "clap", ] +[[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.100" @@ -148,6 +272,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" + [[package]] name = "time" version = "0.1.44" @@ -165,6 +304,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" @@ -241,6 +386,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/shell-1/Cargo.toml b/shell-1/Cargo.toml index 2ff8985e..d3780260 100644 --- a/shell-1/Cargo.toml +++ b/shell-1/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] chrono = "0.4" +clap = { version = "3.2", features = ["derive"] } diff --git a/shell-1/src/main.rs b/shell-1/src/main.rs index 33ba4bd8..14e3dc32 100644 --- a/shell-1/src/main.rs +++ b/shell-1/src/main.rs @@ -1,7 +1,9 @@ use std::io; use std::io::Write; +use std::env::args; use std::time::SystemTime; use chrono::{DateTime, Utc}; +use clap::Parser; mod lexer; mod parser; @@ -14,10 +16,23 @@ use lexer::lex; use parser::parse; use node::Node; +#[derive(Parser, Debug)] +#[clap(version, about)] +struct Args { + #[clap(short, long, value_parser, default_value="")] + path: String, + + // #[clap(short, long, default_value_t=false)] + #[clap(short, long, action)] + debug: bool +} + fn main() { let mut input = String::new(); // let now = SystemTime::now(); let now = Utc::now(); + let args = Args::parse(); + println!("{:#?}", args); println!("shell-1 | {}", now); loop { print!("~ "); @@ -26,10 +41,14 @@ fn main() { io::stdin().read_line(&mut input).unwrap(); let tokens = lex(input.clone(), false).unwrap(); - println!("{:?}", tokens); + if args.debug { + println!("{:#?}", tokens); + } let tree: Node = parse(tokens, false); - println!("{:?}", tree); + if args.debug { + println!("{:#?}", tree); + } println!("{}", tree.evaluate().unwrap()); } diff --git a/shell-1/src/parser.rs b/shell-1/src/parser.rs index f3d330b0..afc63a61 100644 --- a/shell-1/src/parser.rs +++ b/shell-1/src/parser.rs @@ -43,7 +43,7 @@ pub fn parse(tokens: Vec, verbose: bool) -> Node { "}" => { stack.pop(); }, - _ => todo!() + _ => todo!("{:?}", token.content) } } CharType::Newline | CharType::Whitespace | CharType::None => (),