aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2020-12-08 02:53:10 +0900
committernsfisis <nsfisis@gmail.com>2020-12-08 02:55:44 +0900
commitf9f462cd2feb811891fe4e4919534cbdb6dcba2c (patch)
treeecb670b0242daa7e7e1df77c146f3881c3e075e8 /src/main.rs
downloadrand-word-gen-f9f462cd2feb811891fe4e4919534cbdb6dcba2c.tar.gz
rand-word-gen-f9f462cd2feb811891fe4e4919534cbdb6dcba2c.tar.zst
rand-word-gen-f9f462cd2feb811891fe4e4919534cbdb6dcba2c.zip
first commit
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..4ebe0fa
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,41 @@
+use anyhow::{Context, Result};
+use rand::distributions::{Distribution, Uniform};
+use rand_word_gen::Model;
+use std::io::{BufWriter, Write};
+
+fn main() -> Result<()> {
+ let words = parse_args()?;
+
+ let model = Model::new();
+ let out = std::io::stdout();
+ let mut out = BufWriter::new(out.lock());
+ let mut rng = rand::thread_rng();
+ let dist = Uniform::from(3..=6);
+ for _ in 0..words {
+ let len = dist.sample(&mut rng);
+ writeln!(out, "{}", model.generate(&mut rng, len))?;
+ }
+
+ Ok(())
+}
+
+fn parse_args() -> Result<usize> {
+ use clap::{crate_description, crate_version, value_t, App, Arg};
+
+ let matches = App::new("rand-word-gen")
+ .version(crate_version!())
+ .version_short("v")
+ .about(crate_description!())
+ .arg(
+ Arg::with_name("words")
+ .short("n")
+ .long("words")
+ .value_name("NUMBER_OF_WORDS")
+ .default_value("20")
+ .help("Sets number of generated words"),
+ )
+ .get_matches();
+
+ let words = value_t!(matches, "words", usize).context("'--words' must be a number")?;
+ Ok(words)
+}