use std::io::{Read, stdin};
use clap::{Parser, Subcommand};
use color_eyre::eyre::Context;
use crate::{data::GameField, handlers::{get, list}};
#[derive(Parser, Debug)]
#[command(version, about)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Subcommand)]
pub enum Command {
#[command()]
List(list::Command),
#[command()]
Get(get::Command),
}
#[derive(Debug, clap::Args)]
pub struct IdentifyGameArgs {
#[arg()]
pub game_title: Option<String>,
}
impl IdentifyGameArgs {
#[inline]
pub fn get_title(self) -> color_eyre::Result<String> {
let title = if self.game_title.as_ref().is_none_or(String::is_empty) {
let mut t = String::new();
let mut stdin = stdin().lock();
stdin
.read_to_string(&mut t)
.context("failed to read game title from STDIN")?;
t
}
else {
self.game_title.expect("title should exist at this stage")
};
Ok(title.trim().to_lowercase())
}
}
#[derive(Debug, Default, clap::Args)]
pub struct GameDetailArgs {
#[arg(short, long, action, conflicts_with = "delimiter")]
pub json: bool,
#[arg(short, long, conflicts_with = "json")]
pub delimiter: Option<String>,
#[arg(short, long, conflicts_with = "json", default_values = ["title", "launch-command"],
value_delimiter = ',', num_args = 1..)]
pub fields: Vec<GameField>,
}