More consistent exit handling

This commit is contained in:
Andrew Morris
2023-11-05 11:37:49 +10:00
parent 81c86e1728
commit 805ae75a09
6 changed files with 43 additions and 34 deletions

View File

@@ -1,12 +1,10 @@
use std::process::exit;
use valuescript_compiler::{assemble, parse_module};
use crate::exit_command_failed::exit_command_failed;
pub fn assemble_command(args: &Vec<String>) {
if args.len() != 3 {
println!("ERROR: Unrecognized command\n");
show_help();
exit(1);
exit_command_failed(args, None, "vstc assemble --help");
}
if args[2] == "-h" || args[2] == "--help" {

View File

@@ -1,7 +1,7 @@
use std::fs::File;
use std::io::Write;
use std::process::exit;
use crate::exit_command_failed::exit_command_failed;
use crate::resolve_entry_path::resolve_entry_path;
use super::handle_diagnostics_cli::handle_diagnostics_cli;
@@ -10,9 +10,15 @@ use valuescript_compiler::compile;
pub fn compile_command(args: &Vec<String>) {
if args.len() != 3 {
println!("ERROR: Unrecognized command\n");
show_help();
exit(1);
exit_command_failed(args, None, "vstc compile --help");
}
match args.get(2).map(String::as_str) {
Some("--help") | Some("-h") => {
show_help();
return;
}
_ => {}
}
let resolved_entry_path = resolve_entry_path(&args[2]);

View File

@@ -8,6 +8,7 @@ use valuescript_vm::{
};
use crate::{
exit_command_failed::exit_command_failed,
handle_diagnostics_cli::handle_diagnostics_cli,
parse_command_line::parse_command_line,
to_bytecode::{format_from_path, to_bytecode},
@@ -34,9 +35,7 @@ pub fn db_command(args: &[String]) {
let path = match args.get(2) {
Some(path) => path.clone(),
None => {
println!("ERROR: Missing db path\n");
show_help();
exit(1);
exit_command_failed(args, Some("Missing db path"), "vstc db help");
}
};
@@ -53,9 +52,7 @@ pub fn db_command(args: &[String]) {
}
}
println!("ERROR: Unrecognized db command {:?}\n", args);
show_help();
exit(1);
exit_command_failed(args, None, "vstc db help");
}
}
}
@@ -89,9 +86,7 @@ fn db_new(storage: &mut Storage<SledBackend>, args: &[String]) {
let class_file = match args.get(0) {
Some(class_file) => class_file,
None => {
println!("ERROR: Missing class file\n");
show_help();
exit(1);
exit_command_failed(args, Some("Missing class file"), "vstc db help");
}
};
@@ -136,11 +131,7 @@ fn db_new(storage: &mut Storage<SledBackend>, args: &[String]) {
fn db_call(storage: &mut Storage<SledBackend>, args: &[String]) {
let fn_file = match args.get(0) {
Some(fn_file) => fn_file,
None => {
println!("ERROR: Missing function file\n");
show_help();
exit(1);
}
None => exit_command_failed(args, Some("Missing function file"), "vstc db help"),
};
let fn_ = Rc::new(to_bytecode(format_from_path(fn_file), fn_file))
@@ -236,7 +227,10 @@ fn db_interactive(storage: &mut Storage<SledBackend>) {
let args = parse_command_line(&input);
match args.get(0).map(|s| s.as_str()) {
// TODO: help (it's a bit different - code isn't quoted (TODO: quoted should work too))
Some("help") => {
// TODO: help (it's a bit different - code isn't quoted (TODO: quoted should work too))
println!("TODO: help");
}
Some("exit" | "quit") => break,
Some("new") => db_new(storage, args.get(1..).unwrap_or_default()),
Some("call") => db_call(storage, args.get(1..).unwrap_or_default()),
@@ -245,7 +239,8 @@ fn db_interactive(storage: &mut Storage<SledBackend>) {
break 'b db_run_inline(storage, &input);
}
println!("ERROR: Unrecognized db command {:?}\n", args);
println!("Command failed: {:?}", args);
println!(" For help: help");
}
}
}

View File

@@ -0,0 +1,13 @@
use std::process::exit;
pub fn exit_command_failed(args: &[String], context: Option<&str>, help: &str) -> ! {
println!("Command failed: {:?}", args);
if let Some(context) = context {
println!(" {}", context);
}
println!(" For help: {}", help);
exit(1);
}

View File

@@ -2,6 +2,7 @@ mod assemble_command;
mod compile_command;
mod console_command;
mod db_command;
mod exit_command_failed;
mod handle_diagnostics_cli;
mod parse_command_line;
mod resolve_entry_path;
@@ -11,7 +12,6 @@ mod test_inputs;
mod to_bytecode;
use std::env;
use std::process::exit;
use assemble_command::assemble_command;
use compile_command::compile_command;
@@ -20,6 +20,8 @@ use db_command::db_command;
use run_command::run_command;
use termion_test::termion_test;
use crate::exit_command_failed::exit_command_failed;
fn main() {
let args: Vec<String> = env::args().collect();
@@ -31,11 +33,7 @@ fn main() {
Some("db") => db_command(&args),
Some("termion") => termion_test(),
Some("console") => console_command(&args),
_ => {
println!("ERROR: Unrecognized command\n");
show_help();
exit(1);
}
_ => exit_command_failed(&args, None, "vstc help"),
}
}

View File

@@ -4,13 +4,12 @@ use std::rc::Rc;
use valuescript_vm::VirtualMachine;
use valuescript_vm::{vs_value::Val, DecoderMaker};
use crate::exit_command_failed::exit_command_failed;
use crate::to_bytecode::{format_from_path, to_bytecode, RunFormat};
pub fn run_command(args: &Vec<String>) {
if args.len() < 3 {
println!("ERROR: Unrecognized command\n");
show_help();
exit(1);
exit_command_failed(args, None, "vstc run --help");
}
let mut argpos = 2;