If defallt value of "cobra.Command BoolVarP" is "true", it is always "true"
Asked Answered
P

1

6

my code

    var require bool
    
    cmd.Flags().BoolVarP(&require, "require", "r", true,
        "default: true.")

If default value is "true", weather I add "-r" or not, "require" is always "true". But if default value is "false", it works well.

Please help, Thanks!

Parma answered 30/7, 2021 at 3:24 Comment(0)
C
12

If the default value of a boolean flag is true, then not passing that flag and just passing -r or --require will result in true. To set it to false, use --require=false.

Chenault answered 30/7, 2021 at 3:48 Comment(4)
Thanks,you are right. But I think it is not reasonable. If default value is false, I do not need to use =true. And in java, bose case works well.Parma
Default is used if the flag is not given. For boolean flags, --required and --required=true mean the same thing. If you set the default to true, --required=false or -r=false are the only ways to set it back to false. How else would you expect to do it?Chenault
Like in java, default value is true, if I use -r, value will be false. In another way, if use -r, value reversal.Parma
Note that -r is the same thing as --required.Chenault

© 2022 - 2024 — McMap. All rights reserved.