summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs16
-rw-r--r--src/main.rs16
2 files changed, 27 insertions, 5 deletions
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!(<FORMULA> "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::<String>("FORMULA").expect("required")) {
+ Some(result) => println!("Rolled: {}", result),
+ None => eprintln!("Error: Invalid roll formula or calculation failed.")
+ }
+ }
+ _ => unreachable!()
}
}