From 5b5072a82acd3c07a27b925c099e97b8a3481970 Mon Sep 17 00:00:00 2001 From: I Hsin Cheng Date: Mon, 6 Oct 2025 15:06:40 +0800 Subject: [PATCH] Early flags check before "PersistentPreRunE" Utilize Cobra's "MarkFlagsMutuallyExclusive()" on root command. This shifts validation to flag parsing and remove the manual check from "PersistentPreRunE", which can be applied to all subcommands before hooks fire. Signed-off-by: I Hsin Cheng --- cmd/limactl/main.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/limactl/main.go b/cmd/limactl/main.go index 874a4a10165..5b255394414 100644 --- a/cmd/limactl/main.go +++ b/cmd/limactl/main.go @@ -125,6 +125,7 @@ func newApp() *cobra.Command { // TODO: "survey" does not support using cygwin terminal on windows yet rootCmd.PersistentFlags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "Enable TUI interactions such as opening an editor. Defaults to true when stdout is a terminal. Set to false for automation.") rootCmd.PersistentFlags().BoolP("yes", "y", false, "Alias of --tty=false") + rootCmd.MarkFlagsMutuallyExclusive("tty", "yes") rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { if err := processGlobalFlags(rootCmd); err != nil { return err @@ -150,10 +151,6 @@ func newApp() *cobra.Command { return errors.New("must not run on NFS dir") } - if cmd.Flags().Changed("yes") && cmd.Flags().Changed("tty") { - return errors.New("cannot use both --tty and --yes flags at the same time") - } - if cmd.Flags().Changed("yes") { // Sets the value of the yesValue flag by using the yes flag. yesValue, _ := cmd.Flags().GetBool("yes")