diff --git a/crates/ditto-cli/README.md b/crates/ditto-cli/README.md index e74862b06..0dfb1e9e0 100644 --- a/crates/ditto-cli/README.md +++ b/crates/ditto-cli/README.md @@ -5,7 +5,7 @@ $ ditto --help putting the fun in functional -Usage: ditto +Usage: ditto [COMMAND] Commands: bootstrap Bootstrap a new project diff --git a/crates/ditto-cli/src/main.rs b/crates/ditto-cli/src/main.rs index a8a925571..2ec92a5be 100644 --- a/crates/ditto-cli/src/main.rs +++ b/crates/ditto-cli/src/main.rs @@ -9,6 +9,7 @@ mod spinner; mod version; use clap::{ + arg, builder::{IntoResettable, Str}, ArgMatches, Command, }; @@ -31,8 +32,8 @@ fn command( .bin_name("ditto") .version(version_short) .long_version(version_long) + .arg(arg!(--"version-json").hide(true)) .disable_help_subcommand(true) - .subcommand_required(true) .about("putting the fun in functional") .subcommand(bootstrap::command(SUBCOMMAND_BOOTSTRAP).display_order(0)) .subcommand(make::command(SUBCOMMAND_MAKE).display_order(1)) @@ -65,7 +66,17 @@ async fn run(mut cmd: Command, matches: &ArgMatches, version: &Version) -> Resul .unwrap(); bootstrap::run(cmd, matches, version) } else { - unreachable!() + // Print JSON version information if called like + // `ditto --version-json` + if matches.get_flag("version-json") { + println!("{}", serde_json::to_string_pretty(version).unwrap()); + return Ok(()); + } + // Otherwise print help and exit + cmd.print_help().unwrap(); + std::process::exit(1) + // Or could do this... + // clap::Error::new(clap::error::ErrorKind::MissingSubcommand).with_cmd(&cmd).exit(); } } diff --git a/crates/ditto-cli/src/version.rs b/crates/ditto-cli/src/version.rs index bbdabda61..66ea43292 100644 --- a/crates/ditto-cli/src/version.rs +++ b/crates/ditto-cli/src/version.rs @@ -5,7 +5,7 @@ static GIT_DIRTY: &str = env!("GIT_DIRTY"); static BUILD_TIME: &str = env!("BUILD_TIME"); static PROFILE: &str = env!("PROFILE"); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, serde::Serialize)] pub struct Version { pub semversion: semver::Version, pub git_rev: String,