From 466083852c94c2093cb3fcb7fc2ea24b2cfa611c Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 9 Jan 2019 04:13:45 -0800 Subject: [PATCH] make a clean file that just is for parsing the command line Signed-off-by: Jeff Carr --- .gitignore | 1 + community/seascape-shader/commandline.go | 67 ++++++++++++++++++++++++ community/seascape-shader/seascape.go | 34 ------------ 3 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 .gitignore create mode 100644 community/seascape-shader/commandline.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3effb5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +community/seascape-shader/seascape-shader diff --git a/community/seascape-shader/commandline.go b/community/seascape-shader/commandline.go new file mode 100644 index 0000000..8ceb8cc --- /dev/null +++ b/community/seascape-shader/commandline.go @@ -0,0 +1,67 @@ +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. +*/ + +import "log" +import "os" +import "flag" +import "fmt" + +var ( + version string + race bool + debug = os.Getenv("BUILDDEBUG") != "" + filename string + width int + height int + timeout = "120s" + uDrift float32 +) + +var customUsage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) + flag.PrintDefaults() + + fmt.Println("") + fmt.Println("EXAMPLE:") + fmt.Println("") + fmt.Println("seascape-shader --width 640 --height 480 --filename shaders/planetfall.glsl") + fmt.Println("") +} + +func parseFlags() { + flag.StringVar (&version, "version", "v0.1", "Set compiled in version string") + flag.StringVar (&filename, "filename", "shaders/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") + var tmp float64 + flag.Float64Var (&tmp, "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.SetOutput(os.Stdout) + + flag.Usage = customUsage + flag.Parse() + + if flag.Parsed() { + log.Println("Parsed() worked. width=",width) + } else { + log.Println("Parsed() failed. width=",width) + } + +// if err := flag.Parse(); err != nil { +// log.Println("Example:",width) +// } + + + uDrift = float32(tmp) + log.Println("width=",width) + log.Println("height=",height) + log.Println("uDrift=",uDrift) +} diff --git a/community/seascape-shader/seascape.go b/community/seascape-shader/seascape.go index af3be62..2e2d4a9 100644 --- a/community/seascape-shader/seascape.go +++ b/community/seascape-shader/seascape.go @@ -2,7 +2,6 @@ package main import ( "time" - "log" "github.com/go-gl/mathgl/mgl32" @@ -11,20 +10,6 @@ import ( "golang.org/x/image/colornames" ) -import "flag" -import "os" - -var ( - version string - race bool - debug = os.Getenv("BUILDDEBUG") != "" - filename string - width int - height int - timeout = "120s" - uDrift float32 -) - func run() { // Set up window configs cfg := pixelgl.WindowConfig{ // Default: 1024 x 768 @@ -86,25 +71,6 @@ func run() { } -func parseFlags() { - flag.StringVar (&version, "version", "v0.1", "Set compiled in version string") - flag.StringVar (&filename, "filename", "shaders/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") - var tmp float64 - flag.Float64Var (&tmp, "drift", 0.01, "Speed of the gradual camera drift") - flag.BoolVar (&race, "race", race, "Use race detector") - - // this parses the arguements - flag.Parse() - - uDrift = float32(tmp) - log.Println("width=",width) - log.Println("height=",height) - log.Println("uDrift=",uDrift) - -} - func main() { parseFlags()