All checks were successful
ci/woodpecker/push/build Pipeline was successful
- Use is_empty iso len() == 0 - Remove unneeded return type
52 lines
1 KiB
Rust
52 lines
1 KiB
Rust
use clap::Parser;
|
|
use std::fs::File;
|
|
use std::io::{self, BufRead, Write};
|
|
|
|
#[derive(Parser, Debug)]
|
|
#[command(version, about, long_about = None)]
|
|
struct Args {
|
|
/// Script to run
|
|
#[arg(short, long)]
|
|
script: Option<String>,
|
|
}
|
|
|
|
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.is_empty() {
|
|
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 {
|
|
run_file(script)?;
|
|
} else {
|
|
run_prompt()?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|