Support including chosen comments in generated code

This commit is contained in:
David Terei 2018-08-08 16:16:25 -07:00
parent 30e0eba7d0
commit a4db2f8dd0
1 changed files with 18 additions and 0 deletions

View File

@ -31,6 +31,17 @@ import (
"github.com/pascaldekloe/name" "github.com/pascaldekloe/name"
) )
type arrayFlags []string
func (af arrayFlags) String() string {
return strings.Join(af, "")
}
func (af *arrayFlags) Set(value string) error {
*af = append(*af, value)
return nil
}
var ( var (
typeNames = flag.String("type", "", "comma-separated list of type names; must be set") typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.") sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.")
@ -42,6 +53,12 @@ var (
trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"") trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"")
) )
var comments arrayFlags
func init() {
flag.Var(&comments, "comment", "comments to include in generated code, can repeat. Default: \"\"")
}
// Usage is a replacement usage function for the flags package. // Usage is a replacement usage function for the flags package.
func Usage() { func Usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
@ -88,6 +105,7 @@ func main() {
// Print the header and package clause. // Print the header and package clause.
g.Printf("// Code generated by \"enumer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) g.Printf("// Code generated by \"enumer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " "))
g.Printf("\n") g.Printf("\n")
g.Printf("// %s\n", comments.String())
g.Printf("package %s", g.pkg.name) g.Printf("package %s", g.pkg.name)
g.Printf("\n") g.Printf("\n")
g.Printf("import (\n") g.Printf("import (\n")