src : code de sortie du programme = dernier code de sortie

This commit is contained in:
Ahurac 2024-04-10 10:02:24 +02:00
parent 802e97387a
commit ad73edfb2f
2 changed files with 15 additions and 12 deletions

View file

@ -7,9 +7,13 @@ pub trait Command {
} }
impl ExitCode { impl ExitCode {
fn new(exit_code: u8) -> Self { pub fn new(exit_code: u8) -> Self {
Self { exit_code } Self { exit_code }
} }
pub fn get(&self) -> u8 {
self.exit_code
}
} }
pub struct UnixProgram { pub struct UnixProgram {
@ -38,15 +42,12 @@ impl Command for UnixProgram {
let handle = command.spawn(); let handle = command.spawn();
if handle.is_ok() { if handle.is_ok() {
let exit_code = match handle let exit_code = handle
.unwrap() .unwrap()
.wait() .wait()
.expect("error waiting for the child") .expect("error waiting for the child")
.code() .code()
{ .unwrap_or(1);
Some(code) => code,
None => 1,
};
let exit_code = match u8::try_from(exit_code) { let exit_code = match u8::try_from(exit_code) {
Ok(code) => ExitCode::new(code), Ok(code) => ExitCode::new(code),

View file

@ -1,13 +1,15 @@
use crate::command::Command; use crate::command::{Command, ExitCode, UnixProgram};
use crate::command::UnixProgram;
use crate::interface::{get_user_input, print_prompt}; use crate::interface::{get_user_input, print_prompt};
fn exit() { fn exit(code: &ExitCode) {
let code = i32::from(code.get());
println!("exit"); println!("exit");
std::process::exit(0); std::process::exit(code);
} }
pub fn run() { pub fn run() {
let mut current_exit_code = ExitCode::new(0);
loop { loop {
print_prompt(); print_prompt();
@ -22,11 +24,11 @@ pub fn run() {
.collect(); .collect();
if !argv.is_empty() { if !argv.is_empty() {
UnixProgram::new().argv(argv).spawn(); current_exit_code = UnixProgram::new().argv(argv).spawn();
} }
} else { } else {
println!(); println!();
exit(); exit(&current_exit_code);
} }
} }
} }