Open
Description
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.
func Test_flagsOrder(t *testing.T) {
rootCommand := &cobra.Command{SilenceUsage: true, Args: cobra.MaximumNArgs(0)}
var flagValue bool
rootCommand.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)
for name, args := range map[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 = false
rootCommand.SetArgs(args)
if err := 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
Moved here from spf13/pflag#421
Metadata
Metadata
Assignees
Labels
No labels