116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package main
|
|
|
|
/*
|
|
This simply parses the command line arguments using the default golang
|
|
package called 'flag'. This can be used as a simple template to parse
|
|
command line arguments in other programs.
|
|
|
|
It puts everything in the 'config' package which I think is a good
|
|
wrapper around the 'flags' package and doesn't need a whole mess of
|
|
global variables
|
|
*/
|
|
|
|
import "log"
|
|
import "os"
|
|
import "flag"
|
|
import "fmt"
|
|
import "github.com/gookit/config"
|
|
import "github.com/gookit/config/yaml"
|
|
import "github.com/gookit/config/json"
|
|
|
|
// import "github.com/davecgh/go-spew/spew"
|
|
|
|
var customUsage = func() {
|
|
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
|
|
flag.PrintDefaults()
|
|
|
|
fmt.Println("")
|
|
fmt.Println("EXAMPLES:")
|
|
fmt.Println("")
|
|
fmt.Println(os.Args[0] + "--hostname test.hostname.wit.com")
|
|
fmt.Println("")
|
|
}
|
|
|
|
func parseFlags() {
|
|
var version string
|
|
var nogui bool
|
|
var debugging bool
|
|
var hostname string
|
|
var width int
|
|
var height int
|
|
|
|
flag.StringVar (&version, "version", "v0.1", "Set compiled in version string")
|
|
|
|
flag.StringVar (&hostname, "hostname", "localhost", "Your full hostname")
|
|
flag.IntVar (&width, "width", 400, "Width of the Window")
|
|
flag.IntVar (&height, "height", 600, "Height of the Window")
|
|
|
|
flag.BoolVar (&nogui, "nogui", nogui, "Do not display the GUI")
|
|
flag.BoolVar (&debugging, "debugging", debugging, "Enable debugging")
|
|
|
|
// Set the output if something fails to stdout rather than stderr
|
|
flag.CommandLine.SetOutput(os.Stdout)
|
|
|
|
flag.Usage = customUsage
|
|
flag.Parse()
|
|
|
|
if flag.Parsed() {
|
|
log.Println("flag.Parse() worked")
|
|
} else {
|
|
log.Println("flag.Parse() failed")
|
|
}
|
|
|
|
if (debugging == true) {
|
|
log.Println("ENABLE DEBUG", debugging)
|
|
} else {
|
|
log.Println("DISABLE DEBUG", debugging)
|
|
}
|
|
|
|
config.Set("width", width)
|
|
config.Set("height", height)
|
|
config.Set("hostname", hostname)
|
|
config.Set("nogui", nogui)
|
|
config.Set("debugging", debugging)
|
|
}
|
|
|
|
func parseConfig() {
|
|
config.WithOptions(config.ParseEnv)
|
|
parseFlags()
|
|
|
|
config.LoadOSEnv([]string{"MAIL"})
|
|
config.LoadOSEnv([]string{"USER"})
|
|
config.LoadOSEnv([]string{"BUILDDEBUG"})
|
|
|
|
// add driver for support of yaml and json
|
|
config.AddDriver(yaml.Driver)
|
|
config.AddDriver(json.Driver)
|
|
|
|
config.LoadFiles("config.json")
|
|
|
|
for _, addr := range config.Strings("arr1") {
|
|
log.Println("addr =", addr)
|
|
}
|
|
|
|
map1 := config.StringMap("map1")
|
|
fmt.Printf("%#v\n",map1)
|
|
|
|
log.Println(config.String("map1.key1.jwc1"))
|
|
|
|
for account, _ := range config.StringMap("cloud") {
|
|
port := config.String("cloud." + account + ".port")
|
|
proto := config.String("cloud." + account + ".proto")
|
|
hostname := config.String("cloud." + account + ".hostname")
|
|
fmt.Println(hostname, port, proto)
|
|
}
|
|
|
|
if (config.String("nogui") == "true") {
|
|
log.Println("DO NOT DISPLAY THE GUI")
|
|
}
|
|
|
|
if (config.String("debugging") == "true") {
|
|
log.Println("ENABLE DEBUG", config.String("debugging"))
|
|
} else {
|
|
log.Println("DISABLE DEBUG", config.String("debugging"))
|
|
}
|
|
}
|