-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Labels
Description
Here is easygen using the same YAML file of:
easygen/cmd/easygen/easygen.yaml
Lines 1 to 76 in cdf80e9
| # program name, name for the executable | |
| ProgramName: easygen | |
| # package name | |
| # - For standalone program that does not belong to any package, e.g., | |
| # https://github.com/suntong/easygen/blob/7791e4f0e5605543d27da1671a21376cdb9dcf2a/easygen/easygen.go | |
| # just ignore the first line, the `package` output, and copy the rest | |
| # - If you don't mind using a separated file to handle commandline paramters, | |
| # then name the package as "main". see the spin-out "TF-minus1.go" file under | |
| # https://github.com/suntong/easygen/tree/d1ab0b5fe80ddac57fe9ef51f6ccb3ab998cd5ee | |
| # - If you are using it in a pacakge, look no further than | |
| # https://github.com/suntong/easygen/blob/master/easygenapi/config.go | |
| # which was a direct dump: easygen test/commandlineFlag | gofmt > easygenapi/config.go | |
| # | |
| PackageName: easygen | |
| # Name of the structure to hold the values for/from commandline | |
| StructName: Options | |
| # The actual variable that hold the commandline paramter values | |
| StructVar: Opts | |
| Options: | |
| - Name: TemplateStr | |
| Type: string | |
| Flag: ts | |
| Value: '""' | |
| Usage: "template string (in text)" | |
| - Name: ExtYaml | |
| Type: string | |
| Flag: ey | |
| Value: '".yaml"' | |
| Usage: "`extension` of yaml file" | |
| - Name: ExtJson | |
| Type: string | |
| Flag: ej | |
| Value: '".json"' | |
| Usage: "`extension` of json file" | |
| - Name: ExtTmpl | |
| Type: string | |
| Flag: et | |
| Value: '".tmpl"' | |
| Usage: "`extension` of template file" | |
| - Name: Debug | |
| Type: int | |
| Flag: debug | |
| Value: 0 | |
| Usage: "debugging `level`" | |
| # Whether to use the USAGE_SUMMARY in Usage help | |
| UsageSummary: "" | |
| UsageLead: "\\nUsage:\\n %s [flags] template_name [data_filename [data_filename...]]\\n\\nFlags:\\n\\n" | |
| UsageEnd: | | |
| template_name: The name for the template file. | |
| - Can have the extension (specified by -et) or without it. | |
| - Can include the path as well. | |
| - Can be a comma-separated list giving many template files, in which case | |
| at least one data_filename must be given. | |
| data_filename(s): The name for the .yaml or .json data. | |
| - If omitted derive from the template_name. | |
| - Can have the extension or without it. If withot extension, | |
| will try the .yaml file first then .json | |
| - Can include the path as well. | |
| - If there is a single data_filename, and it is "-", | |
| then read the data (.yaml or .json format) from stdin. | |
| Flag defaults can be overridden by corresponding environment variable, e.g.: | |
| EASYGEN_EY=.yml EASYGEN_ET=.tpl easygen ... |
which is used to generates the cli code, and use it to auto-generate Go data structure instead:
Lines 1 to 19 in cdf80e9
| // !!! !!! | |
| // WARNING: Code automatically generated. Editing discouraged. | |
| // !!! !!! | |
| package easygen | |
| //////////////////////////////////////////////////////////////////////////// | |
| // 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 { | |
| TemplateStr string // template string (in text) | |
| ExtYaml string // `extension` of yaml file | |
| ExtJson string // `extension` of json file | |
| ExtTmpl string // `extension` of template file | |
| Debug int // debugging `level` | |
| } |
from this template:
easygen/cmd/easygen/config.tmpl
Lines 1 to 16 in cdf80e9
| // !!! !!! | |
| // WARNING: Code automatically generated. Editing discouraged. | |
| // !!! !!! | |
| package {{.PackageName}} | |
| //////////////////////////////////////////////////////////////////////////// | |
| // 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}} | |
| } | |
with this command:
easygen/cmd/easygen/easygen.gen.sh
Line 1 in cdf80e9
| easygen config easygen | gofmt > ../../config.go |
For more infomation, check out
EasyGen -- Universal code/text generator that is easy to use