Skip to content

Commit

Permalink
Use clap to handle command-line arguments
Browse files Browse the repository at this point in the history
Adds a default help message (provided by -h
option)
  • Loading branch information
spenserblack committed Oct 1, 2019
1 parent 64fc1d8 commit 36088b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ git2 = {version = "0.7.5", default-features = false}
tokei = "8.0"
license = "0.7.1"
bytecount = "0.5.1"
clap = "2.33.0"
29 changes: 14 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ extern crate colored;
extern crate git2;
extern crate license;
extern crate tokei;
#[macro_use]
extern crate clap;

use colored::Color;
use colored::*;
use git2::Repository;
use license::License;
use clap::{App, Arg};
use std::{
cmp,
collections::HashMap,
Expand Down Expand Up @@ -282,21 +285,17 @@ fn main() -> Result<()> {
return Err(Error::GitNotInstalled);
}

let mut args = env::args();

if args.next().is_none() {
return Err(Error::TooFewArgs);
};

let dir = if let Some(arg) = args.next() {
arg
} else {
String::from(".")
};

if args.next().is_some() {
return Err(Error::TooManyArgs);
};
let matches = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!("\n"))
.about(crate_description!())
.arg(Arg::with_name("directory")
.short("d")
.long("dir")
.takes_value(true)
.default_value("."))
.get_matches();
let dir = String::from(matches.value_of("directory").unwrap());

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

0 comments on commit 36088b7

Please sign in to comment.