use clap::{Args, Parser, Subcommand, ValueEnum};
use clap_tui::Tui;
use std::path::PathBuf;
#[derive(Debug, Parser, PartialEq, Eq)]
#[command(
name = "showcase",
about = "A realistic clap-tui showcase CLI",
version = "0.1.0"
)]
enum Command {
Tui,
Build(BuildArgs),
Serve(ServeArgs),
Deploy(DeployArgs),
Config {
#[command(subcommand)]
command: ConfigCommand,
},
}
#[derive(Debug, Args, PartialEq, Eq)]
struct BuildArgs {
#[arg(short, long)]
release: bool,
#[arg(long, value_enum, default_value_t = BuildFormat::Binary)]
format: BuildFormat,
#[arg(long, default_value = "dist")]
output: String,
#[arg(value_hint = clap::ValueHint::DirPath)]
source: PathBuf,
}
#[derive(Debug, Args, PartialEq, Eq)]
struct ServeArgs {
#[arg(long, default_value = "127.0.0.1")]
host: String,
#[arg(long, default_value_t = 3000)]
port: u16,
#[arg(short, long)]
watch: bool,
#[arg(long, value_hint = clap::ValueHint::FilePath)]
config: Option<PathBuf>,
}
#[derive(Debug, Args, PartialEq, Eq)]
struct DeployArgs {
#[arg(long, value_enum)]
environment: Environment,
#[arg(value_enum)]
target: DeployTarget,
#[arg(short = 'n', long)]
dry_run: bool,
#[arg(long = "tag", short = 't')]
tags: Vec<String>,
}
#[derive(Debug, Subcommand, PartialEq, Eq)]
enum ConfigCommand {
Show,
Set(ConfigSetArgs),
}
#[derive(Debug, Args, PartialEq, Eq)]
struct ConfigSetArgs {
key: String,
value: String,
#[arg(long, value_enum, default_value_t = ConfigScope::Local)]
scope: ConfigScope,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
enum BuildFormat {
Binary,
Tarball,
Docker,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
enum Environment {
Staging,
Production,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
enum DeployTarget {
Web,
Worker,
Jobs,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
enum ConfigScope {
Local,
Shared,
Global,
}
fn dispatch(command: Command) {
match command {
Command::Tui => {}
other => println!("{other:#?}"),
}
}
fn main() -> Result<(), clap_tui::TuiError> {
match Command::parse() {
Command::Tui => {
if let Some(command) = Tui::<Command>::new().hide_entrypoint("tui")?.run()? {
dispatch(command);
}
}
command => dispatch(command),
}
Ok(())
}