diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 347eab76..8620465a 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -25,7 +25,7 @@ import ( func init() { addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)") addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command") - addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, "the arguments to auto generate format is 'flagName:type:description'") + addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`) } var packageName, parentName string @@ -136,7 +136,7 @@ import ( {{ printFlagVars .flags }} func init() { - {{ printFlagCreates .flags }} + {{ printFlagCreates .flags false }} {{.parentName}}.AddCommand({{.cmdName}}Cmd) } diff --git a/cobra/cmd/flag_helpers.go b/cobra/cmd/flag_helpers.go index 89d35b9b..9120be94 100644 --- a/cobra/cmd/flag_helpers.go +++ b/cobra/cmd/flag_helpers.go @@ -37,13 +37,18 @@ func printFlagVars(iFlags interface{}) string { return varDefs } -func printFlagCreates(iFlags interface{}) string { +func printFlagCreates(iFlags interface{}, persistent bool) string { flags := toFlagArray(iFlags) createStrs := "" + flagsFn := "Flags" + if persistent { + flagsFn = "PersistentFlags" + } for _, f := range flags { - createStrs += fmt.Sprintf( `%sCmd.Flags().%s(&%s, "%s",%s "%s") + createStrs += fmt.Sprintf( `%sCmd.%s().%s(&%s, "%s",%s "%s") `, f.CmdName, + flagsFn, f.CreateFn, f.VarName, f.Name, diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index 5b795a13..27baf31e 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -23,6 +23,10 @@ import ( "github.com/spf13/viper" ) +func init() { + initCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`) +} + var initCmd = &cobra.Command{ Use: "init [name]", Aliases: []string{"initialize", "initialise", "create"}, @@ -150,6 +154,24 @@ import ( var cfgFile string{{end}} +{{ printFlagVars .flags }} + +func init() { {{- if .viper}} + cobra.OnInitialize(initConfig) +{{end}} + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application.{{ if .viper }} + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }} + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }} + + // Cobra also supports local flags, which will only run + // when this action is called directly. + + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + {{ printFlagCreates .flags true }} +} + // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "{{.appName}}", @@ -173,20 +195,7 @@ func Execute() { os.Exit(1) } } - -func init() { {{- if .viper}} - cobra.OnInitialize(initConfig) -{{end}} - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application.{{ if .viper }} - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }} - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }} - - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -}{{ if .viper }} +{{ if .viper }} // initConfig reads in config file and ENV variables if set. func initConfig() { @@ -221,6 +230,16 @@ func initConfig() { data["license"] = project.License().Header data["appName"] = path.Base(project.Name()) + flags := []flagDefinition{} + for _, value := range cmdFlags { + f, err := buildFlag(value, "rootCmd") + if err != nil { + er(err) + } + flags = append(flags, f) + } + data["flags"] = flags + rootCmdScript, err := executeTemplate(template, data) if err != nil { er(err) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index f63fef52..39dbf008 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -45,7 +45,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project") - rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") + rootCmd.PersistentFlags().Bool("viper", false, "use Viper for configuration") viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")) viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")) viper.SetDefault("author", "NAME HERE ")