Skip to content

Commit 34bd253

Browse files
authored
Merge pull request #42 from spenserblack/master
Use clap to handle command-line arguments
2 parents 64fc1d8 + cc2d95c commit 34bd253

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ git2 = {version = "0.7.5", default-features = false}
1313
tokei = "8.0"
1414
license = "0.7.1"
1515
bytecount = "0.5.1"
16+
clap = "2.33.0"

src/main.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ extern crate colored;
33
extern crate git2;
44
extern crate license;
55
extern crate tokei;
6+
#[macro_use]
7+
extern crate clap;
68

79
use colored::Color;
810
use colored::*;
911
use git2::Repository;
1012
use license::License;
13+
use clap::{App, Arg};
1114
use std::{
1215
cmp,
1316
collections::HashMap,
1417
convert::From,
15-
env,
1618
ffi::OsStr,
1719
fmt,
1820
fmt::Write,
@@ -282,21 +284,17 @@ fn main() -> Result<()> {
282284
return Err(Error::GitNotInstalled);
283285
}
284286

285-
let mut args = env::args();
286-
287-
if args.next().is_none() {
288-
return Err(Error::TooFewArgs);
289-
};
290-
291-
let dir = if let Some(arg) = args.next() {
292-
arg
293-
} else {
294-
String::from(".")
295-
};
296-
297-
if args.next().is_some() {
298-
return Err(Error::TooManyArgs);
299-
};
287+
let matches = App::new(crate_name!())
288+
.version(crate_version!())
289+
.author("o2sh <[email protected]>")
290+
.about(crate_description!())
291+
.arg(Arg::with_name("directory")
292+
.short("d")
293+
.long("dir")
294+
.takes_value(true)
295+
.default_value("."))
296+
.get_matches();
297+
let dir = String::from(matches.value_of("directory").unwrap());
300298

301299
let tokei_langs = project_languages(&dir);
302300
let languages_stat = get_languages_stat(&tokei_langs).ok_or(Error::SourceCodeNotFound)?;
@@ -495,7 +493,6 @@ fn get_configuration(dir: &str) -> Result<Configuration> {
495493

496494
// Return first n most active commiters as authors within this project.
497495
fn get_authors(dir: &str, n: usize) -> Vec<String> {
498-
use std::collections::HashMap;
499496
let output = Command::new("git")
500497
.arg("-C")
501498
.arg(dir)
@@ -673,10 +670,6 @@ enum Error {
673670
ReadDirectory,
674671
/// Not in a Git Repo
675672
NotGitRepo,
676-
/// Too few arguments
677-
TooFewArgs,
678-
/// Too many arguments
679-
TooManyArgs,
680673
}
681674

682675
impl fmt::Debug for Error {
@@ -687,8 +680,6 @@ impl fmt::Debug for Error {
687680
Error::NoGitData => "Could not retrieve git configuration data",
688681
Error::ReadDirectory => "Could not read directory",
689682
Error::NotGitRepo => "This is not a Git Repo",
690-
Error::TooFewArgs => "Too few arguments. Expected program name and a single argument.",
691-
Error::TooManyArgs => "Too many arguments. Expected a single argument.",
692683
};
693684
write!(f, "{}", content)
694685
}

0 commit comments

Comments
 (0)