use std::io::Write;
use clap::{Args as ClapArgs, Subcommand};
use serde::Serialize;
use crate::context::GlobalArgs;
use crate::errors::CliError;
use crate::output::Format;
const GUIDE: &str = include_str!("context.md");
#[derive(Debug, ClapArgs)]
#[command(
about = "Resources for AI agents and automated tools.",
long_about = "Resources for AI agents and automated tools.\n\n\
Run `qn agent context` for a single, self-contained usage guide\n\
(auth, output formats, exit codes, confirmation, retry/idempotency,\n\
the command catalog, and common workflows)."
)]
pub struct Args {
#[command(subcommand)]
pub cmd: AgentCmd,
}
#[derive(Debug, Subcommand)]
pub enum AgentCmd {
Context,
}
pub async fn run(args: Args, global: GlobalArgs) -> Result<(), CliError> {
match args.cmd {
AgentCmd::Context => context(global),
}
}
#[derive(Serialize)]
struct ContextView<'a> {
version: &'a str,
guide: &'a str,
}
fn context(global: GlobalArgs) -> Result<(), CliError> {
let version = env!("CARGO_PKG_VERSION");
let guide = GUIDE.replace("{{VERSION}}", version);
match global.format {
Some(Format::Json) => {
let view = ContextView {
version,
guide: &guide,
};
let mut out = std::io::stdout().lock();
serde_json::to_writer_pretty(&mut out, &view)?;
writeln!(out)?;
}
other => {
print!("{guide}");
if matches!(other, Some(Format::Yaml | Format::Toon | Format::Table)) && !global.quiet {
let fmt = match other {
Some(Format::Yaml) => "yaml",
Some(Format::Toon) => "toon",
_ => "table",
};
let _ = writeln!(
std::io::stderr(),
"ℹ '-o {fmt}' isn't supported by 'qn agent context'; printing markdown. Use '-o json' for structured output."
);
}
}
}
Ok(())
}