use clap::{Args, Parser, Subcommand};
#[derive(Args, Clone)]
pub struct ListArgs {
#[arg(long, default_value = "100")]
pub limit: usize,
#[arg(long, default_value = "0")]
pub offset: usize,
#[arg(long)]
pub fields: Option<String>,
}
#[derive(Parser)]
#[command(
about = "CLI for managing Shelly devices. Run 'shelly schema' for machine-readable introspection.",
version,
after_long_help = "\
Examples:
shelly discover --subnet 192.168.1.0/24
shelly on \"Kitchen Light\"
shelly on \"Office Strip\" --id 1
shelly status -n \"Living Room\"
shelly power -a
shelly energy -a
shelly health
shelly watch
shelly -g lights off"
)]
pub struct Cli {
#[arg(long, global = true)]
pub host: Option<String>,
#[arg(long, short = 'n', global = true)]
pub name: Option<String>,
#[arg(long, short = 'g', global = true)]
pub group: Option<String>,
#[arg(long, short = 'o', global = true, default_value = "auto",
value_parser = ["auto", "text", "json"])]
pub output: String,
#[arg(long, short = 'j', global = true, hide = true)]
pub json: bool,
#[arg(long, short = 'q', global = true)]
pub quiet: bool,
#[arg(long, short = 'p', global = true)]
pub password: Option<String>,
#[arg(long, global = true, default_value = "3000")]
pub timeout: u64,
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand)]
pub enum Command {
Discover {
#[arg(long)]
subnet: Option<String>,
},
Devices {
#[arg(long)]
refresh: bool,
#[command(flatten)]
list: ListArgs,
},
Status {
#[arg(long, short = 'a')]
all: bool,
#[command(flatten)]
list: ListArgs,
},
Switch {
#[command(subcommand)]
action: SwitchAction,
},
Light {
#[command(subcommand)]
action: LightAction,
},
On {
device: Option<String>,
#[arg(long, default_value = "0")]
id: u8,
},
Off {
device: Option<String>,
#[arg(long, default_value = "0")]
id: u8,
},
Toggle {
device: Option<String>,
#[arg(long, default_value = "0")]
id: u8,
},
Power {
#[arg(long, short = 'a')]
all: bool,
#[arg(long, default_value = "0")]
id: u8,
},
Energy {
#[arg(long, short = 'a')]
all: bool,
},
Firmware {
#[command(subcommand)]
action: FirmwareAction,
},
Config {
#[command(subcommand)]
action: ConfigAction,
},
Group {
#[command(subcommand)]
action: GroupAction,
},
Schedule {
#[command(subcommand)]
action: ScheduleAction,
},
Webhook {
#[command(subcommand)]
action: WebhookAction,
},
Backup {
#[arg(long, short = 'a')]
all: bool,
#[arg(long)]
dir: Option<String>,
},
Restore {
file: String,
#[arg(long, short = 'y')]
yes: bool,
},
Rename {
new_name: String,
#[arg(long, short = 'y')]
yes: bool,
},
Reboot {
#[arg(long, short = 'y')]
yes: bool,
},
Watch {
#[arg(long, default_value = "2")]
interval: u64,
},
Info,
Health,
Schema,
Capabilities,
Completions {
shell: clap_complete::Shell,
},
#[command(name = "_complete-device-names", hide = true)]
CompleteDeviceNames,
#[command(name = "_complete-group-names", hide = true)]
CompleteGroupNames,
}
#[derive(Subcommand, Clone)]
pub enum SwitchAction {
Status {
#[arg(long, default_value = "0")]
id: u8,
},
On {
#[arg(long, default_value = "0")]
id: u8,
},
Off {
#[arg(long, default_value = "0")]
id: u8,
},
Toggle {
#[arg(long, default_value = "0")]
id: u8,
},
}
#[derive(Args, Clone)]
pub struct LightSetArgs {
#[arg(long, default_value = "0")]
pub id: u8,
#[arg(long, conflicts_with = "rgb")]
pub color: Option<String>,
#[arg(long)]
pub rgb: Option<String>,
#[arg(long)]
pub brightness: Option<u8>,
#[arg(long)]
pub white: Option<u8>,
#[arg(long)]
pub temp: Option<u32>,
}
#[derive(Subcommand, Clone)]
pub enum LightAction {
Status {
#[arg(long, default_value = "0")]
id: u8,
},
On {
#[command(flatten)]
args: LightSetArgs,
},
Off {
#[arg(long, default_value = "0")]
id: u8,
},
Toggle {
#[arg(long, default_value = "0")]
id: u8,
},
Set {
#[command(flatten)]
args: LightSetArgs,
},
}
#[derive(Subcommand, Clone)]
pub enum FirmwareAction {
Check {
#[arg(long, short = 'a')]
all: bool,
},
Update {
#[arg(long, short = 'a')]
all: bool,
#[arg(long, short = 'y')]
yes: bool,
},
}
#[derive(Subcommand, Clone)]
pub enum ConfigAction {
Get {
#[arg(long, short = 'a')]
all: bool,
},
Set {
key: String,
value: String,
},
}
#[derive(Subcommand, Clone)]
pub enum ScheduleAction {
List {
#[arg(long, short = 'a')]
all: bool,
#[command(flatten)]
list: ListArgs,
},
}
#[derive(Subcommand, Clone)]
pub enum WebhookAction {
List {
#[arg(long, short = 'a')]
all: bool,
#[command(flatten)]
list: ListArgs,
},
}
#[derive(Subcommand, Clone)]
pub enum GroupAction {
List {
#[command(flatten)]
list: ListArgs,
},
Add {
name: String,
#[arg(required = true)]
devices: Vec<String>,
},
Remove {
name: String,
#[arg(long, short = 'y')]
yes: bool,
},
Show {
name: String,
},
}