1use anyhow::Result;
8use clap::{Parser, Subcommand};
9
10use crate::client::RommClient;
11use crate::config::Config;
12
13pub mod api;
14pub mod cache;
15pub mod download;
16pub mod init;
17pub mod platforms;
18pub mod print;
19pub mod roms;
20pub mod update;
21
22#[derive(Clone, Copy, Debug)]
24pub enum OutputFormat {
25 Text,
27 Json,
29}
30
31impl OutputFormat {
32 pub fn from_flags(global_json: bool, local_json: bool) -> Self {
34 if global_json || local_json {
35 OutputFormat::Json
36 } else {
37 OutputFormat::Text
38 }
39 }
40}
41
42#[derive(Parser, Debug)]
48#[command(
49 name = "romm-cli",
50 version,
51 about = "Rust CLI and TUI for the ROMM API",
52 infer_subcommands = true,
53 arg_required_else_help = true
54)]
55pub struct Cli {
56 #[arg(short, long, global = true)]
58 pub verbose: bool,
59
60 #[arg(long, global = true)]
62 pub json: bool,
63
64 #[command(subcommand)]
65 pub command: Commands,
66}
67
68#[derive(Subcommand, Debug)]
70pub enum Commands {
71 #[command(visible_alias = "setup")]
73 Init(init::InitCommand),
74 #[cfg(feature = "tui")]
76 Tui,
77 #[cfg(not(feature = "tui"))]
79 Tui,
80 #[command(visible_alias = "call")]
82 Api(api::ApiCommand),
83 #[command(visible_aliases = ["platform", "p", "plats"])]
85 Platforms(platforms::PlatformsCommand),
86 #[command(visible_aliases = ["rom", "r"])]
88 Roms(roms::RomsCommand),
89 #[command(visible_aliases = ["dl", "get"])]
91 Download(download::DownloadCommand),
92 Cache(cache::CacheCommand),
94 Update,
96}
97
98pub async fn run(cli: Cli, config: Config) -> Result<()> {
99 let client = RommClient::new(&config, cli.verbose)?;
100
101 match cli.command {
102 Commands::Init(_) => {
103 anyhow::bail!("internal error: init must be handled before load_config");
104 }
105 #[cfg(feature = "tui")]
106 Commands::Tui => {
107 anyhow::bail!("internal error: TUI must be started via run_interactive from main");
108 }
109 #[cfg(not(feature = "tui"))]
110 Commands::Tui => anyhow::bail!("this feature requires the tui"),
111 command => crate::frontend::cli::run(command, &client, cli.json).await?,
112 }
113
114 Ok(())
115}