/** * Persists shell state. If fails, calls {@link ru.fizteh.fivt.students.fedorov_andrew * .databaselibrary.shell.ShellState#prepareToExit(int)} with non zero exit code. */ private void persistSafelyAndPrepareToExit() throws ExitRequest { try { shellState.persist(); shellState.prepareToExit(0); } catch (ExitRequest request) { throw request; } catch (Exception exc) { Log.log(Shell.class, exc, "Failed to persist shell state"); shellState.prepareToExit(1); } }
/** * Execute commands from command line arguments. Note that command line arguments are first * concatenated into a single line then split and parsed. * * @param args Array of commands. If an error happens during execution of one of the commands in * the sequence, next commands will not be executed. * @return Exit code. 0 means normal status, anything else - abnormal termination (error). */ public int run(String[] args) throws TerminalException { try { interactive = false; try { List<String[]> commands = splitCommandsString(String.join(" ", args)); for (String[] command : commands) { execute(command); } } catch (TerminalException exc) { // Exception already handled. shellState.prepareToExit(1); } catch (ParseException exc) { Utility.handleError("Cannot parse command arguments: " + exc.getMessage(), exc, true); } persistSafelyAndPrepareToExit(); } catch (ExitRequest request) { return request.getCode(); } // If all contracts are honoured, this line is unreachable. throw new AssertionError("No exit request performed"); }