use clap::{Parser, Subcommand};
use romm_api::client::RommClient;
use romm_api::config::Config;
use romm_api::error::RommError;
pub mod api;
pub mod auth;
pub mod cache;
pub mod completions;
pub mod download;
pub mod init;
pub mod library_scan;
pub mod platforms;
pub mod print;
pub mod roms;
pub mod scan;
pub mod sync;
pub mod update;
#[derive(Clone, Copy, Debug)]
pub enum OutputFormat {
Text,
Json,
}
impl OutputFormat {
pub fn from_flags(global_json: bool, local_json: bool) -> Self {
if global_json || local_json {
OutputFormat::Json
} else {
OutputFormat::Text
}
}
}
#[derive(Parser, Debug)]
#[command(
name = "romm-cli",
version,
about = "Rust CLI and TUI for the ROMM API",
infer_subcommands = true,
arg_required_else_help = true,
after_help = "Exit codes: 0 success, 1 general failure, 2 usage, 3 config/auth, 4 API/network.\n\
See docs/cli.md#exit-codes for scripting examples.\n\
JSON output shapes: docs/json-output.md"
)]
pub struct Cli {
#[arg(short, long, global = true)]
pub verbose: bool,
#[arg(long, global = true)]
pub json: bool,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
#[command(visible_alias = "setup")]
Init(init::InitCommand),
#[command(visible_alias = "call")]
Api(api::ApiCommand),
#[command(visible_aliases = ["platform", "p", "plats"])]
Platforms(platforms::PlatformsCommand),
#[command(visible_aliases = ["rom", "r"])]
Roms(Box<roms::RomsCommand>),
Scan(scan::ScanCommand),
Sync(sync::SyncCommand),
#[command(visible_aliases = ["dl", "get"])]
Download(download::DownloadCommand),
Cache(cache::CacheCommand),
Auth(auth::AuthCommand),
Update,
Completions(completions::CompletionsCommand),
}
pub async fn run(cli: Cli, config: Config) -> Result<(), RommError> {
let client = RommClient::new(&config, cli.verbose)?;
match cli.command {
Commands::Completions(_) => Err(RommError::Other(
"internal error: completions must be handled in main".into(),
)),
Commands::Init(_) => Err(RommError::Other(
"internal error: init must be handled before load_config".into(),
)),
command => crate::frontend::cli::run(command, &client, cli.json, cli.verbose).await,
}
}