Refactor: Migrate to clap for CLI argument parsing
The project now uses the `clap` crate for handling command-line arguments, replacing the previous manual parsing approach. This includes adding `clap` as a dependency and creating a `Cli` struct to define the command-line interface. The `main` function has been updated to parse these arguments and handle file input or direct script evaluation.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
use myc::ast::environment::Environment;
|
||||
use clap::Parser;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "MYC AST Compiler CLI", long_about = None)]
|
||||
struct Cli {
|
||||
/// The script file to run
|
||||
#[arg(value_name = "FILE")]
|
||||
file: Option<PathBuf>,
|
||||
|
||||
/// Run a script string directly
|
||||
#[arg(short, long)]
|
||||
eval: Option<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let env = Environment::new();
|
||||
|
||||
if let Some(script_str) = cli.eval {
|
||||
execute(&env, &script_str);
|
||||
} else if let Some(file_path) = cli.file {
|
||||
match fs::read_to_string(&file_path) {
|
||||
Ok(content) => execute(&env, &content),
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file {:?}: {}", file_path, e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("MYC AST Compiler CLI. Use --help for usage.");
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(env: &Environment, source: &str) {
|
||||
match env.run_script(source) {
|
||||
Ok(result) => println!("{}", result),
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub mod ast;
|
||||
|
||||
#[cfg(test)]
|
||||
mod integration_test;
|
||||
+1
-5
@@ -1,9 +1,5 @@
|
||||
use eframe::egui;
|
||||
use crate::ast::environment::Environment;
|
||||
|
||||
pub mod ast;
|
||||
#[cfg(test)]
|
||||
mod integration_test;
|
||||
use myc::ast::environment::Environment;
|
||||
|
||||
fn main() -> eframe::Result {
|
||||
let options = eframe::NativeOptions {
|
||||
|
||||
Reference in New Issue
Block a user