spf13/cobra

Always got default value from flag

Ok9xNirab opened this issue · 1 comments

Code :

package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
)

var production bool

// getCmd represents the get command
var getCmd = &cobra.Command{
	Use:                "get [plugin_slug]",
	DisableFlagParsing: true,
	Short:              "Download all or single plugins",
	Long:               `Download all or single plugins`,
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 1 {
			return fmt.Errorf("please provide plugin slug")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(args, production)
	},
}

func init() {
	getCmd.Flags().BoolVarP(&production, "production", "p", false, "Get latest production ready zip")
	rootCmd.AddCommand(getCmd)
}

Result :

Screenshot 2023-10-27 at 6 19 20 PM

I'm tired and didn't find any solution.

Thanks.

Sorry, it's my bad . I didn't notice that i've use DisableFlagParsing: true inside &cobra.Command.