From 463b7068d18c896043d9e4ecd39f2f27c23ce4cb Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Mon, 6 Jan 2025 16:52:27 +0100 Subject: [PATCH] refactor(ant-cli): error when no subcommand given --- ant-cli/src/commands.rs | 9 +++++++-- ant-cli/src/opt.rs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ant-cli/src/commands.rs b/ant-cli/src/commands.rs index ff065a06c0..56d1c1ae99 100644 --- a/ant-cli/src/commands.rs +++ b/ant-cli/src/commands.rs @@ -12,7 +12,7 @@ mod vault; mod wallet; use crate::opt::Opt; -use clap::Subcommand; +use clap::{error::ErrorKind, CommandFactory as _, Subcommand}; use color_eyre::Result; #[derive(Subcommand, Debug)] @@ -230,6 +230,11 @@ pub async fn handle_subcommand(opt: Opt) -> Result<()> { WalletCmd::Export => wallet::export(), WalletCmd::Balance => wallet::balance().await, }, - None => Ok(()), + None => { + // If no subcommand is given, default to clap's error behaviour. + Opt::command() + .error(ErrorKind::InvalidSubcommand, "No subcommand given") + .exit(); + } } } diff --git a/ant-cli/src/opt.rs b/ant-cli/src/opt.rs index 9d7e4edd9b..c437829b2b 100644 --- a/ant-cli/src/opt.rs +++ b/ant-cli/src/opt.rs @@ -19,7 +19,7 @@ use std::time::Duration; #[command(disable_version_flag = true)] #[command(author, version, about, long_about = None)] pub(crate) struct Opt { - /// Available sub commands. + // Available subcommands. This is optional to allow `--version` to work without a subcommand. #[clap(subcommand)] pub command: Option,