Skip to content

Commit

Permalink
Add option to print version and exit
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBicPen committed Sep 8, 2024
1 parent 6de5509 commit 557e7dd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn real_main() -> Result<(), Box<dyn Error>> {
&config,
)?),
parse_args::ParseArgsResult::Error(err) => Err(Box::new(err)),
parse_args::ParseArgsResult::Help => Ok(()),
parse_args::ParseArgsResult::PrintAndExit => Ok(()),
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/parse_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ impl Default for OutputConfig {
pub enum ParseArgsResult {
Config(Config),
Error(SimpleError),
Help,
PrintAndExit,
}
enum ParseArgsResultInner {
Success,
Error(SimpleError),
Help,
PrintAndExit,
}

#[derive(Default)]
Expand Down Expand Up @@ -102,6 +102,10 @@ fn print_help() {
}
}

fn print_version() {
println!("hunk v{}", env!("CARGO_PKG_VERSION"));
}

pub fn parse_args(args: &[&str]) -> ParseArgsResult {
struct ParsingState {
has_search_string: bool,
Expand Down Expand Up @@ -185,7 +189,11 @@ pub fn parse_args(args: &[&str]) -> ParseArgsResult {
)),
["--help"] | ["-h"] => {
print_help();
ParseArgsResultInner::Help
ParseArgsResultInner::PrintAndExit
}
["--version"] => {
print_version();
ParseArgsResultInner::PrintAndExit
}
["--", rest @ ..] if !state.has_search_string => {
state.no_more_options = true;
Expand Down Expand Up @@ -231,7 +239,7 @@ pub fn parse_args(args: &[&str]) -> ParseArgsResult {
match parse_slice(args, &mut parsing_state, &mut config) {
ParseArgsResultInner::Error(err) => ParseArgsResult::Error(err),
ParseArgsResultInner::Success => ParseArgsResult::Config(config),
ParseArgsResultInner::Help => ParseArgsResult::Help,
ParseArgsResultInner::PrintAndExit => ParseArgsResult::PrintAndExit,
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ mod tests {

#[test]
fn test_parse_help() {
matches!(parse_args(&vec!["asd", "-h"]), ParseArgsResult::Help);
matches!(parse_args(&vec!["asd", "-h"]), ParseArgsResult::PrintAndExit);
}

#[test]
fn test_parse_version() {
matches!(parse_args(&vec!["hi", "--version"]), ParseArgsResult::PrintAndExit);
}
}

0 comments on commit 557e7dd

Please sign in to comment.