You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expected behavior: command flags should be allowed in front of the commands, even without =true.
Actual behavior: persistent boolean flags put in front of the command are not recognized.
funcTest_flagsOrder(t*testing.T) {
rootCommand:=&cobra.Command{SilenceUsage: true, Args: cobra.MaximumNArgs(0)}
varflagValueboolrootCommand.PersistentFlags().BoolVar(&flagValue, "rootflag", false, "root boolean flag")
cmd:=&cobra.Command{Use: "command", SilenceUsage: true, Args: cobra.MaximumNArgs(0)}
cmd.PersistentFlags().BoolVar(&flagValue, "cmdflag", false, "command boolean flag")
rootCommand.AddCommand(cmd)
forname, args:=rangemap[string][]string{
"root flag before command": {"--rootflag", "command"}, // ok"command flag before command": {"--cmdflag", "command"}, // unknown flag: --cmdflag"command flag with =value before command": {"--cmdflag=true", "command"}, // ok
} {
t.Run(name, func(t*testing.T) {
flagValue=falserootCommand.SetArgs(args)
iferr:=rootCommand.Execute(); err!=nil {
t.Error(err)
}
if!flagValue {
t.Errorf("flag is false")
}
})
}
}
Output:
-- FAIL: Test_flagsOrder (0.00s)
--- FAIL: Test_flagsOrder/command_flag_before_command (0.00s)
flags_test.go:27: unknown flag: --cmdflag
flags_test.go:30: flag is false
Hmm this does look like a bug: my expectation would be that persistent flags for commands should be accessible regardless of position. This will require some thought since I'm not sure when this was introduced and the impact fixing this would have.
Expected behavior: command flags should be allowed in front of the commands, even without
=true
.Actual behavior: persistent boolean flags put in front of the command are not recognized.
Output:
The text was updated successfully, but these errors were encountered: