Skip to content

Commit 36088b7

Browse files
committed
Use clap to handle command-line arguments
Adds a default help message (provided by -h option)
1 parent 64fc1d8 commit 36088b7

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
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-15
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ 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,
@@ -282,21 +285,17 @@ fn main() -> Result<()> {
282285
return Err(Error::GitNotInstalled);
283286
}
284287

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-
};
288+
let matches = App::new(crate_name!())
289+
.version(crate_version!())
290+
.author(crate_authors!("\n"))
291+
.about(crate_description!())
292+
.arg(Arg::with_name("directory")
293+
.short("d")
294+
.long("dir")
295+
.takes_value(true)
296+
.default_value("."))
297+
.get_matches();
298+
let dir = String::from(matches.value_of("directory").unwrap());
300299

301300
let tokei_langs = project_languages(&dir);
302301
let languages_stat = get_languages_stat(&tokei_langs).ok_or(Error::SourceCodeNotFound)?;

0 commit comments

Comments
 (0)