mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Better usage of clap.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
name = "aoc-2018"
|
name = "aoc-2018"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Bert Peters <bert@bertptrs.nl>"]
|
authors = ["Bert Peters <bert@bertptrs.nl>"]
|
||||||
|
description = "Implementation of the problems of Advent of Code 2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "2.32"
|
clap = "2.32"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
extern crate clap;
|
#[macro_use] extern crate clap;
|
||||||
extern crate aoc_2018;
|
extern crate aoc_2018;
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -8,27 +8,24 @@ use std::time::Instant;
|
|||||||
|
|
||||||
use aoc_2018::*;
|
use aoc_2018::*;
|
||||||
|
|
||||||
use clap::{App, Arg};
|
use clap::Arg;
|
||||||
|
|
||||||
fn get_impl(day: &str) -> Box<common::Solution> {
|
fn get_impl(day: u32) -> Box<common::Solution> {
|
||||||
match day.parse() {
|
match day {
|
||||||
Ok(1) => Box::new(day01::Day01::new()),
|
1 => Box::new(day01::Day01::new()),
|
||||||
Ok(2) => Box::new(day02::Day02::new()),
|
2 => Box::new(day02::Day02::new()),
|
||||||
Ok(3) => Box::new(day03::Day03::new()),
|
3 => Box::new(day03::Day03::new()),
|
||||||
Ok(4) => Box::new(day04::Day04::new()),
|
4 => Box::new(day04::Day04::new()),
|
||||||
Ok(5) => Box::new(day05::Day05::new()),
|
5 => Box::new(day05::Day05::new()),
|
||||||
Ok(6) => Box::new(day06::Day06::new()),
|
6 => Box::new(day06::Day06::new()),
|
||||||
Ok(7) => Box::new(day07::Day07::new()),
|
7 => Box::new(day07::Day07::new()),
|
||||||
Ok(8) => Box::new(day08::Day08::new()),
|
8 => Box::new(day08::Day08::new()),
|
||||||
Ok(val) => panic!("Unimplemented day {}", val),
|
val => panic!("Unimplemented day {}", val),
|
||||||
_ => panic!("Invalid number"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = App::new("Advent of Code")
|
let matches = app_from_crate!()
|
||||||
.version("2018")
|
|
||||||
.author("Bert Peters <bert@bertptrs.nl>")
|
|
||||||
.arg(Arg::with_name("day")
|
.arg(Arg::with_name("day")
|
||||||
.value_name("DAY")
|
.value_name("DAY")
|
||||||
.help("Number of the day to execute")
|
.help("Number of the day to execute")
|
||||||
@@ -36,7 +33,7 @@ fn main() {
|
|||||||
.takes_value(true))
|
.takes_value(true))
|
||||||
.arg(Arg::with_name("part2")
|
.arg(Arg::with_name("part2")
|
||||||
.short("2")
|
.short("2")
|
||||||
.help("Whether to run part 2")
|
.help("Run part 2 instead of part 1")
|
||||||
.long("part2"))
|
.long("part2"))
|
||||||
.arg(Arg::with_name("input")
|
.arg(Arg::with_name("input")
|
||||||
.short("i")
|
.short("i")
|
||||||
@@ -49,7 +46,7 @@ fn main() {
|
|||||||
.help("Print the time for the result"))
|
.help("Print the time for the result"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let mut implementation = get_impl(matches.value_of("day").unwrap());
|
let mut implementation = get_impl(value_t_or_exit!(matches, "day", u32));
|
||||||
let mut data: Box<io::Read> = match matches.value_of("input") {
|
let mut data: Box<io::Read> = match matches.value_of("input") {
|
||||||
Some(filename) => { Box::new(fs::File::open(filename).unwrap()) }
|
Some(filename) => { Box::new(fs::File::open(filename).unwrap()) }
|
||||||
None => { Box::new(io::stdin()) }
|
None => { Box::new(io::stdin()) }
|
||||||
@@ -74,9 +71,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_get_impl() {
|
fn test_get_impl() {
|
||||||
// Verify that we can load all days
|
// Verify that we can load all days
|
||||||
let last_implemented = 6;
|
let last_implemented = 8;
|
||||||
for d in 1..=last_implemented {
|
for d in 1..=last_implemented {
|
||||||
get_impl(&format!("{}", d));
|
get_impl(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user