From 9e8c9e72dd8da858888434b4aaf77d61eb8d166d Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 10 Feb 2022 03:25:19 +0700 Subject: [PATCH] feat: args handling --- example/hello_world.vy | 1 + src/main.rs | 48 +++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 example/hello_world.vy diff --git a/example/hello_world.vy b/example/hello_world.vy new file mode 100644 index 0000000..b88d905 --- /dev/null +++ b/example/hello_world.vy @@ -0,0 +1 @@ +(print '(Hello, World!)) \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c67ba18..cfe5c26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,45 @@ +use std::{process::exit, fs::read_to_string}; + pub mod parser; -fn main() { - let src = r#" - (print "Hello, World!") - (print '(hello, world!)) - "#; +const EXECUTABLE_NAME: &str = env!("CARGO_PKG_NAME"); - let parsed = parser::parse(src); - for result in parsed { - println!("{:?}", result); +fn main() { + let args = std::env::args().collect::>(); + let mut args_index: usize = 0; + match args.len() { + // No argument provided + 1 => { display_help(1); }, + _ => { + while args.len() > args_index { + let arg: &str = &args[args_index]; + match arg { + "-h" | "--help" => { display_help(0); }, + "-v" | "--version" => { + println!("{} version {}", EXECUTABLE_NAME, env!("CARGO_PKG_VERSION")); + exit(0); + }, + "-c" | "--compile" => { + args_index += 1; + if args_index < args.len() { + let file_path: &str = &args[args_index]; + let file_content: String = read_to_string(file_path).unwrap(); + let ast = parser::parse(&file_content); + for node in ast { + println!("{:?}", node); + } + } else { + display_help(1); + } + } + _ => { args_index += 1; } + } + } + } } } + +fn display_help(exit_code: i32) { + println!("Usage: {} ", EXECUTABLE_NAME); + exit(exit_code); +} \ No newline at end of file