-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from akoken/feature-handle-args-better
Use clap for command line arguments
- Loading branch information
Showing
6 changed files
with
355 additions
and
112 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[dependencies] | ||
clap = { version = "4.5.20", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use clap::{Arg, ArgAction, Command}; | ||
|
||
#[derive(Debug)] | ||
pub struct Config { | ||
pub pattern: String, | ||
pub filename: String, | ||
pub ignore_case: bool, | ||
} | ||
|
||
pub fn parse_args() -> Config { | ||
let matches = Command::new("minigrep") | ||
.version("1.0") | ||
.author("Abdurrahman Alp Köken") | ||
.about("Searches for a pattern in a file") | ||
.arg( | ||
Arg::new("pattern") | ||
.help("The pattern to search for") | ||
.required(true) | ||
.index(1), | ||
) | ||
.arg( | ||
Arg::new("file") | ||
.help("The file to search in") | ||
.required(true) | ||
.index(2), | ||
) | ||
.arg( | ||
Arg::new("ignore_case") | ||
.short('i') | ||
.long("ignore-case") | ||
.help("Ignore case during search") | ||
.action(ArgAction::SetTrue), | ||
) | ||
.get_matches(); | ||
|
||
// Get the values from the matches | ||
let pattern = matches.get_one::<String>("pattern").unwrap().to_string(); | ||
let filename = matches.get_one::<String>("file").unwrap().to_string(); | ||
let ignore_case = matches.get_flag("ignore_case"); | ||
|
||
Config { | ||
pattern, | ||
filename, | ||
ignore_case, | ||
} | ||
} |
Oops, something went wrong.