| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Go Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style | |
| 3 // license that can be found in the LICENSE file. | |
| 4 | |
| 5 /* | |
| 6 Gomobile is a tool for building and running mobile apps written in Go. | |
| 7 | |
| 8 The tool is under development and not ready for use. | |
| 9 */ | |
| 10 package main | |
| 11 | |
| 12 import ( | |
| 13 "bufio" | |
| 14 "flag" | |
| 15 "fmt" | |
| 16 "html/template" | |
| 17 "io" | |
| 18 "log" | |
| 19 "os" | |
| 20 ) | |
| 21 | |
| 22 func printUsage(w io.Writer) { | |
| 23 bufw := bufio.NewWriter(w) | |
| 24 if err := usageTmpl.Execute(bufw, commands); err != nil { | |
| 25 panic(err) | |
| 26 } | |
| 27 bufw.Flush() | |
| 28 } | |
| 29 | |
| 30 var gomobileName = "gomobile" | |
| 31 | |
| 32 func main() { | |
| 33 gomobileName = os.Args[0] | |
| 34 flag.Usage = func() { | |
| 35 printUsage(os.Stderr) | |
| 36 os.Exit(2) | |
| 37 } | |
| 38 flag.Parse() | |
| 39 log.SetFlags(0) | |
| 40 | |
| 41 args := flag.Args() | |
| 42 if len(args) < 1 { | |
| 43 flag.Usage() | |
| 44 } | |
| 45 | |
| 46 if args[0] == "help" { | |
| 47 help(args[1:]) | |
| 48 return | |
| 49 } | |
| 50 | |
| 51 for _, cmd := range commands { | |
| 52 if cmd.Name == args[0] { | |
| 53 cmd.flag.Usage = func() { | |
| 54 cmd.usage() | |
| 55 os.Exit(1) | |
| 56 } | |
| 57 cmd.flag.Parse(args[1:]) | |
| 58 if err := cmd.run(cmd); err != nil { | |
| 59 msg := err.Error() | |
| 60 if msg != "" { | |
| 61 fmt.Fprintf(os.Stderr, "%s: %v\n", os.Ar
gs[0], err) | |
| 62 } | |
| 63 os.Exit(1) | |
| 64 } | |
| 65 return | |
| 66 } | |
| 67 } | |
| 68 fmt.Fprintf(os.Stderr, "%s: unknown subcommand %q\nRun 'gomobile help' f
or usage.\n", os.Args[0], args[0]) | |
| 69 os.Exit(2) | |
| 70 } | |
| 71 | |
| 72 func help(args []string) { | |
| 73 if len(args) == 0 { | |
| 74 printUsage(os.Stdout) | |
| 75 return // succeeded at helping | |
| 76 } | |
| 77 if len(args) != 1 { | |
| 78 fmt.Fprintf(os.Stderr, "usage: %s help command\n\nToo many argum
ents given.\n", gomobileName) | |
| 79 os.Exit(2) // failed to help | |
| 80 } | |
| 81 | |
| 82 arg := args[0] | |
| 83 for _, cmd := range commands { | |
| 84 if cmd.Name == arg { | |
| 85 cmd.usage() | |
| 86 return // succeeded at helping | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run '%s help'.\n", arg,
gomobileName) | |
| 91 os.Exit(2) | |
| 92 } | |
| 93 | |
| 94 var commands = []*command{ | |
| 95 // TODO(crawshaw): cmdRun | |
| 96 cmdBind, | |
| 97 cmdBuild, | |
| 98 cmdInit, | |
| 99 cmdInstall, | |
| 100 } | |
| 101 | |
| 102 type command struct { | |
| 103 run func(*command) error | |
| 104 flag flag.FlagSet | |
| 105 Name string | |
| 106 Usage string | |
| 107 Short string | |
| 108 Long string | |
| 109 } | |
| 110 | |
| 111 func (cmd *command) usage() { | |
| 112 fmt.Fprintf(os.Stdout, "usage: %s %s %s\n%s", gomobileName, cmd.Name, cm
d.Usage, cmd.Long) | |
| 113 } | |
| 114 | |
| 115 var usageTmpl = template.Must(template.New("usage").Parse( | |
| 116 `Gomobile is a tool for building Android and iOS Go apps. | |
| 117 | |
| 118 Usage: | |
| 119 | |
| 120 gomobile command [arguments] | |
| 121 | |
| 122 Commands: | |
| 123 {{range .}} | |
| 124 {{.Name | printf "%-11s"}} {{.Short}}{{end}} | |
| 125 | |
| 126 Use 'gomobile help [command]' for more information about that command. | |
| 127 `)) | |
| OLD | NEW |