advent-of-code/src/main.rs

99 lines
2.8 KiB
Rust
Raw Normal View History

use advent_of_code::template::commands::{all, download, read, scaffold, solve};
use args::{parse, AppArguments};
2021-12-29 14:12:01 +01:00
2023-10-21 21:08:46 +02:00
mod args {
use advent_of_code::template::Day;
2023-10-21 21:08:46 +02:00
use std::process;
pub enum AppArguments {
2023-10-21 21:08:46 +02:00
Download {
day: Day,
2023-10-21 21:08:46 +02:00
},
Read {
day: Day,
2023-10-21 21:08:46 +02:00
},
Scaffold {
day: Day,
download: bool,
2023-10-21 21:08:46 +02:00
},
Solve {
day: Day,
2023-10-21 21:08:46 +02:00
release: bool,
time: bool,
submit: Option<u8>,
},
All {
release: bool,
time: bool,
},
}
2021-12-29 14:12:01 +01:00
pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
2023-10-21 21:08:46 +02:00
let mut args = pico_args::Arguments::from_env();
2021-12-29 19:14:40 +01:00
2023-10-21 21:08:46 +02:00
let app_args = match args.subcommand()?.as_deref() {
Some("all") => AppArguments::All {
2023-10-21 21:08:46 +02:00
release: args.contains("--release"),
time: args.contains("--time"),
},
Some("download") => AppArguments::Download {
2023-10-21 21:08:46 +02:00
day: args.free_from_str()?,
},
Some("read") => AppArguments::Read {
2023-10-21 21:08:46 +02:00
day: args.free_from_str()?,
},
Some("scaffold") => AppArguments::Scaffold {
2023-10-21 21:08:46 +02:00
day: args.free_from_str()?,
download: args.contains("--download"),
2023-10-21 21:08:46 +02:00
},
Some("solve") => AppArguments::Solve {
2023-10-21 21:08:46 +02:00
day: args.free_from_str()?,
release: args.contains("--release"),
submit: args.opt_value_from_str("--submit")?,
time: args.contains("--time"),
},
Some(x) => {
eprintln!("Unknown command: {x}");
2023-10-21 21:08:46 +02:00
process::exit(1);
}
None => {
eprintln!("No command specified.");
process::exit(1);
}
};
2021-12-29 19:14:40 +01:00
2023-10-21 21:08:46 +02:00
let remaining = args.finish();
if !remaining.is_empty() {
eprintln!("Warning: unknown argument(s): {remaining:?}.");
2023-10-21 21:08:46 +02:00
}
2021-12-29 19:14:40 +01:00
2023-10-21 21:08:46 +02:00
Ok(app_args)
}
}
2021-12-29 19:14:40 +01:00
2023-10-21 21:08:46 +02:00
fn main() {
match parse() {
2023-10-21 21:08:46 +02:00
Err(err) => {
eprintln!("Error: {err}");
2023-10-21 21:08:46 +02:00
std::process::exit(1);
}
Ok(args) => match args {
AppArguments::All { release, time } => all::handle(release, time),
AppArguments::Download { day } => download::handle(day),
AppArguments::Read { day } => read::handle(day),
AppArguments::Scaffold { day, download } => {
scaffold::handle(day);
if download {
download::handle(day);
}
}
AppArguments::Solve {
2023-10-21 21:08:46 +02:00
day,
release,
time,
submit,
} => solve::handle(day, release, time, submit),
2023-10-21 21:08:46 +02:00
},
};
2021-12-29 14:12:01 +01:00
}