diff options
-rw-r--r-- | src/cli.rs | 15 | ||||
-rw-r--r-- | src/main.rs | 62 |
2 files changed, 58 insertions, 19 deletions
@@ -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"); |