func usage() { buf := new(bytes.Buffer) flag.CommandLine.SetOutput(buf) flag.PrintDefaults() locale.Printf(locale.FlagUsage, app.Name, buf.String(), app.RepoURL, app.OfficialURL) }
func main() { tag, err := locale.Init() if err != nil { warn.Println(err) info.Println("无法获取系统语言,使用默认的本化语言:", app.DefaultLocale) tag, err = language.Parse(app.DefaultLocale) if err != nil { erro.Println(err) return } } locale.SetLocale(tag) h := flag.Bool("h", false, locale.Sprintf(locale.FlagHUsage)) v := flag.Bool("v", false, locale.Sprintf(locale.FlagVUsage)) l := flag.Bool("l", false, locale.Sprintf(locale.FlagLUsage)) g := flag.Bool("g", false, locale.Sprintf(locale.FlagGUsage)) pprofType := flag.String("pprof", "", locale.Sprintf(locale.FlagPprofUsage)) flag.Usage = usage flag.Parse() switch { case *h: flag.Usage() return case *v: locale.Printf(locale.FlagVersionBuildWith, app.Name, app.Version, runtime.Version()) return case *l: locale.Printf(locale.FlagSupportedLangs, input.Langs()) return case *g: path, err := getConfigFile() if err != nil { erro.Println(err) return } if err = genConfigFile(path); err != nil { erro.Println(err) return } info.Println(locale.Sprintf(locale.FlagConfigWritedSuccess, path)) return } // 指定了 pprof 参数 if len(*pprofType) > 0 { profile := filepath.Join("./", app.Profile) f, err := os.Create(profile) if err != nil { // 不能创建文件,则忽略 pprof 相关操作 warn.Println(err) goto RUN } defer func() { if err = f.Close(); err != nil { erro.Println(err) return } info.Println(locale.Sprintf(locale.FlagPprofWritedSuccess, profile)) }() switch strings.ToLower(*pprofType) { case "mem": defer func() { if err = pprof.Lookup("heap").WriteTo(f, 1); err != nil { warn.Println(err) } }() case "cpu": if err := pprof.StartCPUProfile(f); err != nil { warn.Println(err) } defer pprof.StopCPUProfile() default: erro.Println(locale.Sprintf(locale.FlagInvalidPprrof)) return } } RUN: run() }