mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
Allow both persistent and local flags for root vs command. Removed default viper. Allow flags to be set on the rootCmd as well.
This commit is contained in:
parent
bcc54ae73f
commit
b3a45b8407
4 changed files with 43 additions and 19 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 <EMAIL ADDRESS>")
|
||||
|
|
Loading…
Add table
Reference in a new issue