use std::io;
use clap::{Command, CommandFactory, Parser, Subcommand, ValueHint};
use clap_complete::{Generator, Shell, generate};
use colored::Colorize;
const DEFAULT_ITERATIONS_COUNT: usize = 5;
#[derive(Parser)] #[command(name = "cpast", version, author, about, long_about = None)]
#[command(bin_name = "cpast")]
pub(crate) struct CpastCommand {
#[arg(long = "completions", value_enum)]
completions: Option<Shell>,
#[command(subcommand)]
pub(crate) subcommand: Option<CpastSubcommands>,
}
#[derive(Subcommand)] pub(crate) enum CpastSubcommands {
#[command(author)]
Test(TestArgs),
#[command(author)]
Generate(GenerateArgs),
Ai(AiArgs),
}
fn print_completions<G: Generator>(generator: G, cmd: &mut Command) {
generate(
generator,
cmd,
cmd.get_name().to_string(),
&mut io::stdout(),
);
}
#[derive(clap::Args)]
pub(crate) struct TestArgs {
#[arg(short, long, value_hint = ValueHint::FilePath)]
pub(crate) correct_file: Option<String>,
#[arg(short, long, required = true, value_hint = ValueHint::FilePath)]
pub(crate) test_file: Option<String>,
#[arg(short, long, value_hint = ValueHint::Other)]
pub(crate) generator: Option<String>,
#[arg(short='u', long, value_hint = ValueHint::Url)]
pub(crate) problem_url: Option<String>,
#[arg(short, long, default_value_t = DEFAULT_ITERATIONS_COUNT, value_hint = ValueHint::Other)]
pub(crate) iterations: usize,
#[arg(short, long)]
pub(crate) no_stop: bool,
#[arg(short, long)]
pub(crate) force_recompile: bool,
#[arg(short, long)]
pub(crate) debug: bool,
}
#[derive(clap::Args)]
pub(crate) struct GenerateArgs {
pub(crate) generator: Option<String>,
#[arg(short, long)]
pub(crate) clipboard: bool,
}
#[derive(clap::Args)]
pub(crate) struct AiArgs {
#[arg(short, long, value_hint = ValueHint::Other)]
pub(crate) input_format: Option<String>,
#[arg(short, long, value_hint = ValueHint::Other)]
pub(crate) constraints: Option<String>,
#[arg(short='u', long, value_hint = ValueHint::Url)]
pub(crate) problem_url: Option<String>,
#[arg(long)]
pub(crate) clipboard: bool,
}
impl CpastCommand {
pub(crate) fn new() -> Option<Self> {
let opt = Self::parse();
if let Some(completions) = opt.completions {
let mut cmd = CpastCommand::command();
eprintln!("Generating completion file for {completions:?}...");
print_completions(completions, &mut cmd);
match completions {
Shell::Zsh => {
eprintln!("\n\n{}\n {}",
"Run the following command below to add it permanently to your shell:".bright_blue(),
"cpast --completions=zsh | sudo tee /usr/local/share/zsh/site-functions/_cpast".yellow()
);
}
Shell::Bash => {
eprintln!(
"\n\n{}\n {}",
"Run the following command below to add it permanently to your shell:"
.bright_blue(),
"cpast --completions=bash | sudo tee /etc/bash_completion.d/cpast.bash"
.yellow()
);
}
Shell::Fish => {
eprintln!("\n\n{}\n {}",
"Run the following command below to add it permanently to your shell:".bright_blue(),
"cpast --completions=fish > ~/.local/share/fish/generated_completions/cpast.fish".yellow()
);
}
Shell::PowerShell => {
eprintln!(
"{}\n {}",
"Run the following command below to add it permanently to your shell:"
.bright_blue(),
"cpast --completions=powershell | Out-File -FilePath $PROFILE -Append"
.yellow()
);
}
_ => {}
}
None
} else {
Some(opt)
}
}
}