aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2019-12-02 03:22:51 +1000
committerGravatar alecdwm 2019-12-02 03:22:51 +1000
commit175671c82b3ca0938a99c2f6f7cee4ff9f1eeafd (patch)
tree5087a5dbc68ea6a1f8593a68a2e28814d88238ce
parent5a5b0179214080aa1e3b473c27c822a64f12b00c (diff)
improved cli
-rw-r--r--src/common.rs4
-rw-r--r--src/main.rs76
2 files changed, 44 insertions, 36 deletions
diff --git a/src/common.rs b/src/common.rs
index b3cb656..9fda70a 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -2,9 +2,7 @@
use std::io;
-/// Reads from stdin until an empty line is encountered
-///
-/// Returns the read data as a string
+/// Collects from stdin into a String until an empty line is encountered
pub fn read_stdin_to_string() -> String {
let mut input = String::new();
let mut line = String::new();
diff --git a/src/main.rs b/src/main.rs
index c8b7ce5..2c75a89 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,45 +1,55 @@
use std::collections::BTreeMap;
+use std::env;
fn main() {
- let args: Vec<String> = std::env::args().collect();
- if args.len() < 2 {
- eprintln!("usage: advent-of-code-2018 list | <puzzle_solution>");
- std::process::exit(1);
- }
+ let mut puzzle_solutions: BTreeMap<&str, fn()> = BTreeMap::new();
- let mut puzzle_solution_map: BTreeMap<&str, fn()> = BTreeMap::new();
- puzzle_solution_map.insert("y2018::day1::part1", advent_of_code::y2018::day1::part1);
- puzzle_solution_map.insert("y2018::day1::part2", advent_of_code::y2018::day1::part2);
- puzzle_solution_map.insert("y2018::day2::part1", advent_of_code::y2018::day2::part1);
- puzzle_solution_map.insert("y2018::day2::part2", advent_of_code::y2018::day2::part2);
- puzzle_solution_map.insert("y2018::day3::part1", advent_of_code::y2018::day3::part1);
- puzzle_solution_map.insert("y2018::day3::part2", advent_of_code::y2018::day3::part2);
- puzzle_solution_map.insert("y2018::day4::part1", advent_of_code::y2018::day4::part1);
- puzzle_solution_map.insert("y2018::day4::part2", advent_of_code::y2018::day4::part2);
- puzzle_solution_map.insert("y2018::day5::part1", advent_of_code::y2018::day5::part1);
- puzzle_solution_map.insert("y2018::day5::part2", advent_of_code::y2018::day5::part2);
- puzzle_solution_map.insert("y2018::day6::part1", advent_of_code::y2018::day6::part1);
- puzzle_solution_map.insert("y2018::day6::part2", advent_of_code::y2018::day6::part2);
- puzzle_solution_map.insert("y2018::day7::part1", advent_of_code::y2018::day7::part1);
- puzzle_solution_map.insert("y2018::day7::part2", advent_of_code::y2018::day7::part2);
+ puzzle_solutions.insert("y2018::day1::part1", advent_of_code::y2018::day1::part1);
+ puzzle_solutions.insert("y2018::day1::part2", advent_of_code::y2018::day1::part2);
+ puzzle_solutions.insert("y2018::day2::part1", advent_of_code::y2018::day2::part1);
+ puzzle_solutions.insert("y2018::day2::part2", advent_of_code::y2018::day2::part2);
+ puzzle_solutions.insert("y2018::day3::part1", advent_of_code::y2018::day3::part1);
+ puzzle_solutions.insert("y2018::day3::part2", advent_of_code::y2018::day3::part2);
+ puzzle_solutions.insert("y2018::day4::part1", advent_of_code::y2018::day4::part1);
+ puzzle_solutions.insert("y2018::day4::part2", advent_of_code::y2018::day4::part2);
+ puzzle_solutions.insert("y2018::day5::part1", advent_of_code::y2018::day5::part1);
+ puzzle_solutions.insert("y2018::day5::part2", advent_of_code::y2018::day5::part2);
+ puzzle_solutions.insert("y2018::day6::part1", advent_of_code::y2018::day6::part1);
+ puzzle_solutions.insert("y2018::day6::part2", advent_of_code::y2018::day6::part2);
+ puzzle_solutions.insert("y2018::day7::part1", advent_of_code::y2018::day7::part1);
+ puzzle_solutions.insert("y2018::day7::part2", advent_of_code::y2018::day7::part2);
- puzzle_solution_map.insert("y2019::day1::part1", advent_of_code::y2019::day1::part1);
- puzzle_solution_map.insert("y2019::day1::part2", advent_of_code::y2019::day1::part2);
+ puzzle_solutions.insert("y2019::day1::part1", advent_of_code::y2019::day1::part1);
+ puzzle_solutions.insert("y2019::day1::part2", advent_of_code::y2019::day1::part2);
- let command = args[1].as_str();
- if command == "list" {
- for puzzle_solution in puzzle_solution_map.keys() {
- println!("{}", puzzle_solution);
+ let command = match env::args().skip(1).next() {
+ Some(command) => command,
+ None => {
+ eprintln!(
+ "Usage: advent-of-code <command>\n\nCommands:\n\tlist{}",
+ puzzle_solutions
+ .keys()
+ .map(|key| format!("\n\t{}", key))
+ .collect::<String>()
+ );
+ std::process::exit(1);
}
- std::process::exit(1);
- }
+ };
- let puzzle_solution = args[1].as_str();
- match puzzle_solution_map.get(puzzle_solution) {
- None => {
- eprintln!("puzzle solution '{}' not found", puzzle_solution);
+ match command.as_str() {
+ "list" => {
+ for puzzle_solution in puzzle_solutions.keys() {
+ println!("{}", puzzle_solution);
+ }
std::process::exit(1);
}
- Some(day_fn) => day_fn(),
+
+ puzzle_solution => match puzzle_solutions.get(puzzle_solution) {
+ Some(puzzle_solution_fn) => puzzle_solution_fn(),
+ None => {
+ eprintln!("Puzzle solution '{}' not found", puzzle_solution);
+ std::process::exit(1);
+ }
+ },
}
}