use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use crate::config::{ConfigOverrides, ConfigSetKey, OutputFormat};
use crate::error::CgdError;
#[derive(Debug, Parser)]
#[command(
name = "cgd",
version,
about = "Source-linked private-market data from Coragrid",
long_about = "The official Coragrid CLI. Explicit commands are deterministic and headless; stdout is data and stderr is diagnostics.",
disable_help_subcommand = true
)]
pub struct Cli {
#[arg(long, global = true)]
pub profile: Option<String>,
#[arg(long, global = true, value_enum)]
pub output: Option<OutputFormat>,
#[arg(long, global = true, conflicts_with = "output")]
pub json: bool,
#[arg(long, global = true)]
pub no_color: bool,
#[arg(long, global = true)]
pub quiet: bool,
#[arg(short, long, global = true, action = ArgAction::Count)]
pub verbose: u8,
#[arg(long, global = true)]
pub timeout: Option<String>,
#[arg(long, global = true, value_name = "URL")]
pub api_base_url: Option<String>,
#[arg(long, global = true)]
pub no_pager: bool,
#[command(subcommand)]
pub command: Option<Command>,
}
impl Cli {
pub fn config_overrides(&self) -> Result<ConfigOverrides, CgdError> {
Ok(ConfigOverrides {
profile: self.profile.clone(),
api_base_url: self.api_base_url.clone(),
output: if self.json {
Some(OutputFormat::Json)
} else {
self.output
},
timeout: self.timeout.clone(),
no_color: self.no_color,
})
}
}
#[derive(Debug, Subcommand)]
pub enum Command {
Auth(AuthCommand),
Config(ConfigCommand),
Resolve(ResolveArgs),
Companies(CompaniesCommand),
Documents(DocumentsCommand),
Tui(TuiArgs),
Completions(CompletionsArgs),
Version,
}
#[derive(Debug, Args)]
pub struct AuthCommand {
#[command(subcommand)]
pub command: AuthSubcommand,
}
#[derive(Debug, Subcommand)]
pub enum AuthSubcommand {
Login,
Logout,
Status,
}
#[derive(Debug, Args)]
pub struct ConfigCommand {
#[command(subcommand)]
pub command: ConfigSubcommand,
}
#[derive(Debug, Subcommand)]
pub enum ConfigSubcommand {
List,
Get {
#[arg(value_enum)]
key: ConfigKey,
},
Set {
#[arg(value_enum)]
key: ConfigSetKey,
value: String,
},
Profiles,
Use { profile: String },
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ConfigKey {
Profile,
ApiBaseUrl,
Workspace,
Output,
Timeout,
NoColor,
CredentialSource,
CredentialRef,
ConfigPath,
}
#[derive(Debug, Args)]
pub struct ResolveArgs {
pub query: String,
#[arg(long, value_name = "ISO2")]
pub country: Option<String>,
#[arg(long)]
pub domain: Option<String>,
#[arg(long, value_name = "SCHEME:VALUE")]
pub identifier: Vec<String>,
#[arg(long, default_value_t = 5)]
pub limit: u16,
#[arg(long)]
pub require_unique: bool,
}
#[derive(Debug, Args)]
pub struct CompaniesCommand {
#[command(subcommand)]
pub command: CompaniesSubcommand,
}
#[derive(Debug, Subcommand)]
pub enum CompaniesSubcommand {
Search(CompanySearchArgs),
Get(CompanyGetArgs),
Officers(CompanyOfficersArgs),
}
#[derive(Debug, Args)]
pub struct CompanyGetArgs {
pub company_id: String,
}
#[derive(Debug, Args)]
pub struct CompanyOfficersArgs {
pub company_id: String,
#[arg(long, default_value_t = 100, value_parser = clap::value_parser!(u16).range(1..=200))]
pub limit: u16,
}
#[derive(Debug, Args)]
pub struct DocumentsCommand {
#[command(subcommand)]
pub command: DocumentsSubcommand,
}
#[derive(Debug, Subcommand)]
pub enum DocumentsSubcommand {
Get(DocumentGetArgs),
}
#[derive(Debug, Args)]
pub struct DocumentGetArgs {
pub document_id: String,
#[arg(long, required = true)]
pub metadata: bool,
}
#[derive(Debug, Args)]
pub struct CompanySearchArgs {
#[arg(long)]
pub query: Option<String>,
#[arg(long, value_name = "ISO2")]
pub country: Vec<String>,
#[arg(long)]
pub status: Vec<String>,
#[arg(long = "type")]
pub company_type: Vec<String>,
#[arg(long, default_value_t = 25)]
pub limit: u16,
#[arg(long)]
pub cursor: Option<String>,
#[arg(long, requires = "max_results", conflicts_with = "cursor")]
pub all: bool,
#[arg(long, value_name = "N")]
pub max_results: Option<usize>,
#[arg(long, value_enum)]
pub financial_field: Option<FinancialFieldArg>,
#[arg(long)]
pub financial_period: Option<String>,
#[arg(long)]
pub financial_currency: Option<String>,
#[arg(long, allow_hyphen_values = true)]
pub financial_gte: Option<i64>,
#[arg(long, allow_hyphen_values = true)]
pub financial_lte: Option<i64>,
#[arg(long, value_enum, default_value = "company")]
pub financial_scope: ReportingScopeArg,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum FinancialFieldArg {
Revenue,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ReportingScopeArg {
Company,
Group,
}
#[derive(Debug, Args)]
pub struct TuiArgs {
#[arg(long)]
pub no_mouse: bool,
#[arg(long, value_enum, default_value = "dark")]
pub theme: ThemeArg,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ThemeArg {
Dark,
Light,
Mono,
}
#[derive(Debug, Args)]
pub struct CompletionsArgs {
#[arg(value_enum)]
pub shell: Shell,
}
#[cfg(test)]
mod tests {
use clap::Parser;
use super::*;
#[test]
fn explicit_data_commands_parse_without_tty_assumptions() {
let cli = Cli::try_parse_from([
"cgd",
"--output",
"jsonl",
"companies",
"search",
"--query",
"Nordic automation",
"--country",
"FI",
"--country",
"SE",
"--limit",
"25",
])
.unwrap();
assert!(matches!(
cli.command,
Some(Command::Companies(CompaniesCommand {
command: CompaniesSubcommand::Search(_)
}))
));
assert_eq!(cli.output, Some(OutputFormat::Jsonl));
}
#[test]
fn credentials_are_not_accepted_as_command_line_arguments() {
let error =
Cli::try_parse_from(["cgd", "--api-key", "secret", "auth", "status"]).unwrap_err();
assert_eq!(error.exit_code(), 2);
assert!(!error.to_string().contains("secret"));
}
#[test]
fn document_get_requires_an_explicit_metadata_mode() {
assert!(
Cli::try_parse_from(["cgd", "documents", "get", "doc_014d2pf2dbsqqg28t5cy4tqkff"])
.is_err()
);
assert!(Cli::try_parse_from([
"cgd",
"documents",
"get",
"doc_014d2pf2dbsqqg28t5cy4tqkff",
"--metadata"
])
.is_ok());
}
}