Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade options down from cargo aoc to cargo aoc <bench/input> #106

Open
wants to merge 1 commit into
base: v0.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions cargo-aoc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum SubCommands {
}

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

impl Bench {
/// Cascade --day/--part/--input from the root command to the bench command
fn cascade(&self, cli: &Cli) -> Self {
let mut bench = (*self).clone();
bench.day = self.day.or(cli.day);
bench.part = self.part.or(cli.part);
bench.input = bench.input.or_else(|| cli.input.clone());
bench
}
}

/// Sets the session cookie
#[derive(Parser, Debug)]
pub struct Credentials {
set: Option<String>,
}

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

impl Input {
/// Cascade --day from the root command to the bench command
fn cascade(&self, cli: &Cli) -> Self {
let mut input = (*self).clone();
input.day = self.day.or(cli.day);
input
}
}

fn main() {
let cli = Cli::parse_from(args_without_aoc());

let Some(subcommand) = cli.subcmd else {
let Some(ref subcommand) = cli.subcmd else {
return execute_default(&cli).unwrap();
};

match subcommand {
SubCommands::Bench(arg) => execute_bench(&arg),
SubCommands::Bench(arg) => execute_bench(&arg.cascade(&cli)),
SubCommands::Credentials(arg) => {
execute_credentials(&arg);
Ok(())
}
SubCommands::Input(arg) => execute_input(&arg),
SubCommands::Input(arg) => execute_input(&arg.cascade(&cli)),
}
.unwrap()
}