Cobra: Value local boolean flag not set

Created on 29 Oct 2017  路  2Comments  路  Source: spf13/cobra

Hi,
Trying to set a local boolean flag 'prompt', but the value is not set,
for the local integer flag 'times' the value is set.
What I am doing wrong ?

````
package cmd
import (
"github.com/spf13/cobra"
"fmt"
)

var myCommand = &cobra.Command{
Use: "new 'application'",
Short: "Create something new",
Long: "",
Run : runmyCommand,
}

var prompt bool
var times int

func init() {
myCommand.Flags().BoolVarP(&prompt, "prompt", "p", true, "Prompt user")
myCommand.Flags().IntVarP(&times, "times", "t", 1, "test local flag")
RootCmd.AddCommand(myCommand)
}

func runmyCommand(cmd *cobra.Command, args []string) {

fmt.Printf("value of prompt %v (is always true)\n",prompt)
fmt.Printf("value of times %v\n",times)
}
````

areflags kinquestion

Most helpful comment

I understand, that was not clear from the README, for flags taking integers I can specify
'--times 2' instead of '--times=2'

All 2 comments

Hello. If you don't provide flag from CLI, it will always be set to default value.
User should explicitly provide false value to this flag to set it false, i.e. `--prompt=false', otherwise it will be always true.

I understand, that was not clear from the README, for flags taking integers I can specify
'--times 2' instead of '--times=2'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

supershal picture supershal  路  4Comments

icholy picture icholy  路  6Comments

zevdg picture zevdg  路  5Comments

umbynos picture umbynos  路  5Comments

phanirithvij picture phanirithvij  路  5Comments