From 4e5f2211e0be7d189235bc88b1ca8fef29e77f87 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sat, 30 Sep 2023 23:14:13 -0400 Subject: Basic CLI --- Cargo.toml | 6 ++++-- src/cli.rs | 16 ++++++++++++++++ src/main.rs | 16 +++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/cli.rs diff --git a/Cargo.toml b/Cargo.toml index 353126b..39437c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,12 +2,14 @@ name = "dmn" version = "0.1.0" edition = "2021" +description = "A CLI and library to assist with Advanced Dungeons & Dragons (AD&D)" authors = ["David Gay "] +repository = "https://git.aikuro.net/poems/dmn" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = "^4.4" +clap = { version = "^4.4", features = ["cargo"] } rand = "^0.8" -serde = "^1.0" \ No newline at end of file +serde = "^1.0" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..4ceaf90 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,16 @@ +use clap::{arg, Command, crate_authors, crate_description, crate_name, crate_version}; + +pub fn cli() -> Command { + Command::new(crate_name!()) + .about(crate_description!()) + .author(crate_authors!()) + .version(crate_version!()) + .subcommand_required(true) + .arg_required_else_help(true) + .allow_external_subcommands(true) + .subcommand(Command::new("roll") + .about("Rolls dice based on a given formula.") + .arg(arg!( "The dice rolling formula")) + .arg_required_else_help(true), + ) +} diff --git a/src/main.rs b/src/main.rs index 88fa2ab..c3431cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,16 @@ +mod cli; mod dice; fn main() { - const TEST_FORMULA : &str = "3d6"; - println!("Rolling {}:", TEST_FORMULA); - match dice::roll_formula(TEST_FORMULA) { - Some(result) => println!("{}", result), - None => eprintln!("Error: Invalid roll formula, or die roll otherwise failed.") + let matches = cli::cli().get_matches(); + + match matches.subcommand() { + Some(("roll", sub_matches)) => { + match dice::roll_formula(sub_matches.get_one::("FORMULA").expect("required")) { + Some(result) => println!("Rolled: {}", result), + None => eprintln!("Error: Invalid roll formula or calculation failed.") + } + } + _ => unreachable!() } } -- cgit v1.2.3