This commit is contained in:
parent
da82ece1ca
commit
df54cc7484
1 changed files with 36 additions and 4 deletions
40
src/main.rs
40
src/main.rs
|
|
@ -1,5 +1,6 @@
|
|||
use clap::Parser;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead, Write};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
|
|
@ -9,12 +10,43 @@ struct Args {
|
|||
script: Option<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn run(lines: io::Lines<io::BufReader<File>>) -> () {
|
||||
for line in lines {
|
||||
println!("Line: {}", line.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
fn run_file(script: String) -> Result<(), io::Error> {
|
||||
let file = File::open(script)?;
|
||||
run(io::BufReader::new(file).lines());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_prompt() -> Result<(), io::Error> {
|
||||
loop {
|
||||
print!("> ");
|
||||
io::stdout().flush()?;
|
||||
|
||||
let mut buffer = String::new();
|
||||
io::stdin().read_line(&mut buffer)?;
|
||||
if buffer.len() == 0 {
|
||||
break;
|
||||
}
|
||||
println!("input: '{}', {}", buffer, buffer.len());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Args::parse();
|
||||
|
||||
if let Some(script) = args.script {
|
||||
println!("Run script! {}", script);
|
||||
run_file(script)?;
|
||||
} else {
|
||||
println!("Run Prompt");
|
||||
run_prompt()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue