use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use simulator::utils::consts::SDK_ENV_VAR;
use crate::device::query::Query;
pub fn parse() -> Cfg { Cfg::parse() }
#[derive(Parser, Debug)]
#[command(author, version, about, name = "pdtool")]
pub struct Cfg {
#[command(subcommand)]
pub cmd: Command,
#[clap(long, global = true, default_value_t = Format::Human)]
pub format: Format,
}
#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum Format {
Human,
Json,
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Format::Human => "human".fmt(f),
Format::Json => "json".fmt(f),
}
}
}
#[derive(Subcommand, Debug)]
pub enum Command {
List {
#[arg(default_value_t = DeviceKind::Any)]
kind: DeviceKind,
},
Mount {
#[command(flatten)]
query: Query,
#[arg(long, default_value_t = false)]
wait: bool,
},
Unmount {
#[command(flatten)]
query: Query,
#[arg(long, default_value_t = false)]
wait: bool,
},
Install(#[command(flatten)] Install),
Run(#[command(flatten)] run::Run),
Read(#[command(flatten)] Query),
Send(#[command(flatten)] Send),
#[cfg(debug_assertions)]
Debug(#[command(flatten)] Dbg),
}
#[derive(Clone, Debug, clap::Parser)]
pub struct Dbg {
#[clap(subcommand)]
pub cmd: DbgCmd,
#[command(flatten)]
pub query: Query,
}
#[derive(Debug, Clone, clap::Subcommand)]
pub enum DbgCmd {
Inspect,
Eject { path: PathBuf },
}
#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum DeviceKind {
Any,
Data,
Storage,
}
impl std::fmt::Display for DeviceKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DeviceKind::Any => "any",
DeviceKind::Data => "data",
DeviceKind::Storage => "storage",
}.fmt(f)
}
}
#[derive(Clone, Debug, clap::Parser)]
#[command(author, version, about, long_about = None, name = "install")]
pub struct Install {
#[arg(value_name = "PACKAGE")]
pub pdx: PathBuf,
#[arg(long, short, default_value_t = false)]
pub force: bool,
#[command(flatten)]
pub query: Query,
}
#[derive(Clone, Debug, clap::Parser)]
#[command(author, version, about, long_about = None, name = "send")]
pub struct Send {
#[clap(subcommand)]
pub command: device::device::command::Command,
#[command(flatten)]
pub query: Query,
#[arg(long, default_value_t = false)]
pub read: bool,
}
pub use run::*;
mod run {
use std::borrow::Cow;
use super::*;
#[derive(Clone, Debug, clap::Parser)]
#[command(author, version, about, long_about = None, name = "run")]
pub struct Run {
#[clap(subcommand)]
pub destination: Destination,
}
#[derive(Clone, Debug, clap::Subcommand)]
pub enum Destination {
#[clap(visible_alias("dev"))]
Device(Dev),
#[clap(visible_alias("sim"))]
Simulator(Sim),
}
impl std::fmt::Display for Destination {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name: Cow<str> = match self {
Destination::Device(Dev { install: Install { query, .. },
.. }) => format!("device:{query}").into(),
Destination::Simulator(_) => "simulator".into(),
};
name.fmt(f)
}
}
#[derive(Clone, Debug, clap::Parser)]
pub struct Sim {
#[arg(value_name = "PACKAGE")]
pub pdx: PathBuf,
#[arg(long, env = SDK_ENV_VAR, value_name = "DIRECTORY", value_hint = clap::ValueHint::DirPath)]
pub sdk: Option<PathBuf>,
}
#[derive(Clone, Debug, clap::Parser)]
pub struct Dev {
#[command(flatten)]
pub install: super::Install,
#[arg(long, name = "no-install", default_value_t = false)]
pub no_install: bool,
#[arg(long, name = "no-read", default_value_t = false)]
pub no_read: bool,
}
}