This commit is contained in:
Fletcher Nichol 2015-09-19 12:28:27 -07:00
parent f852f53302
commit b7557465ef
3 changed files with 51 additions and 1 deletions

20
Cargo.lock generated
View File

@ -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"

View File

@ -4,4 +4,6 @@ version = "0.1.0"
authors = ["Fletcher Nichol <fnichol@nichol.ca>"]
[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"

View File

@ -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 <fnichol@nichol.ca>")
.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> {