Skip to content
Myr

Add explanation for viper.BindPflag in README.md

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #429 /cc @deltaskelta

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent ca57f0f · Browse files · 1 file+51 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+51 added−3 removed

  • other:+51 added−3 removed

1 file+51 added−3 removed

modified:README.md+51 added-3 removed
274 unmodified lines
275### Create the root command276277The root command represents your binary itself.278279279#### Manually create rootCmd280281Cobra doesn't require any special constructors. Simply create your commands.282
13 unmodified lines
296```297298You will additionally define flags and handle configuration in your init() function.299301for example cmd/root.go:300For example cmd/root.go:301302```go303import (304 "fmt"305 "os"306307 homedir "github.com/mitchellh/go-homedir"308 "github.com/spf13/cobra"309 "github.com/spf13/viper"310)311312func init() {313 cobra.OnInitialize(initConfig)314 RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")315 RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")
5 unmodified lines
321 viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))322 viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")323 viper.SetDefault("license", "apache")324}325326func main() {327 // Don't forget to read config either from cfgFile or from home directory!328 if cfgFile != "" {329 // Use config file from the flag.330 viper.SetConfigFile(cfgFile)331 } else {332 // Find home directory.333 home, err := homedir.Dir()334 if err != nil {335 fmt.Println(home)336 os.Exit(1)337 }338339 // Search config in home directory with name ".cobra" (without extension).340 viper.AddConfigPath(home)341 viper.SetConfigName(".cobra")342 }343344 if err := viper.ReadInConfig(); err != nil {345 fmt.Println("Can't read config:", err)346 os.Exit(1)347 }348}349```350351### Create your main.go352
19 unmodified lines
372 }373}374```375344376### Create additional commands377378Additional commands can be defined and typically are each given their own file379inside of the cmd/ directory.
81 unmodified lines
461```go462RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")463```464465### Bind Flags with Config466467You can also bind your flags with [viper](https://github.com/spf13/viper):468```go469var author string470471func init() {472 RootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")473 viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))474}475```476477In this example the persistent flag `author` is bound with `viper`.478**Note**, that the variable `author` will not be set to the value from config,479when the `--author` flag is not provided by user.480481More in [viper documentation](https://github.com/spf13/viper#working-with-flags).482483## Example484485In the example below, we have defined three commands. Two are at the top level486and one (cmdTimes) is a child of one of the top commands. In this case the root
449 unmodified lines