1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

feat: args handling

This commit is contained in:
Natapat Samutpong 2022-02-10 03:25:19 +07:00
parent 252a8aae33
commit 9e8c9e72dd
2 changed files with 41 additions and 8 deletions

1
example/hello_world.vy Normal file
View file

@ -0,0 +1 @@
(print '(Hello, World!))

View file

@ -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::<Vec<String>>();
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: {} <file>", EXECUTABLE_NAME);
exit(exit_code);
}