Skip to content

Commit

Permalink
Merge pull request #4 from akoken/feature-handle-args-better
Browse files Browse the repository at this point in the history
Use clap for command line arguments
  • Loading branch information
akoken authored Oct 15, 2024
2 parents 2f9f95c + b02676c commit 5b00b93
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 112 deletions.
230 changes: 230 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ To install Minigrep, you can download binaries or follow these steps:

2. Case-insensitive search for "warning" in a log file:
```bash
export IGNORE_CASE=1
minigrep warning app.log
minigrep -i warning app.log
```

## Contributing
Expand Down
46 changes: 46 additions & 0 deletions src/args.rs
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,
}
}
Loading

0 comments on commit 5b00b93

Please sign in to comment.