use crate::Result;
use crate::cli::Cli;
use clap::CommandFactory;

/// Generates a usage spec for the CLI
///
/// https://usage.jdx.dev
#[derive(Debug, clap::Args)]
#[clap(hide = true, verbatim_doc_comment)]
pub struct Usage {}

impl Usage {
    pub async fn run(&self) -> Result<()> {
        let mut cmd = Cli::command();
        let mut spec = clap_usage::spec(&mut cmd, "hk");
        // Declare what each command does to the world; clap cannot express it.
        super::command_effects::apply(&mut spec);
        // 4.0 added flag-level `effect=`; older `usage` CLIs reject the spec with
        // "unsupported cmd prop effect".
        let usage = format!(
            "// @generated by usage-cli from clap metadata\nmin_usage_version \"4.0\"\n{spec}\n"
        ) + include_str!("../hk-extras.usage.kdl");
        println!("{}", usage.trim());
        Ok(())
    }
}
