// !!! !!!
// WARNING: Code automatically generated. Editing discouraged.
// !!! !!!

package {{.PackageName}}

import (
	"flag"
	"fmt"
	"os"
)

////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions

const progname = "{{.ProgramName}}" // os.Args[0]

// The {{.StructName}} struct defines the structure to hold the commandline values
type {{.StructName}} struct { {{range .Options}}{{if ne .Name "SEPARATOR" }}
	{{.Name}}		{{.Type}}	// {{.Usage}}{{end}}{{end}}
}

////////////////////////////////////////////////////////////////////////////
// Global variables definitions

// {{.StructVar}} holds the actual values from the command line parameters
var {{.StructVar}} {{.StructName}}

////////////////////////////////////////////////////////////////////////////
// Commandline definitions

func init() {

	// set default values for command line parameters{{range .Options}}{{if eq .Name "SEPARATOR" }}

{{else}}
	flag.{{clk2uc .Type}}Var(&{{$.StructVar}}.{{.Name}}, "{{.Flag}}", {{.Value}},
		"{{.Usage}}"){{end}}{{end}}

	// Now override those default values from environment variables{{range .Options}}{{if eq .Name "SEPARATOR" }}

{{else}}{{if eq .Type "string" }}{{$envVar := printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .Flag)}}
	if len({{$.StructVar}}.{{.Name}}) == 0 ||
		len(os.Getenv("{{$envVar}}")) != 0 {
		{{$.StructVar}}.{{.Name}} = os.Getenv("{{$envVar}}")
	}{{end}}{{end}}{{end}}

}

{{if eqf .UsageSummary "True" }}
const USAGE_SUMMARY = "{{range .Options}}{{if eq .Name "SEPARATOR" }}\n{{else}}{{$envVar := printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .Flag)}}  -{{.Flag}}\t{{.Usage}} ({{$envVar}})\n{{end}}{{end}}\nDetails:\n\n"
{{end}}

// Usage function shows help on commandline usage
func Usage() {
	fmt.Fprintf(os.Stderr,
		"{{.UsageLead}}",
		progname){{if eq .UsageSummary "TRUE" }}
	fmt.Fprintf(os.Stderr, USAGE_SUMMARY){{end}}
	flag.PrintDefaults()
	fmt.Fprintf(os.Stderr,
		"{{.UsageEnd}}")
	os.Exit(0)
}

