Skip to content

Commit

Permalink
UPD cli arguments using clap
Browse files Browse the repository at this point in the history
  • Loading branch information
katekorsaro committed Jan 17, 2024
1 parent 792fa38 commit 9089159
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ keywords = ["rpg","dice","random"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.18", features = ["derive"] }
rand = "0.8.5"
29 changes: 16 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
use std::env;
use clap::Parser;
use iron_dice::Roller;

#[derive(Parser)]
#[command()]
struct Args {
#[arg(long, short)]
/// the definition of dice to throw. "3d6" "4d6 max3" "5d10 sc9"
definition: Option<String>,
}
fn main() {
let args = env::args();
let args = args
.skip(1)
.reduce(|mut acc:String, e| {
acc.push_str(" ");
acc.push_str(&e);
acc
})
.unwrap();
let args = args.trim();
let args = Args::parse();

let definition = match args.definition {
Some(definition) => definition,
None => String::from("3d6"),
};

let mut r:Roller = String::from(args).parse().unwrap();
let mut r: Roller = definition.parse().unwrap();
let result = r.roll();

println!("{:?} {}", result.dice, result.outcome);
println!("{:?} => {}", result.dice, result.outcome);
}

0 comments on commit 9089159

Please sign in to comment.