OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package main |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "flag" |
| 10 "fmt" |
| 11 "os" |
| 12 ) |
| 13 |
| 14 // This file contains the main() for the mojom tool binary. |
| 15 // The mojom tool is used to process .mojom files. |
| 16 // |
| 17 // The tool is invoked as follows: |
| 18 // |
| 19 // mojom <command> [<arguments>] |
| 20 // |
| 21 // where <commands> is one of: |
| 22 // - parse |
| 23 // - fmt |
| 24 // |
| 25 // For further information about each command, see the file named |
| 26 // <command>_cmd.go for example "parse_cmd.go" and "fmt_cmd.go". |
| 27 func main() { |
| 28 commands := NewCommandSet() |
| 29 commands.AddCommand("parse", parseCmd, "Parses mojom files.") |
| 30 commands.AddCommand("fmt", fmtCmd, "Formats a mojom file.") |
| 31 commands.AddHelpCommand() |
| 32 commands.RunCommand(os.Args) |
| 33 } |
| 34 |
| 35 func ErrorExit(message string) { |
| 36 fmt.Fprintf(os.Stderr, "%s\n", message) |
| 37 os.Exit(1) |
| 38 } |
| 39 |
| 40 type command struct { |
| 41 Name string |
| 42 Func func([]string) |
| 43 Desc string |
| 44 } |
| 45 |
| 46 type commandSet struct { |
| 47 commandNames []string |
| 48 commandMap map[string]command |
| 49 } |
| 50 |
| 51 func NewCommandSet() *commandSet { |
| 52 commandSet := new(commandSet) |
| 53 commandSet.commandMap = make(map[string]command) |
| 54 return commandSet |
| 55 } |
| 56 |
| 57 func (c *commandSet) AddCommand(name string, f func([]string), desc string) { |
| 58 if _, ok := c.commandMap[name]; ok { |
| 59 panic(fmt.Sprintf("Tried to add a second command with the name:
%s", name)) |
| 60 } |
| 61 c.commandNames = append(c.commandNames, name) |
| 62 c.commandMap[name] = command{name, f, desc} |
| 63 } |
| 64 |
| 65 func (c *commandSet) Usage(toolName string) string { |
| 66 b := bytes.Buffer{} |
| 67 b.WriteString(fmt.Sprintf("%s is a tool for managing .mojom files.\n\n",
toolName)) |
| 68 b.WriteString("Usage:\n\n") |
| 69 b.WriteString(fmt.Sprintf("\t%s <command> [<arguments>]\n\n", toolName)) |
| 70 b.WriteString("The commands are:\n\n") |
| 71 |
| 72 for _, name := range c.commandNames { |
| 73 b.WriteString(fmt.Sprintf("\t%s\t%s\n", name, c.commandMap[name]
.Desc)) |
| 74 } |
| 75 return b.String() |
| 76 } |
| 77 |
| 78 func (c *commandSet) AddHelpCommand() { |
| 79 helpCmd := func(args []string) { |
| 80 fmt.Print(c.Usage(args[0])) |
| 81 } |
| 82 c.AddCommand("help", helpCmd, "Prints out this help message.") |
| 83 } |
| 84 |
| 85 func (c *commandSet) RunCommand(args []string) { |
| 86 if len(args) < 2 { |
| 87 fmt.Println("No command specified.") |
| 88 fmt.Print(c.Usage(args[0])) |
| 89 os.Exit(1) |
| 90 } |
| 91 |
| 92 cmd, ok := c.commandMap[args[1]] |
| 93 if !ok { |
| 94 fmt.Printf("%s is not a recognized command.\n", args[1]) |
| 95 fmt.Print(c.Usage(args[0])) |
| 96 os.Exit(1) |
| 97 } |
| 98 |
| 99 cmd.Func(args) |
| 100 } |
| 101 |
| 102 // UsageString constructs a usage string for the flags of a FlagSet. |
| 103 func UsageString(f *flag.FlagSet) string { |
| 104 b := bytes.Buffer{} |
| 105 |
| 106 // isZeroValue guesses if the string represents the zero value for a fla
g. |
| 107 // isZeroValue is used to determine if the default value of a flag shoul
d be |
| 108 // printed in its usage string. |
| 109 isZeroValue := func(value string) bool { |
| 110 switch value { |
| 111 case "false": |
| 112 return true |
| 113 case "": |
| 114 return true |
| 115 case "0": |
| 116 return true |
| 117 } |
| 118 return false |
| 119 } |
| 120 |
| 121 // Iterate over the flags and for each of them generate its usage string |
| 122 // and add it to the flag set's usage string. |
| 123 f.VisitAll(func(f *flag.Flag) { |
| 124 s := fmt.Sprintf(" -%s", f.Name) // Two spaces before -; see ne
xt two comments. |
| 125 // Four spaces before the tab triggers good alignment |
| 126 // for both 4- and 8-space tab stops. |
| 127 s += "\n \t" |
| 128 s += f.Usage |
| 129 if !isZeroValue(f.DefValue) { |
| 130 s += fmt.Sprintf(" (default %q)", f.DefValue) |
| 131 } |
| 132 b.WriteString(fmt.Sprint(s, "\n")) |
| 133 }) |
| 134 |
| 135 return b.String() |
| 136 } |
OLD | NEW |