Skip to content

Commit a7293fa

Browse files
committed
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 <[email protected]>
1 parent ccf017c commit a7293fa

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

cmd/limactl/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func newApp() *cobra.Command {
125125
// TODO: "survey" does not support using cygwin terminal on windows yet
126126
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.")
127127
rootCmd.PersistentFlags().BoolP("yes", "y", false, "Alias of --tty=false")
128+
rootCmd.MarkFlagsMutuallyExclusive("tty", "yes")
128129
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
129130
if err := processGlobalFlags(rootCmd); err != nil {
130131
return err
@@ -150,10 +151,6 @@ func newApp() *cobra.Command {
150151
return errors.New("must not run on NFS dir")
151152
}
152153

153-
if cmd.Flags().Changed("yes") && cmd.Flags().Changed("tty") {
154-
return errors.New("cannot use both --tty and --yes flags at the same time")
155-
}
156-
157154
if cmd.Flags().Changed("yes") {
158155
// Sets the value of the yesValue flag by using the yes flag.
159156
yesValue, _ := cmd.Flags().GetBool("yes")

0 commit comments

Comments
 (0)