use std::path::PathBuf;
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
use dekopon_core::{AgentId, CapabilityId, ProviderId};
#[derive(Clone, Debug, Parser)]
#[command(
name = "dekopon",
version,
about = "Capability-oriented control plane for self-hosted AI agents",
long_about = None,
propagate_version = true
)]
pub struct Cli {
#[arg(long, global = true, value_name = "PATH")]
pub config: Option<PathBuf>,
#[arg(
short = 'o',
long = "output",
global = true,
value_enum,
default_value_t = OutputFormat::Table,
value_name = "FORMAT"
)]
pub output: OutputFormat,
#[arg(long, global = true)]
pub no_color: bool,
#[arg(long, global = true, conflicts_with = "verbose")]
pub quiet: bool,
#[arg(
short = 'v',
global = true,
action = ArgAction::Count,
conflicts_with = "quiet"
)]
pub verbose: u8,
#[command(subcommand)]
pub command: Command,
}
#[derive(Clone, Debug, Subcommand)]
pub enum Command {
Version,
Auth {
#[command(subcommand)]
account: AuthCommand,
},
Get {
#[command(subcommand)]
resource: GetCommand,
},
Describe {
#[command(subcommand)]
resource: DescribeCommand,
},
Validate,
Config {
#[command(subcommand)]
command: ConfigCommand,
},
}
#[derive(Clone, Debug, Subcommand)]
pub enum AuthCommand {
#[command(name = "chatgpt")]
ChatGpt {
#[command(subcommand)]
command: ChatGptAuthCommand,
},
}
#[derive(Clone, Debug, Subcommand)]
pub enum ChatGptAuthCommand {
Login {
#[arg(long, value_name = "PATH")]
auth_file: Option<PathBuf>,
},
Status {
#[arg(long, value_name = "PATH")]
auth_file: Option<PathBuf>,
},
Logout {
#[arg(long, value_name = "PATH")]
auth_file: Option<PathBuf>,
},
}
#[derive(Clone, Debug, Subcommand)]
pub enum GetCommand {
Agent {
name: AgentId,
},
Agents,
Capability {
name: CapabilityId,
},
Capabilities,
Provider {
name: ProviderId,
},
Providers,
}
#[derive(Clone, Debug, Subcommand)]
pub enum DescribeCommand {
Agent {
name: AgentId,
},
}
#[derive(Clone, Debug, Subcommand)]
pub enum ConfigCommand {
View,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
#[value(rename_all = "lower")]
pub enum OutputFormat {
Table,
Wide,
Json,
Yaml,
Name,
}
#[cfg(test)]
mod tests {
use clap::{CommandFactory, Parser};
use super::{AuthCommand, ChatGptAuthCommand, Cli, Command, GetCommand, OutputFormat};
#[test]
fn clap_definition_is_internally_consistent() {
Cli::command().debug_assert();
}
#[test]
fn parses_chatgpt_auth_commands() {
let cli = Cli::try_parse_from([
"dekopon",
"auth",
"chatgpt",
"status",
"--auth-file",
"auth.json",
])
.expect("valid auth command");
assert!(matches!(
cli.command,
Command::Auth {
account: AuthCommand::ChatGpt {
command: ChatGptAuthCommand::Status { .. }
}
}
));
}
#[test]
fn parses_global_output_after_nested_command() {
let cli = Cli::try_parse_from(["dekopon", "get", "agents", "-o", "wide"])
.expect("valid command line");
assert_eq!(cli.output, OutputFormat::Wide);
assert!(matches!(
cli.command,
Command::Get {
resource: GetCommand::Agents
}
));
}
}