summaryrefslogtreecommitdiff
path: root/config.go
blob: 3c5fb117847e63592e654785c1dc6ab091a30a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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

	flag.StringVar(&version, "version", "v0.1", "Set compiled in version string")

	flag.StringVar(&filename, "filename", "seascape.glsl", "path to GLSL file")
	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"})
}