Skip to content

Commit 1bc1eac

Browse files
author
梶塚太智
committed
Update main.rs
it became use CLI framework `clap`
1 parent 181fea4 commit 1bc1eac

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/main.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clap::{App, Arg};
12
use rand::seq::SliceRandom;
23
use regex::Regex;
34
use rodio::{OutputStream, Sink, Source};
@@ -14,31 +15,40 @@ use sys_info::{cpu_num, cpu_speed, hostname, mem_info, os_release, os_type};
1415
mod test;
1516

1617
fn main() {
17-
// Reading command line arguments
18-
let args = env::args().collect::<Vec<_>>();
19-
if args.len() > 2 {
20-
// Open the script file
21-
match get_file_contents(args[1].clone()) {
22-
Ok(code) => {
23-
// Judge execution mode
24-
if args[2].contains("-d") {
25-
let mut executor = Executor::new(Mode::Debug);
26-
executor.evaluate_program(code); // Debug execution
27-
} else {
28-
let mut executor = Executor::new(Mode::Script);
29-
executor.evaluate_program(code); // Script execution
18+
let matches = App::new("Stack")
19+
.version("1.11")
20+
.author("Stack Programming Community")
21+
.about("The powerful script language designed with a stack oriented approach for efficient execution. ")
22+
.arg(Arg::new("script")
23+
.index(1)
24+
.value_name("FILE")
25+
.help("Sets the script file to execution")
26+
.takes_value(true))
27+
.arg(Arg::new("debug")
28+
.short('d')
29+
.long("debug")
30+
.help("Enables debug mode"))
31+
.get_matches();
32+
33+
if let Some(script) = matches.value_of("script") {
34+
if matches.is_present("debug") {
35+
let mut stack = Executor::new(Mode::Debug);
36+
stack.evaluate_program(match get_file_contents(script.to_string()) {
37+
Ok(code) => code,
38+
Err(err) => {
39+
println!("Error! {err}");
40+
return;
3041
}
31-
}
32-
Err(e) => println!("Error! {e}"),
33-
}
34-
} else if args.len() > 1 {
35-
// Open the script file
36-
match get_file_contents(args[1].clone()) {
37-
Ok(code) => {
38-
let mut executor = Executor::new(Mode::Script);
39-
executor.evaluate_program(code); // Default is script execution
40-
}
41-
Err(e) => println!("Error! {e}"),
42+
})
43+
} else {
44+
let mut stack = Executor::new(Mode::Script);
45+
stack.evaluate_program(match get_file_contents(script.to_string()) {
46+
Ok(code) => code,
47+
Err(err) => {
48+
println!("Error! {err}");
49+
return;
50+
}
51+
})
4252
}
4353
} else {
4454
// Show a title

0 commit comments

Comments
 (0)