Skip to content

How to get the value of the BoolVar to be false in this case  #613

Closed
@researchlab

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 ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions