// Implements REQ-0037 (tool version reporting).
use anyhow::Result;
use serde_json::json;
use crate::cli::VersionArgs;
pub fn run(args: VersionArgs) -> Result<()> {
// Use the binary name ("req") so `req version` and clap's --version/-v/-V
// agree exactly. The crates.io package name (CARGO_PKG_NAME = "req-cli")
// is surfaced separately under the "package" field in JSON.
let bin_name = "req";
let version = env!("CARGO_PKG_VERSION");
let package = env!("CARGO_PKG_NAME");
// SemVer pre-release identifier sits after a `-`. Surface it as
// a separate field in the JSON form for tooling; the human-readable
// line stays exactly `req <version>` so it matches clap's
// --version / -v / -V output (REQ-0037 parity guarantee).
let is_prerelease = version.contains('-');
if args.json {
println!(
"{}",
serde_json::to_string_pretty(&json!({
"name": bin_name,
"package": package,
"version": version,
"prerelease": is_prerelease,
"file_format": crate::storage::FORMAT_TAG,
"mcp_protocol": "2024-11-05",
}))?
);
} else {
println!("{} {}", bin_name, version);
}
Ok(())
}