Skip to content

Commit c8a90ad

Browse files
committed
Add verbose argument and prep for new version
1 parent 4ebecbc commit c8a90ad

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "searcher_txt"
3-
version = "1.2.4"
3+
version = "1.2.5"
44
authors = ["Raphdf201 <raphdf201@hotmail.com>"]
55
edition = "2021"
66
license-file = "unlicense.txt"

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
## Usage
66

77
This is a cli program, which means you need to execute it using `.\searcher_txt.exe` on windows and `./searcher_txt` on
8-
linux and macOS<br/>
9-
The arguments needed are the query, filename and case sensitivity
10-
Example for searching "potato" in file.txt with no case sensitivity on windows :
8+
linux and macOS
9+
10+
The arguments needed are the query and filename. You can also add optional arguments like case sensitivity or verbose
11+
output. Example for searching "potato" in file.txt with no case sensitivity on windows :
1112

1213
```
1314
.\searcher_txt potato file.txt
@@ -19,10 +20,16 @@ If you want your search to be case-sensitive :
1920
.\searcher_txt potato file.txt --case
2021
```
2122

23+
Available arguments :
24+
`--case/-c`
25+
`--verbose/-v`
26+
2227
If you're on linux or macOS, replace the \ by /
2328

2429
## Changelog
2530

31+
1.2.5 : Verbose argument
32+
2633
1.2.4 : New argument usage
2734

2835
1.2.3 : Small optimizations

src/lib.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@ use std::fs;
55

66
/// Runs a search with the provided config
77
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
8+
let results: Vec<&str>;
89
let contents = fs::read_to_string(&config.filename)?;
9-
10-
let results = if config.case {
11-
search(&config.query, &contents)
10+
if config.verbose {
11+
println!("Parsing file contents");
12+
13+
println!("Checking case sensitivity");
14+
results = if config.case {
15+
println!("Running case sensitive search");
16+
search(&config.query, &contents)
17+
} else {
18+
println!("Running case insensitive search");
19+
search_case_insensitive(&config.query, &contents)
20+
};
1221
} else {
13-
search_case_insensitive(&config.query, &contents)
14-
};
15-
22+
results = if config.case {
23+
search(&config.query, &contents)
24+
} else {
25+
search_case_insensitive(&config.query, &contents)
26+
};
27+
}
1628
if results.is_empty() {
1729
println!("Found no line containing {}", config.query);
1830
} else {
@@ -21,7 +33,6 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
2133
println!("{}", line);
2234
}
2335
}
24-
2536
Ok(())
2637
}
2738

@@ -36,12 +47,17 @@ pub struct Config {
3647
/// Case sensitivity (optional)
3748
#[arg(short, long)]
3849
pub case: bool,
50+
/// Verbose logging (optional)
51+
#[arg(short, long)]
52+
pub verbose: bool,
3953
}
4054

4155
/// Implementation of the Config struct
4256
impl Config {
4357
pub fn new(args: &[String]) -> Result<Config, &str> {
44-
if args.len() < 3 {
58+
if args.len() < 4 {
59+
return Err("Missing 3 arguments");
60+
} else if args.len() < 3 {
4561
return Err("Missing 2 arguments");
4662
} else if args.len() < 4 {
4763
return Err("Missing 1 argument");
@@ -50,11 +66,13 @@ impl Config {
5066
let query = args[1].clone();
5167
let filename = args[2].clone();
5268
let case = string_to_bool(args[3].clone());
69+
let verbose = string_to_bool(args[4].clone());
5370

5471
Ok(Config {
5572
query,
5673
filename,
5774
case,
75+
verbose,
5876
})
5977
}
6078
}
@@ -94,8 +112,7 @@ pub fn string_to_bool(string: String) -> bool {
94112
} else if string == "false" {
95113
is_bool = false;
96114
} else {
97-
println!("An error ocurred");
98-
eprintln!("Internal error : bad usage of searcher_txt::string_to_bool");
115+
eprintln!("An error occurred during a string_to_bool operation");
99116
}
100117

101118
is_bool

0 commit comments

Comments
 (0)