|
package easygenapi |
|
|
|
import ( |
|
"flag" |
|
"fmt" |
|
"os" |
|
) |
|
|
|
//////////////////////////////////////////////////////////////////////////// |
|
// Constant and data type/structure definitions |
|
|
|
const progname = "easygen" // os.Args[0] |
|
|
|
// The Options struct defines the structure to hold the commandline values |
|
type Options struct { |
|
HTML bool // treat the template file as html instead of text |
|
TemplateStr string // template string (in text) |
|
TemplateFile string // .tmpl (comma-separated) template file `name(s)` (default: same as .yaml file) |
|
ExtYaml string // `extension` of yaml file |
|
ExtTmpl string // `extension` of template file |
|
StrFrom string // replace from, the from string used for the replace function |
|
StrTo string // replace to, the to string used for the replace function |
|
Debug int // debugging `level` |
|
} |
|
|
|
//////////////////////////////////////////////////////////////////////////// |
|
// Global variables definitions |
|
|
|
// Opts holds the actual values from the command line parameters |
|
var Opts Options |
|
|
|
//////////////////////////////////////////////////////////////////////////// |
|
// Commandline definitions |
|
|
|
func initVars() { |
|
|
|
// set default values for command line parameters |
|
flag.BoolVar(&Opts.HTML, "html", false, |
|
"treat the template file as html instead of text") |
|
flag.StringVar(&Opts.TemplateStr, "ts", "", |
|
"template string (in text)") |
|
flag.StringVar(&Opts.TemplateFile, "f", "", |
|
".tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)") |
|
flag.StringVar(&Opts.TemplateFile, "tf", "", |
|
".tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)") |
|
flag.StringVar(&Opts.ExtYaml, "ey", ".yaml", |
|
"`extension` of yaml file") |
|
flag.StringVar(&Opts.ExtTmpl, "et", ".tmpl", |
|
"`extension` of template file") |
|
flag.StringVar(&Opts.StrFrom, "rf", "", |
|
"replace from, the from string used for the replace function") |
|
flag.StringVar(&Opts.StrTo, "rt", "", |
|
"replace to, the to string used for the replace function") |
|
flag.IntVar(&Opts.Debug, "d", 0, |
|
"debugging `level`") |
|
flag.IntVar(&Opts.Debug, "debug", 0, |
|
"debugging `level`") |
|
} |
|
|
|
func initVals() { |
|
exists := false |
|
// Now override those default values from environment variables |
|
if _, exists = os.LookupEnv("EASYGEN_HTML"); Opts.HTML|| exists { |
|
Opts.HTML = true |
|
} |
|
if len(Opts.TemplateStr) == 0 || |
|
len(os.Getenv("EASYGEN_TS")) != 0 { |
|
Opts.TemplateStr = os.Getenv("EASYGEN_TS") |
|
} |
|
if len(Opts.TemplateFile) == 0 || |
|
len(os.Getenv("EASYGEN_F")) != 0 { |
|
Opts.TemplateFile = os.Getenv("EASYGEN_F") |
|
} |
|
if len(Opts.TemplateFile) == 0 || |
|
len(os.Getenv("EASYGEN_TF")) != 0 { |
|
Opts.TemplateFile = os.Getenv("EASYGEN_TF") |
|
} |
|
if len(Opts.ExtYaml) == 0 || |
|
len(os.Getenv("EASYGEN_EY")) != 0 { |
|
Opts.ExtYaml = os.Getenv("EASYGEN_EY") |
|
} |
|
if len(Opts.ExtTmpl) == 0 || |
|
len(os.Getenv("EASYGEN_ET")) != 0 { |
|
Opts.ExtTmpl = os.Getenv("EASYGEN_ET") |
|
} |
|
if len(Opts.StrFrom) == 0 || |
|
len(os.Getenv("EASYGEN_RF")) != 0 { |
|
Opts.StrFrom = os.Getenv("EASYGEN_RF") |
|
} |
|
if len(Opts.StrTo) == 0 || |
|
len(os.Getenv("EASYGEN_RT")) != 0 { |
|
Opts.StrTo = os.Getenv("EASYGEN_RT") |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Usage function shows help on commandline usage |
|
func Usage() { |
|
fmt.Fprintf(os.Stderr, |
|
"\nUsage:\n %s [flags] YamlFileName [YamlFileName...]\n\nFlags:\n\n", |
|
progname) |
|
flag.PrintDefaults() |
|
fmt.Fprintf(os.Stderr, |
|
"\nYamlFileName(s): The name for the .yaml data and .tmpl template file\n\tOnly the name part, without extension. Can include the path as well.\n") |
|
os.Exit(0) |
|
} |
The
easygenprovides a rich set of options to auto-generating Go code for command line parameter handling. Here are them in real code.For built-in flag package
Sample Go flag package based code
Sample Go code:
easygen/test/commandlineFlag.ref
Lines 5 to 112 in cdf80e9
Which is generated from this YAML file:
easygen/test/commandlineFlag.yaml
Lines 1 to 76 in cdf80e9
For
easygenitself using the built-in flag packageeasygen/cmd/easygen/flags.go
Lines 1 to 86 in cdf80e9
Which is generated from this YAML file:
easygen/cmd/easygen/easygen.yaml
Lines 1 to 76 in cdf80e9
For
ffcvtusing the built-in flag packageThis is by far the longest command line parameter handling code I've ever built --
https://github.com/suntong/ffcvt/blob/master/config.go
and it is built by
easygenautomatically as well.For the
viperpackageSample Go code:
easygen/test/commandlineCV.ref
Lines 2 to 18 in cdf80e9
Which is generated from this YAML file:
easygen/test/commandlineCV.yaml
Lines 1 to 30 in cdf80e9
For the
clipackageSample
clipackage based code 1Sample Go code:
easygen/test/commandlineCLI-024.ref
Lines 1 to 144 in cdf80e9
Which is generated from this YAML file:
easygen/test/commandlineCLI-024.yaml
Lines 1 to 108 in cdf80e9
Sample
clipackage based code 2Sample Go code 2:
easygen/test/commandlineCLI-027.ref
Lines 1 to 138 in cdf80e9
This is my current way of generating
clipackage based command line parameter handling code.It is generated from this YAML file:
easygen/test/commandlineCLI-027.yaml
Lines 1 to 116 in cdf80e9