diff --git a/README.md b/README.md index 8121c32..60e2061 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ cargo build --release # Build with all features enabled cargo build --release --no-default-features --features=circus # Build with only specific features enabled. See supported types below. ``` +## Exit Codes +By default, msg-tool will always return exit code 0 unless a exit signal is received (such as Ctrl+C). +You can use the `--exit-code` / `-x` option to specify a non-zero exit code when some jobs failed. +If all jobs failed, you can use the `--exit-code-all-failed` / `-X` option to specify a different exit code. + ## Basic Usage ### Extract messages from script files ```bash diff --git a/src/args.rs b/src/args.rs index 1c2205c..7b519f1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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, #[command(subcommand)] /// Command pub command: Command, diff --git a/src/main.rs b/src/main.rs index 5726a48..c06bce5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); + } } diff --git a/src/utils/counter.rs b/src/utils/counter.rs index 77c2d66..4ab0592 100644 --- a/src/utils/counter.rs +++ b/src/utils/counter.rs @@ -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 {