Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
greenberga committed Jul 9, 2018
1 parent 2568c22 commit bfdd890
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
175 changes: 175 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "choose"
version = "0.1.0"
authors = ["Aaron Greenberg <[email protected]>"]

[dependencies]
clap = "2.32.0"
rand = "0.5.3"
34 changes: 34 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
extern crate clap;
extern crate rand;

use clap::{Arg, App};
use rand::{thread_rng, Rng};
use std::io::{self, Read};

fn main() {
let matches = App::new("choose")
.version("0.1.0")
.author("Aaron Greenberg <[email protected]>")
.about("a command line tool for random selection from a list")
.arg(Arg::with_name("count")
.takes_value(true)
.default_value("1")
.help("number of items to choose"))
.arg(Arg::with_name("delimiter")
.short("d")
.takes_value(true)
.default_value("\n")
.help("list delimiter"))
.get_matches();
let count = matches.value_of("count").unwrap().parse::<i32>().unwrap();
let delim = matches.value_of("delimiter").unwrap();

let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).unwrap();
let choices: Vec<&str> = buffer.split(delim).collect();

let mut rng = thread_rng();
for _ in 0..count {
println!("{}", rng.choose(&choices).unwrap());
}
}

0 comments on commit bfdd890

Please sign in to comment.