一、命令行参数解析
Pflag使用示例:
import (
"github.com/spf13/pflag"
)
pflag.BoolVarP(&version, "version", "v", true, "show version infomation")
二、配置解析
Viper使用示例:
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigFile("dbconfig")
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
三、命令行框架
Cobra
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(versionCmd)
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version",
Long: `Print the version`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("v1.0.0")
},
}