func newFilename(old string, pkg string) string {
	if *out != "" {
		if pre := strings.TrimPrefix(*out, old); len(pre) > 0 &&
			!strings.HasSuffix(*out, ".go") {
			return filepath.Join(old, *out)
		}
		return *out
	}
	if fi, err := os.Stat(old); err == nil && fi.IsDir() {
		old = filepath.Join(old, pkg)
	}
	return strings.TrimSuffix(old, ".go") + "_gen.go"
}
picks a new file name based on input flags and input filename(s).
package main
import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"github.com/algorand/msgp/gen"
	"github.com/algorand/msgp/parse"
	"github.com/algorand/msgp/printer"
	"github.com/ttacon/chalk"
)
var (
	out         = flag.String("o", "", "output file")
	file        = flag.String("file", "", "input file")
	marshal     = flag.Bool("marshal", true, "create Marshal and Unmarshal methods")
	tests       = flag.Bool("tests", true, "create tests and benchmarks")
	unexported  = flag.Bool("unexported", true, "also process unexported types")
	skipFormat  = flag.Bool("skip-format", false, "skip formatting the generated code (for debug)")
	warnPkgMask = flag.String("warnmask", "", "skip generating warnings on datatypes outside given package")
)
func main() {
	flag.Parse()
	if *file == "" {
		*file = os.Getenv("GOFILE")
		if *file == "" {
			fmt.Println(chalk.Red.Color("No file to parse."))
			os.Exit(1)
		}
	}
	var mode gen.Method
	if *marshal {
		mode |= (gen.Marshal | gen.Unmarshal | gen.Size | gen.IsZero)
	}
	if *tests {
		mode |= gen.Test
	}
	if mode&^gen.Test == 0 {
		fmt.Println(chalk.Red.Color("No methods to generate; -marshal=false"))
		os.Exit(1)
	}
	if err := Run(*file, mode, *unexported, *warnPkgMask); err != nil {
		fmt.Println(chalk.Red.Color(err.Error()))
		os.Exit(1)
	}
}
func Run(gofile string, mode gen.Method, unexported bool, warnPkgMask string) error {
	if mode&^gen.Test == 0 {
		return nil
	}
	fmt.Println(chalk.Magenta.Color("======== MessagePack Code Generator ======="))
	fmt.Printf(chalk.Magenta.Color(">>> Input: \"%s\"\n"), gofile)
	fs, err := parse.File(gofile, unexported, warnPkgMask)
	if err != nil {
		return err
	}
	if len(fs.Identities) == 0 {
		fmt.Println(chalk.Magenta.Color("No types requiring code generation were found!"))
		return nil
	}
	return printer.PrintFile(newFilename(gofile, fs.Package), fs, mode, *skipFormat)
}