From 0973e0d9d5fe96470ce62d7f99625778280495e6 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Fri, 22 Nov 2024 04:49:59 +0000 Subject: [PATCH] Add option to suppress trailing newline When generating a UID within a script, it can be helpful to have it returned without the trailing newline. Let's add a `-n` option to enable this. --- xtask/src/main.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 3998eec..2b94c80 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -19,20 +19,21 @@ fn print_uid() { let mut xs = [0u8; UID_LEN]; getrandom::getrandom(&mut xs).unwrap(); xs.iter().for_each(|x| print!("{x:02x}")); - println!(); } -fn print_usage() { +fn print_usage() -> ExitCode { const USAGE: &str = "\ -Usage: cargo xtask [OPTIONS] COMMAND +Usage: cargo xtask [OPTIONS] COMMAND [COMMAND_OPTIONS] Commands: generate-uid, uid Generate an event UID + -n Suppress trailing newline Options: -h Print help "; println!("{USAGE}"); + ExitCode::SUCCESS } fn err_usage() -> ExitCode { @@ -44,10 +45,21 @@ fn main() -> ExitCode { let mut args = std::env::args(); let _arg0 = args.next().unwrap(); let Some(arg1) = args.next() else { return err_usage() }; - let None = args.next() else { return err_usage() }; match &*arg1 { - "-h" => print_usage(), - "generate-uid" | "uid" => print_uid(), + "-h" => return print_usage(), + "generate-uid" | "uid" => { + let mut trailing_nl = true; + match args.next().as_deref() { + Some("-h") => return print_usage(), + Some("-n") => trailing_nl = false, + None => {} + _ => return err_usage(), + }; + print_uid(); + if trailing_nl { + println!(); + } + } _ => return err_usage(), } ExitCode::SUCCESS