Skip to content

Commit

Permalink
Add option to suppress trailing newline
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
traviscross committed Nov 22, 2024
1 parent 1449574 commit 0973e0d
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 0973e0d

Please sign in to comment.