Run a REPL if no arguments are given
This commit is contained in:
parent
e8e81b4564
commit
254ac20186
|
@ -16,7 +16,7 @@ together if the units are compatible:
|
|||
metrify 1 acre 15 square feet
|
||||
|
||||
You can also invoke metrify without arguments, in which case it will give you a
|
||||
prompt to type the expression into.
|
||||
prompt to type the expression into. Press ^D to exit.
|
||||
|
||||
Result display
|
||||
--------------
|
||||
|
|
52
src/main.rs
52
src/main.rs
|
@ -9,31 +9,43 @@ fn main() {
|
|||
let name = args[0].clone();
|
||||
let args = args[1..].join(" ");
|
||||
|
||||
let mut input = args;
|
||||
|
||||
if input.len() == 0 {
|
||||
print!("> ");
|
||||
match io::stdout().flush() {
|
||||
Ok(_) => {}
|
||||
if args.len() == 0 {
|
||||
loop {
|
||||
print!("> ");
|
||||
match io::stdout().flush() {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
eprintln!("{name}: Error: {err}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
let mut input = String::new();
|
||||
match io::stdin().read_line(&mut input) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
eprintln!("{name}: Error: {err}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
if input.len() == 0 {
|
||||
println!(); // Add a newline if user pressed ^D
|
||||
break;
|
||||
}
|
||||
match run(&input) {
|
||||
Ok(str) => println!("{str}"),
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match run(&args) {
|
||||
Ok(str) => println!("{str}"),
|
||||
Err(err) => {
|
||||
eprintln!("{name}: Error: {err}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
match io::stdin().read_line(&mut input) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
eprintln!("{name}: Error: {err}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match run(&input) {
|
||||
Ok(str) => println!("{str}"),
|
||||
Err(err) => {
|
||||
eprintln!("{name}: Error: {err}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue