Closed
Description
I hope to get the false value, but always get true from toggle and tog boolean varaibles.
$ ./demo --toggle false --tog false
I hope get the following answer:
tog= false err=
toggle= false
But I got the following one:
tog= true err=
toggle= true
here is the demo code:
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var (
toggle bool
)
var RootCmd = &cobra.Command{
Use: "demo",
Run: func(cmd *cobra.Command, args []string) {
tog, err := cmd.Flags().GetBool("tog")
fmt.Println("tog=", tog, " err=", err)
fmt.Println("toggle=", toggle)
},
}
func init() {
RootCmd.PersistentFlags().BoolVarP(&toggle, "toggle", "t", false, "toggle bool")
RootCmd.Flags().BoolP("tog", "o", false, "tog bool")
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
is there any error in above codes ?
Metadata
Assignees
Labels
No labels
Activity