1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
// Copyright 2015 Axel Rasmussen // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use command::{CommandResult, ExecutableCommand}; use error::Result; use parse_and_execute::parse_and_execute; use parse_and_execute::parse_and_execute_command; use parsed_parameters::get_program_parameters; use std::env; use std::error; use std::process; use std::vec::Vec; const EXIT_SUCCESS: i32 = 0; const EXIT_FAILURE: i32 = 1; fn handle_result<E: error::Error>(r: Result<CommandResult<E>>) -> i32 { match r { Ok(command_result) => { match command_result { Ok(_) => EXIT_SUCCESS, Err(e) => { error!("{}", e); EXIT_FAILURE }, } }, Err(e) => { error!("Internal error: {}", e); EXIT_FAILURE }, } } /// Parses command-line parameters and executes the specified command. /// /// This function exits this process with an appropriate exit code. Like /// `std::process::exit`, because this function never returns and it terminates /// the process, no destructors on the current stack or any other thread's /// stack will be run. The caller should ensure that this function is called /// from the only thread, and that any destructors which need to be run are in /// the stack of the command callback. pub fn main_impl_multiple_commands<E: error::Error>(commands: Vec<ExecutableCommand<E>>) -> ! { process::exit(handle_result(parse_and_execute_command(env::args().next().unwrap().as_ref(), &get_program_parameters(), commands))); } /// Parses command-line parameters and executes the given command. /// /// This function exits this process with an appropriate exit code. Like /// `std::process::exit`, because this function never returns and it terminates /// the process, no destructors on the current stack or any other thread's /// stack will be run. The caller should ensure that this function is called /// from the only thread, and that any destructors which need to be run are in /// the stack of the command callback. pub fn main_impl_single_command<E: error::Error>(command: ExecutableCommand<E>) -> ! { process::exit(handle_result(parse_and_execute(env::args().next().unwrap().as_ref(), &get_program_parameters(), command))); }