From 00000006fadc4550c696939966d63157024ab63b Mon Sep 17 00:00:00 2001 From: masklinn Date: Wed, 15 Nov 2023 17:18:10 +0100 Subject: [PATCH] Add support for a help flag While lucky-commit has a pretty simple interface, because it operates by side-effects it can be safer to check its behaviour. Currently, to get the help it's necessary to be lucky enough to mistakenly give it 2 args, thus failing to match any case. Otherwise, it will panic if called from not-within-a-repository, and silently rewrite the HEAD if invoked from one. --- src/bin.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index 6b483e2..3f88ec5 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -9,6 +9,17 @@ use std::{ process::{self, Command, Stdio}, }; +fn usage() -> ! { + eprintln!( + "\ + Usage: lucky_commit [commit-hash-prefix]\n\ + \n\ + `commit-hash-prefix` must contain hex characters and underscores. An underscore indicates \ + that the hash may have any value in the given position. + " + ); + process::exit(1) +} fn main() -> Result<(), ParseHashSpecErr> { let args = env::args().collect::>(); let maybe_prefix = match args.as_slice() { @@ -16,17 +27,10 @@ fn main() -> Result<(), ParseHashSpecErr> { benchmark::run_benchmark(); process::exit(0) } + [_, arg] if arg == "-h" || arg == "--help" => usage(), [_, prefix] => Some(prefix.as_str()), [_] => None, - _ => { - eprintln!("\ - Usage: lucky_commit [commit-hash-prefix]\n\ - \n\ - `commit-hash-prefix` must contain hex characters and underscores. An underscore indicates \ - that the hash may have any value in the given position. - "); - process::exit(1) - } + _ => usage(), }; let existing_commit = spawn_git(&["cat-file", "commit", "HEAD"], None);