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() {
|
func init() {
|
||||||
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")
|
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().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
|
var packageName, parentName string
|
||||||
|
@ -136,7 +136,7 @@ import (
|
||||||
{{ printFlagVars .flags }}
|
{{ printFlagVars .flags }}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
{{ printFlagCreates .flags }}
|
{{ printFlagCreates .flags false }}
|
||||||
{{.parentName}}.AddCommand({{.cmdName}}Cmd)
|
{{.parentName}}.AddCommand({{.cmdName}}Cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,13 +37,18 @@ func printFlagVars(iFlags interface{}) string {
|
||||||
return varDefs
|
return varDefs
|
||||||
}
|
}
|
||||||
|
|
||||||
func printFlagCreates(iFlags interface{}) string {
|
func printFlagCreates(iFlags interface{}, persistent bool) string {
|
||||||
flags := toFlagArray(iFlags)
|
flags := toFlagArray(iFlags)
|
||||||
createStrs := ""
|
createStrs := ""
|
||||||
|
flagsFn := "Flags"
|
||||||
|
if persistent {
|
||||||
|
flagsFn = "PersistentFlags"
|
||||||
|
}
|
||||||
for _, f := range flags {
|
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,
|
f.CmdName,
|
||||||
|
flagsFn,
|
||||||
f.CreateFn,
|
f.CreateFn,
|
||||||
f.VarName,
|
f.VarName,
|
||||||
f.Name,
|
f.Name,
|
||||||
|
|
|
@ -23,6 +23,10 @@ import (
|
||||||
"github.com/spf13/viper"
|
"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{
|
var initCmd = &cobra.Command{
|
||||||
Use: "init [name]",
|
Use: "init [name]",
|
||||||
Aliases: []string{"initialize", "initialise", "create"},
|
Aliases: []string{"initialize", "initialise", "create"},
|
||||||
|
@ -150,6 +154,24 @@ import (
|
||||||
|
|
||||||
var cfgFile string{{end}}
|
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
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "{{.appName}}",
|
Use: "{{.appName}}",
|
||||||
|
@ -173,20 +195,7 @@ func Execute() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
{{ if .viper }}
|
||||||
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 }}
|
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
@ -221,6 +230,16 @@ func initConfig() {
|
||||||
data["license"] = project.License().Header
|
data["license"] = project.License().Header
|
||||||
data["appName"] = path.Base(project.Name())
|
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)
|
rootCmdScript, err := executeTemplate(template, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
|
|
|
@ -45,7 +45,7 @@ func init() {
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
|
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().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
|
||||||
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
|
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("author", rootCmd.PersistentFlags().Lookup("author"))
|
||||||
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
||||||
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
||||||
|
|
Loading…
Add table
Reference in a new issue