Skip to content

Commit 20d3ec9

Browse files
committed
add --file cmd parameter
1 parent 1d19403 commit 20d3ec9

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn get_from_path_or_else<E: Error>(
4444
Ok(res.trim().to_string())
4545
} else {
4646
let res = fallback()?;
47-
create_dir_all(PathBuf::from(path).parent().unwrap())
47+
create_dir_all(PathBuf::from(path).parent().expect("no parent directory"))
4848
.and_then(|_| File::create(path))
4949
.and_then(|mut file| file.write_all(res.as_bytes()))
5050
.unwrap_or_else(|err| eprintln!("could not write {}: {}", path, err));

src/lib.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ pub mod input;
22

33
use std::cmp::min;
44
use std::iter;
5+
use std::path::PathBuf;
56
use std::time::Duration;
67

78
pub use clap::Clap;
8-
pub use colored;
99
use colored::*;
1010

1111
const DISPLAY_WIDTH: usize = 40;
@@ -41,11 +41,15 @@ pub fn print_with_duration(line: &str, output: Option<&str>, duration: Duration)
4141
)]
4242
pub struct Opt {
4343
/// Read input from stdin instead of downloading it
44-
#[clap(short, long)]
44+
#[clap(short = 'i', long, conflicts_with = "file")]
4545
pub stdin: bool,
4646

47+
/// Read input from file instead of downloading it
48+
#[clap(short, long, conflicts_with = "stdin")]
49+
pub file: Option<PathBuf>,
50+
4751
/// Days to execute. By default all implemented days will run.
48-
#[clap(short, long)]
52+
#[clap(name = "day num", short, long = "day")]
4953
pub days: Vec<String>,
5054
}
5155

@@ -56,8 +60,9 @@ macro_rules! main {
5660
$( $day: ident $( : $generator: ident )? => $( $solution: ident ),+ );+
5761
$( ; )?
5862
) => {
59-
use std::time::Instant;
63+
use std::fs::read_to_string;
6064
use std::io::Read;
65+
use std::time::Instant;
6166

6267
use $crate::Clap;
6368

@@ -69,6 +74,27 @@ macro_rules! main {
6974

7075
if opt.days.is_empty() {
7176
opt.days = DAYS.iter().map(|s| s[3..].to_string()).collect();
77+
} else {
78+
let ignored_days: Vec<_> = opt.days
79+
.iter()
80+
.filter(|day| !DAYS.contains(&format!("day{}", day).as_str()))
81+
.map(String::as_str)
82+
.collect();
83+
84+
if !ignored_days.is_empty() {
85+
eprintln!(r"/!\ Ignoring unimplemented days: {}", ignored_days.join(", "));
86+
}
87+
88+
opt.days = opt.days
89+
.into_iter()
90+
.filter(|day| DAYS.contains(&format!("day{}", day).as_str()))
91+
.collect();
92+
}
93+
94+
if opt.days.len() > 1 && (opt.stdin || opt.file.is_some()) {
95+
eprintln!(r"/!\ You are using a personalized output over several days which can");
96+
eprintln!(r" be missleading. If you only intend to run solutions for a");
97+
eprintln!(r" specific day, you can specify it by using the `-d DAY_NUM` flag.");
7298
}
7399

74100
for (i, day) in opt.days.iter().enumerate() {
@@ -94,6 +120,9 @@ macro_rules! main {
94120
std::io::stdin().read_to_string(&mut data)
95121
.expect("failed to read from stdin");
96122
data
123+
} else if let Some(path) = opt.file.as_ref() {
124+
read_to_string(path)
125+
.expect("failed to read specified file")
97126
} else {
98127
$crate::input::get_input(YEAR, day).expect("could not fetch input")
99128
}

0 commit comments

Comments
 (0)