From b7557465ef562b5ea73b548042ce5d1df826037f Mon Sep 17 00:00:00 2001 From: Fletcher Nichol Date: Sat, 19 Sep 2015 12:28:27 -0700 Subject: [PATCH] MVP. --- Cargo.lock | 20 ++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 30 +++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 67d9304..9e60900 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "names" version = "0.1.0" dependencies = [ + "clap 1.4.0 (git+https://github.com/kbknapp/clap-rs?rev=40104af)", "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -14,6 +15,20 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ansi_term" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clap" +version = "1.4.0" +source = "git+https://github.com/kbknapp/clap-rs?rev=40104af#40104afe9d124b658569656ca643dc0a9d813216" +dependencies = [ + "ansi_term 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "libc" version = "0.1.10" @@ -29,6 +44,11 @@ dependencies = [ "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strsim" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.4" diff --git a/Cargo.toml b/Cargo.toml index 32ff5f1..031fe5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,6 @@ version = "0.1.0" authors = ["Fletcher Nichol "] [dependencies] +# pending a release which includes https://github.com/kbknapp/clap-rs/pull/257 +clap = { git = "https://github.com/kbknapp/clap-rs", rev = "40104af" } rand = "0.3.0" diff --git a/src/main.rs b/src/main.rs index 035ca3f..45daa6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,39 @@ extern crate rand; use rand::Rng; +#[macro_use] +extern crate clap; + +use clap::{App, Arg}; + fn main() { + let matches = App::new("names") + .version(&crate_version!()[..]) + .author("Fletcher Nichol ") + .about("Random name generator") + .arg(Arg::with_name("AMOUNT") + .help("Number of names to generate (default: 1)") + .index(1) + ) + .arg(Arg::with_name("number") + .short("n") + .long("number") + .help("Adds a random number to the name(s)") + ) + .get_matches(); + + let amount = value_t!(matches.value_of("AMOUNT"), u32).unwrap_or(1); let adjectives = Dict::new(ADJECTIVE_WORDS); let nouns = Dict::new(NOUN_WORDS); - println!("{}-{}", adjectives.random(), nouns.random()); + for _ in 0..amount { + if matches.is_present("number") { + let number = rand::thread_rng().gen_range(1, 10000); + println!("{}-{}-{:04}", adjectives.random(), nouns.random(), number); + } else { + println!("{}-{}", adjectives.random(), nouns.random()); + } + } } struct Dict<'a> {