Add support to speicfy exit code when error happened

This commit is contained in:
2025-11-03 22:46:45 +08:00
parent 5df59d6345
commit 0e4b308433
4 changed files with 38 additions and 4 deletions

View File

@@ -564,6 +564,12 @@ pub struct Arg {
#[arg(long, global = true)]
/// Compress files in Musica paz archive when packing paz archive.
pub musica_compress: bool,
#[arg(short = 'x', long, default_value_t = 0)]
/// Exit code when some jobs failed
pub exit_code: i32,
#[arg(short = 'X', long)]
/// Exit code when all jobs failed. By default, this is same as exit_code. This can override exit_code when all jobs failed.
pub exit_code_all_failed: Option<i32>,
#[command(subcommand)]
/// Command
pub command: Command,

View File

@@ -2944,7 +2944,9 @@ fn main() {
if op.exists() {
if !op.is_dir() {
eprintln!("Output path is not a directory");
return;
std::process::exit(
argn.exit_code_all_failed.unwrap_or(argn.exit_code),
);
}
} else {
std::fs::create_dir_all(op).unwrap();
@@ -3057,7 +3059,7 @@ fn main() {
if pb.exists() {
if !pb.is_dir() {
eprintln!("Patched path is not a directory");
return;
std::process::exit(argn.exit_code_all_failed.unwrap_or(argn.exit_code));
}
} else {
std::fs::create_dir_all(pb).unwrap();
@@ -3169,7 +3171,9 @@ fn main() {
if op.exists() {
if !op.is_dir() {
eprintln!("Output path is not a directory");
return;
std::process::exit(
argn.exit_code_all_failed.unwrap_or(argn.exit_code),
);
}
} else {
std::fs::create_dir_all(op).unwrap();
@@ -3241,5 +3245,11 @@ fn main() {
}
}
}
eprintln!("{}", std::ops::Deref::deref(&COUNTER));
let counter = std::ops::Deref::deref(&COUNTER);
eprintln!("{}", counter);
if counter.all_failed() {
std::process::exit(argn.exit_code_all_failed.unwrap_or(argn.exit_code));
} else if counter.has_error() {
std::process::exit(argn.exit_code);
}
}

View File

@@ -44,6 +44,19 @@ impl Counter {
ScriptResult::Uncount => {}
}
}
/// Returns true if all jobs failed. (ok == 0 && error > 0)
pub fn all_failed(&self) -> bool {
let ok = self.ok.load(SeqCst);
let error = self.error.load(SeqCst);
ok == 0 && error > 0
}
/// Returns true if there were any errors.
pub fn has_error(&self) -> bool {
let error = self.error.load(SeqCst);
error > 0
}
}
impl std::fmt::Display for Counter {