From 3f74cb23cd7301aa4bdca70946cc41b19b7eaf41 Mon Sep 17 00:00:00 2001 From: kumavale Date: Sat, 17 Feb 2024 21:08:52 +0900 Subject: [PATCH] refactor: clap version to 4 from 2 --- Cargo.toml | 2 +- src/cli.rs | 7 ++++--- src/main.rs | 25 +++++++++++++------------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd3e9c4..695ddb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,5 @@ description = "A MIPS-32 simulator" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "4.5.1", features = ["derive"] } rand = "0.8.2" -clap = "2.33.3" diff --git a/src/cli.rs b/src/cli.rs index fd5968a..a521aec 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,12 +1,13 @@ use std::io::{BufRead, BufReader}; use std::fs::File; +use std::path::PathBuf; use super::token::Tokens; use super::token::memory::Memory; use super::lexer::tokenize; use super::parser::parse; -pub fn run(files: &[&str]) { +pub fn run(files: &[PathBuf]) { let mut tokens: Tokens = Tokens::new(); let mut memory = Memory::default(); @@ -14,12 +15,12 @@ pub fn run(files: &[&str]) { for (filename_idx, file) in files.iter().enumerate() { let mut number_of_lines: u32 = 1; let mut reader = BufReader::new(File::open(file).expect("Failed file open")); - tokens.add_file(file); + tokens.add_file(file.to_str().unwrap()); let mut buf = String::new(); while 0 < reader.read_line(&mut buf).unwrap() { if let Err(e) = tokenize(number_of_lines, filename_idx, &buf, &mut tokens) { - eprintln!("{}:{}: {}", file, number_of_lines, e); + eprintln!("{}:{}: {}", file.display(), number_of_lines, e); std::process::exit(1); } number_of_lines += 1; diff --git a/src/main.rs b/src/main.rs index 0892a72..3802b07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,30 @@ -#[macro_use] -extern crate clap; - mod cli; mod lexer; mod parser; mod repl; mod token; -use clap::{Arg, App}; +use std::path::PathBuf; +use clap::Parser; + +#[derive(Parser)] +#[command(version, about)] +struct Cli { + /// Optional name to operate on + #[arg(short, long, value_name = "FILE")] + files: Vec, +} fn main() { - let matches = App::new("mipsi") - .version(crate_version!()) - .about(crate_description!()) - .arg(Arg::with_name("file").multiple(true)) - .get_matches(); + let cli = Cli::parse(); // REPL - if matches.values_of("file").is_none() { + if cli.files.is_empty() { repl::run(); return; } // CLI - let files: Vec<&str> = matches.values_of("file").unwrap().collect(); - cli::run(&files); + cli::run(&cli.files); }