Skip to content

Commit 2175141

Browse files
committed
Cascade options down from cargo aoc to cargo aoc <bench/input>
1 parent 618531b commit 2175141

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

cargo-aoc/src/main.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ enum SubCommands {
4949
}
5050

5151
/// Runs the benchmark for the last day (or a given day)
52-
#[derive(Parser, Debug)]
52+
#[derive(Parser, Debug, Clone)]
5353
pub struct Bench {
5454
/// Specifies the day. Defaults to last implemented.
5555
#[clap(short, long)]
@@ -76,14 +76,25 @@ pub struct Bench {
7676
profile: bool,
7777
}
7878

79+
impl Bench {
80+
/// Cascade --day/--part/--input from the root command to the bench command
81+
fn cascade(&self, cli: &Cli) -> Self {
82+
let mut bench = (*self).clone();
83+
bench.day = self.day.or(cli.day);
84+
bench.part = self.part.or(cli.part);
85+
bench.input = bench.input.or_else(|| cli.input.clone());
86+
bench
87+
}
88+
}
89+
7990
/// Sets the session cookie
8091
#[derive(Parser, Debug)]
8192
pub struct Credentials {
8293
set: Option<String>,
8394
}
8495

8596
/// Downloads the input for today (or a given day)
86-
#[derive(Parser, Debug)]
97+
#[derive(Parser, Debug, Clone, Copy)]
8798
pub struct Input {
8899
/// Specifies the day. Defaults to today's date.
89100
#[clap(short, long)]
@@ -102,20 +113,29 @@ pub struct Input {
102113
generate: bool,
103114
}
104115

116+
impl Input {
117+
/// Cascade --day from the root command to the bench command
118+
fn cascade(&self, cli: &Cli) -> Self {
119+
let mut input = (*self).clone();
120+
input.day = self.day.or(cli.day);
121+
input
122+
}
123+
}
124+
105125
fn main() {
106126
let cli = Cli::parse_from(args_without_aoc());
107127

108-
let Some(subcommand) = cli.subcmd else {
128+
let Some(ref subcommand) = cli.subcmd else {
109129
return execute_default(&cli).unwrap();
110130
};
111131

112132
match subcommand {
113-
SubCommands::Bench(arg) => execute_bench(&arg),
133+
SubCommands::Bench(arg) => execute_bench(&arg.cascade(&cli)),
114134
SubCommands::Credentials(arg) => {
115135
execute_credentials(&arg);
116136
Ok(())
117137
}
118-
SubCommands::Input(arg) => execute_input(&arg),
138+
SubCommands::Input(arg) => execute_input(&arg.cascade(&cli)),
119139
}
120140
.unwrap()
121141
}

0 commit comments

Comments
 (0)