use crate::command::NewCommand;
use camino::Utf8PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum Log {
Wasm,
Server,
}
#[derive(Debug, Clone, Parser, PartialEq, Default)]
pub struct Opts {
#[arg(short, long)]
pub release: bool,
#[arg(short = 'P', long)]
pub precompress: bool,
#[arg(long)]
pub hot_reload: bool,
#[arg(short, long)]
pub project: Option<String>,
#[arg(long)]
pub features: Vec<String>,
#[arg(long)]
pub lib_features: Vec<String>,
#[arg(long)]
pub lib_cargo_args: Option<Vec<String>>,
#[arg(long)]
pub bin_features: Vec<String>,
#[arg(long)]
pub bin_cargo_args: Option<Vec<String>>,
#[arg(long)]
pub wasm_debug: bool,
#[arg(short, action = clap::ArgAction::Count)]
pub verbose: u8,
#[arg(long, short)]
pub clear: bool,
#[arg(long, default_value = "true", value_parser=clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
pub js_minify: bool,
#[arg(long)]
pub split: bool,
#[arg(long)]
pub frontend_only: bool,
#[arg(long, conflicts_with = "frontend_only")]
pub server_only: bool,
}
#[derive(Debug, Clone, Parser, PartialEq, Default)]
pub struct BinOpts {
#[command(flatten)]
opts: Opts,
#[arg(trailing_var_arg = true)]
bin_args: Vec<String>,
}
#[derive(Debug, Clone, Parser, PartialEq, Default)]
pub struct TestSpecificOpts {
#[arg(long)]
pub no_run: bool,
}
impl TestSpecificOpts {
pub fn to_args(&self) -> Vec<String> {
let mut args = Vec::new();
if self.no_run {
args.push("--no-run".to_string());
}
args
}
}
#[derive(Debug, Clone, Parser, PartialEq, Default)]
pub struct TestOpts {
#[command(flatten)]
opts: Opts,
#[command(flatten, next_help_heading = "Test-specific Options")]
pub opts_specific: TestSpecificOpts,
}
#[derive(Debug, Parser)]
#[clap(version)]
pub struct Cli {
#[arg(long)]
pub manifest_path: Option<Utf8PathBuf>,
#[arg(long)]
pub log: Vec<Log>,
#[command(subcommand)]
pub command: Commands,
}
impl Cli {
pub fn opts(&self) -> Option<Opts> {
match &self.command {
Commands::New(_) => None,
Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => Some(bin_opts.opts.clone()),
Commands::Test(test_opts) => Some(test_opts.opts.clone()),
Commands::Build(opts) | Commands::EndToEnd(opts) => Some(opts.clone()),
_ => None,
}
}
pub fn opts_mut(&mut self) -> Option<&mut Opts> {
match &mut self.command {
Commands::New(_) => None,
Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => Some(&mut bin_opts.opts),
Commands::Test(test_opts) => Some(&mut test_opts.opts),
Commands::Build(opts) | Commands::EndToEnd(opts) => Some(opts),
_ => None,
}
}
pub fn bin_args(&self) -> Option<&[String]> {
match &self.command {
Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => {
Some(bin_opts.bin_args.as_ref())
}
_ => None,
}
}
}
#[derive(Debug, Subcommand, PartialEq)]
pub enum Commands {
Build(Opts),
Test(TestOpts),
EndToEnd(Opts),
Serve(BinOpts),
Watch(BinOpts),
New(NewCommand),
Completions { shell: Shell },
}