Skip to content

Commit dd7f30a

Browse files
authored
Merge pull request #6 from kevinmatthes/main
[Enhancement] New Option for Help and Source Code Documentation
2 parents 96a0ab4 + bcedcee commit dd7f30a

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
2-
*.txt
32
*.csv
3+
*.log
4+
*.txt

src/main.rs

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use getopts::Options;
12
use std::{collections::HashMap, error::Error, io::Write, num::ParseIntError, ops::AddAssign};
23

34
fn main() -> Result<(), Box<dyn Error>> {
45
let mut opts = getopts::Options::new();
56
let opts = opts
6-
.optflag("q", "quiet", "Hides the output of commit messages")
7+
.optflag("q", "quiet", "Hides the output of commit messages.")
8+
.optflag("h", "help", "Show this help and exit.")
79
.optmulti(
810
"a",
911
"author-contains",
@@ -79,12 +81,12 @@ fn main() -> Result<(), Box<dyn Error>> {
7981
let matches = match opts.parse(std::env::args()) {
8082
Ok(it) => it,
8183
Err(err) => {
82-
usage("commit-analzer", opts);
84+
usage(opts);
8385
return Err(err.into());
8486
}
8587
};
86-
if matches.free.len() < 2 {
87-
usage(&matches.free[0], opts);
88+
if matches.opt_present("h") || matches.free.len() < 2 {
89+
usage(opts);
8890
return Ok(());
8991
}
9092
let is_quiet = matches.opt_present("q");
@@ -176,9 +178,15 @@ fn main() -> Result<(), Box<dyn Error>> {
176178
Ok(())
177179
}
178180

179-
fn usage(program_name: &str, opts: &getopts::Options) {
180-
println!("Usage: {} <FILE> [OPTIONS]\n", program_name);
181-
println!("{}", opts.usage("Parses the output of `git log`."));
181+
/// A small in-app documentation.
182+
///
183+
/// This function will write a brief usage information, including a short
184+
/// introduction to the meaning of the configured `options`, to `stdout`.
185+
fn usage(options: &Options) {
186+
println!(
187+
"Usage: commit-analyzer <FILE> [OPTIONS]\n\n{}",
188+
options.usage("Parses the output of `git log`.")
189+
);
182190
}
183191

184192
fn matches_filter(commit: &Commit, filter: &Filter) -> bool {
@@ -239,7 +247,11 @@ impl Filter {
239247
}
240248

241249
fn check_loc(&self, loc: &&Loc) -> bool {
242-
self.file_extension.is_empty() || self.file_extension.iter().any(|ext| loc.file.ends_with(&format!(".{}", ext)))
250+
self.file_extension.is_empty()
251+
|| self
252+
.file_extension
253+
.iter()
254+
.any(|ext| loc.file.ends_with(&format!(".{}", ext)))
243255
}
244256
}
245257

@@ -320,7 +332,7 @@ fn parse_commit(commit: &str) -> Result<(Commit, &str), CommitParseError> {
320332
if loc.is_empty() || loc.starts_with("commit") {
321333
break;
322334
}
323-
locs.push(Loc::parse(loc).map_err(|err| CommitParseError::LocFailed(err))?);
335+
locs.push(Loc::parse(loc).map_err(CommitParseError::LocFailed)?);
324336
remainder_result = remainder;
325337
if let Some(remainder) = remainder_result.strip_prefix('\n') {
326338
remainder_result = remainder;
@@ -331,9 +343,9 @@ fn parse_commit(commit: &str) -> Result<(Commit, &str), CommitParseError> {
331343
let commit = Commit {
332344
commit: commit.into(),
333345
merge,
334-
author: Author::parse(author).map_err(|err| CommitParseError::AuthorFailed(err))?,
346+
author: Author::parse(author).map_err(CommitParseError::AuthorFailed)?,
335347
date: chrono::DateTime::parse_from_str(date, "%a %b %e %T %Y %z")
336-
.map_err(|err| CommitParseError::DateFailed(err))?,
348+
.map_err(CommitParseError::DateFailed)?,
337349
message: message.into(),
338350
locs,
339351
};
@@ -352,7 +364,11 @@ pub struct Commit {
352364

353365
impl Commit {
354366
pub fn loc(&self, filter: &Filter) -> i64 {
355-
self.locs.iter().filter(|l| filter.check_loc(l)).map(|l| l.loc()).sum()
367+
self.locs
368+
.iter()
369+
.filter(|l| filter.check_loc(l))
370+
.map(|l| l.loc())
371+
.sum()
356372
}
357373
}
358374

@@ -407,20 +423,12 @@ impl Loc {
407423
let added = if added == "-" {
408424
None
409425
} else {
410-
Some(
411-
added
412-
.parse()
413-
.map_err(|err| LocParseError::AddedParseError(err))?,
414-
)
426+
Some(added.parse().map_err(LocParseError::AddedParseError)?)
415427
};
416428
let removed = if removed == "-" {
417429
None
418430
} else {
419-
Some(
420-
removed
421-
.parse()
422-
.map_err(|err| LocParseError::RemovedParseError(err))?,
423-
)
431+
Some(removed.parse().map_err(LocParseError::RemovedParseError)?)
424432
};
425433
let file = file.into();
426434
Ok(Self {

0 commit comments

Comments
 (0)