pixelgl/config.go

85 lines
2.2 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 (
"flag"
"fmt"
"log"
"os"
"github.com/gookit/config"
)
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] + " --width 1024 --height 768 --drift .1 --filename seascape.glsl")
fmt.Println(os.Args[0] + " --width 640 --height 480 --filename planetfall.glsl")
fmt.Println("")
}
func parseFlags() {
var version string
var race bool
var filename string
var width int
var height int
var glDrift float64
var guiJunk string
flag.StringVar(&version, "version", "v0.1", "Set compiled in version string")
flag.StringVar(&filename, "filename", "seascape.glsl", "path to GLSL file")
flag.StringVar(&guiJunk, "gui", "something", "redo all this code")
flag.IntVar(&width, "width", 1024, "Width of the OpenGL Window")
flag.IntVar(&height, "height", 768, "Height of the OpenGL Window")
flag.Float64Var(&glDrift, "drift", 0.01, "Speed of the gradual camera drift")
flag.BoolVar(&race, "race", race, "Use race detector")
// 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")
}
// keys := []string{"filename", "width", "height", "drift"}
// keys := []string{"width", "height", "drift"}
// keys := []string{"height"}
// config.LoadFlags(keys)
config.Set("width", width)
config.Set("height", height)
config.Set("glDrift", glDrift)
config.Set("filename", filename)
}
func parseConfig() {
config.WithOptions(config.ParseEnv)
parseFlags()
// config.LoadOSEnv([]string{"MAIL"})
// config.LoadOSEnv([]string{"USER"})
// config.LoadOSEnv([]string{"BUILDDEBUG"})
}