From c41d728d1d837fc345d6d896c1a7c51d82acd8a4 Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Fri, 22 Jun 2018 11:28:02 -0700 Subject: [PATCH] [cli] Part 2: Don't use exit() to terminate the CLI. It's not possible to do meaningful clean-up (such as saving history) if we use exit() to quit. Instead, each handled command returns a boolean requesting exit. I elected not to allow ".exit" when processing commands from the command line; it might be useful to handle accept that. In general, though, REPLs that accept "-c 'commands'" on the command line exit after processing those commands, so I'd rather think more deeply about that model than build in ".exit" with our existing system. --- tools/cli/src/mentat_cli/repl.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/cli/src/mentat_cli/repl.rs b/tools/cli/src/mentat_cli/repl.rs index d68b3d7a..266e0c30 100644 --- a/tools/cli/src/mentat_cli/repl.rs +++ b/tools/cli/src/mentat_cli/repl.rs @@ -9,7 +9,6 @@ // specific language governing permissions and limitations under the License. use std::io::Write; -use std::process; use failure::{ err_msg, @@ -238,7 +237,9 @@ impl Repl { match res { Ok(MetaCommand(cmd)) => { debug!("read command: {:?}", cmd); - self.handle_command(cmd); + if !self.handle_command(cmd) { + break; + } }, Ok(Empty) | Ok(More) => (), @@ -265,7 +266,7 @@ impl Repl { } /// Runs a single command input. - fn handle_command(&mut self, cmd: Command) { + fn handle_command(&mut self, cmd: Command) -> bool { let should_print_times = self.timer_on && cmd.is_timed(); let mut start = PreciseTime::now(); @@ -279,9 +280,8 @@ impl Repl { self.close(); }, Command::Exit => { - self.close(); eprintln!("Exiting…"); - process::exit(0); + return false; }, Command::Help(args) => { self.help_command(args); @@ -378,6 +378,8 @@ impl Repl { eprint!(": "); format_time(start.to(end)); } + + return true; } fn execute_import(&mut self, path: T)