97542b5ca4
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.
46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
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);
|
|
}
|
|
}
|
|
}
|