summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <eapoems@riseup.net>2023-10-16 15:48:29 -0400
committerDavid Gay <eapoems@riseup.net>2023-10-16 15:48:29 -0400
commit5ecd591a49c78f6059fc840ed5551ef7f5cad895 (patch)
tree2d4256d46d58aed2a7928d67542e9001c4259d3f
parent152cc94968badba24593a69d6d9917a6638ac659 (diff)
CSV output for random henchmen
-rw-r--r--src/cli.rs15
-rw-r--r--src/main.rs62
2 files changed, 58 insertions, 19 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 226b06a..4fdef22 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,4 +1,6 @@
-use clap::{arg, crate_authors, crate_description, crate_name, crate_version, Command};
+use clap::{
+ arg, crate_authors, crate_description, crate_name, crate_version, Arg, ArgAction, Command,
+};
pub fn cli() -> Command {
Command::new(crate_name!())
@@ -12,7 +14,16 @@ pub fn cli() -> Command {
Command::new("random")
.about("Generates a random something")
.args_conflicts_with_subcommands(true)
- .subcommand(Command::new("henchman").about("Generates a random henchman"))
+ .subcommand(
+ Command::new("henchman")
+ .about("Generates a random henchman")
+ .arg(
+ Arg::new("csv")
+ .long("csv")
+ .action(ArgAction::SetTrue)
+ .help("Output in CSV format"),
+ ),
+ )
.subcommand(Command::new("magic").about("Generates a magic item")),
)
.subcommand(
diff --git a/src/main.rs b/src/main.rs
index 307c1f9..9fd7963 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,7 +18,7 @@ fn main() {
Some(("random", sub_matches)) => {
let random_command = sub_matches.subcommand().unwrap();
match random_command {
- ("henchman", _) => {
+ ("henchman", henchman_matches) => {
let class_name = random_tables.roll_table("henchman_class").to_string();
let race_name = random_tables.roll_table("henchman_race").to_string();
// HACK: Need a proper way to do lookups, shouldn't rely on
@@ -43,22 +43,50 @@ fn main() {
);
npc.roll_henchman_ability_scores();
let ability_scores = npc.ability_scores.unwrap();
- println!(
- "{} {} {}. STR {}, INT {}, WIS {}, CON {}, DEX {}, CHA {}",
- npc.alignment.unwrap(),
- npc.race.unwrap().name,
- npc.class.unwrap().name,
- ability_scores.get_score(AbilityScore::Strength).unwrap(),
- ability_scores
- .get_score(AbilityScore::Intelligence)
- .unwrap(),
- ability_scores.get_score(AbilityScore::Wisdom).unwrap(),
- ability_scores
- .get_score(AbilityScore::Constitution)
- .unwrap(),
- ability_scores.get_score(AbilityScore::Dexterity).unwrap(),
- ability_scores.get_score(AbilityScore::Charisma).unwrap()
- );
+
+ let output_csv = henchman_matches.get_flag("csv");
+
+ // TODO: DRY. Can't store template in a variable since println! needs the
+ // string literal at compile time.
+ if output_csv {
+ println!(
+ "{},{},{},{},{},{},{},{},{}",
+ npc.alignment
+ .unwrap()
+ .split_whitespace()
+ .map(|word| word.chars().next().unwrap())
+ .collect::<String>(),
+ npc.race.unwrap().name,
+ npc.class.unwrap().name,
+ ability_scores.get_score(AbilityScore::Strength).unwrap(),
+ ability_scores
+ .get_score(AbilityScore::Intelligence)
+ .unwrap(),
+ ability_scores.get_score(AbilityScore::Wisdom).unwrap(),
+ ability_scores
+ .get_score(AbilityScore::Constitution)
+ .unwrap(),
+ ability_scores.get_score(AbilityScore::Dexterity).unwrap(),
+ ability_scores.get_score(AbilityScore::Charisma).unwrap()
+ );
+ } else {
+ println!(
+ "{} {} {}. STR {}, INT {}, WIS {}, CON {}, DEX {}, CHA {}",
+ npc.alignment.unwrap(),
+ npc.race.unwrap().name,
+ npc.class.unwrap().name,
+ ability_scores.get_score(AbilityScore::Strength).unwrap(),
+ ability_scores
+ .get_score(AbilityScore::Intelligence)
+ .unwrap(),
+ ability_scores.get_score(AbilityScore::Wisdom).unwrap(),
+ ability_scores
+ .get_score(AbilityScore::Constitution)
+ .unwrap(),
+ ability_scores.get_score(AbilityScore::Dexterity).unwrap(),
+ ability_scores.get_score(AbilityScore::Charisma).unwrap()
+ );
+ };
}
("magic", _) => {
let magic = random_tables.roll_table("ua_magic");