github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/i18n/ui18n/ui18n.go (about) 1 // Copyright 2013 Unknown 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 // ui18n is a helper tool for Unknwon/i18n package. 16 package main 17 18 import ( 19 "flag" 20 "fmt" 21 "html/template" 22 "io" 23 "log" 24 "os" 25 "strings" 26 ) 27 28 type Command struct { 29 // Run runs the command. 30 // The args are the arguments after the command name. 31 Run func(cmd *Command, args []string) 32 33 // UsageLine is the one-line usage message. 34 // The first word in the line is taken to be the command name. 35 UsageLine string 36 37 // Short is the short description shown in the 'go help' output. 38 Short string 39 40 // Long is the long message shown in the 'go help <this-command>' output. 41 Long string 42 43 // Flag is a set of flags specific to this command. 44 Flag flag.FlagSet 45 46 // CustomFlags indicates that the command will do its own 47 // flag parsing. 48 CustomFlags bool 49 } 50 51 // Name returns the command's name: the first word in the usage line. 52 func (c *Command) Name() string { 53 name := c.UsageLine 54 i := strings.Index(name, " ") 55 if i >= 0 { 56 name = name[:i] 57 } 58 return name 59 } 60 61 func (c *Command) Usage() { 62 fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine) 63 fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long)) 64 os.Exit(2) 65 } 66 67 // Runnable reports whether the command can be run; otherwise 68 // it is a documentation pseudo-command such as importpath. 69 func (c *Command) Runnable() bool { 70 return c.Run != nil 71 } 72 73 var commands = []*Command{ 74 cmdSync, 75 } 76 77 func main() { 78 flag.Usage = usage 79 flag.Parse() 80 log.SetFlags(0) 81 82 args := flag.Args() 83 if len(args) < 1 { 84 usage() 85 } 86 87 if args[0] == "help" { 88 help(args[1:]) 89 return 90 } 91 92 for _, cmd := range commands { 93 if cmd.Name() == args[0] && cmd.Run != nil { 94 cmd.Flag.Usage = func() { cmd.Usage() } 95 if cmd.CustomFlags { 96 args = args[1:] 97 } else { 98 cmd.Flag.Parse(args[1:]) 99 args = cmd.Flag.Args() 100 } 101 cmd.Run(cmd, args) 102 os.Exit(2) 103 return 104 } 105 } 106 107 fmt.Fprintf(os.Stderr, "ui18n: unknown subcommand %q\nRun 'bee help' for usage.\n", args[0]) 108 os.Exit(2) 109 } 110 111 var usageTemplate = `ui18n is a helper tool for beego/i18n package. 112 113 Usage: 114 115 ui18n command [arguments] 116 117 The commands are: 118 {{range .}}{{if .Runnable}} 119 {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}} 120 121 Use "ui18n help [command]" for more information about a command. 122 ` 123 124 var helpTemplate = `{{if .Runnable}}usage: ui18n {{.UsageLine}} 125 126 {{end}}{{.Long | trim}} 127 ` 128 129 func usage() { 130 tmpl(os.Stdout, usageTemplate, commands) 131 os.Exit(2) 132 } 133 134 func tmpl(w io.Writer, text string, data interface{}) { 135 t := template.New("top") 136 t.Funcs(template.FuncMap{"trim": strings.TrimSpace}) 137 template.Must(t.Parse(text)) 138 if err := t.Execute(w, data); err != nil { 139 panic(err) 140 } 141 } 142 143 func help(args []string) { 144 if len(args) == 0 { 145 usage() 146 // not exit 2: succeeded at 'go help'. 147 return 148 } 149 if len(args) != 1 { 150 fmt.Fprintf(os.Stdout, "usage: ui18n help command\n\nToo many arguments given.\n") 151 os.Exit(2) // failed at 'bee help' 152 } 153 154 arg := args[0] 155 156 for _, cmd := range commands { 157 if cmd.Name() == arg { 158 tmpl(os.Stdout, helpTemplate, cmd) 159 // not exit 2: succeeded at 'go help cmd'. 160 return 161 } 162 } 163 164 fmt.Fprintf(os.Stdout, "Unknown help topic %#q. Run 'ui18n help'.\n", arg) 165 os.Exit(2) // failed at 'bee help cmd' 166 }