Chromium Code Reviews| Index: mojom/mojom_parser/mojom_main.go |
| diff --git a/mojom/mojom_parser/mojom_main.go b/mojom/mojom_parser/mojom_main.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..646e4286cc8d6893bc61ffbf595ae05445d1c483 |
| --- /dev/null |
| +++ b/mojom/mojom_parser/mojom_main.go |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "flag" |
| + "fmt" |
| + "os" |
| +) |
| + |
| +// This file contains the main() for the mojom tool binary. |
| +// The mojom tool is used to process .mojom files. |
| +// |
| +// The tool is invoked as follows: |
| +// |
| +// mojom <command> [<arguments>] |
| +// |
| +// where <commands> is one of: |
| +// - parse |
| +// - fmt |
| +// |
| +// For further information about each command, see the file named |
| +// <command>_cmd.go for example "parse_cmd.go" and "fmt_cmd.go". |
| +func main() { |
| + commands := NewCommandSet() |
| + commands.AddCommand("parse", parseCmd, "Parses mojom files.") |
| + commands.AddCommand("fmt", fmtCmd, "Formats a mojom file.") |
| + commands.AddHelpCommand() |
| + commands.RunCommand(os.Args) |
| +} |
| + |
| +func ErrorExit(message string) { |
| + fmt.Fprintf(os.Stderr, "%s\n", message) |
| + os.Exit(1) |
| +} |
| + |
| +type command struct { |
| + Name string |
| + Func func([]string) |
| + Desc string |
| +} |
| + |
| +type commandSet struct { |
| + commandNames []string |
| + commandMap map[string]command |
| +} |
| + |
| +func NewCommandSet() *commandSet { |
| + commandSet := new(commandSet) |
| + commandSet.commandMap = make(map[string]command) |
| + return commandSet |
| +} |
| + |
| +func (c *commandSet) AddCommand(name string, f func([]string), desc string) { |
| + if _, ok := c.commandMap[name]; ok { |
| + panic(fmt.Sprintf("Tried to add a second command with the name: %s", name)) |
| + } |
| + c.commandNames = append(c.commandNames, name) |
| + c.commandMap[name] = command{name, f, desc} |
| +} |
| + |
| +func (c *commandSet) Usage(toolName string) string { |
| + b := bytes.Buffer{} |
| + b.WriteString(fmt.Sprintf("%s is a tool for managing .mojom files.\n\n", toolName)) |
| + b.WriteString("Usage:\n\n") |
| + b.WriteString(fmt.Sprintf("\t%s <command> [<arguments>]\n\n", toolName)) |
| + b.WriteString("The commands are:\n\n") |
| + |
| + for _, name := range c.commandNames { |
| + b.WriteString(fmt.Sprintf("\t%s\t%s\n", name, c.commandMap[name].Desc)) |
| + } |
| + return b.String() |
| +} |
| + |
| +func (c *commandSet) AddHelpCommand() { |
| + helpCmd := func(args []string) { |
| + fmt.Print(c.Usage(args[0])) |
| + } |
| + c.AddCommand("help", helpCmd, "Prints out this help message.") |
| +} |
| + |
| +func (c *commandSet) RunCommand(args []string) { |
| + if len(args) < 2 { |
| + fmt.Println("No command specified.") |
| + fmt.Print(c.Usage(args[0])) |
| + os.Exit(1) |
| + } |
| + |
| + cmd, ok := c.commandMap[args[1]] |
| + if !ok { |
| + fmt.Printf("%s is not a recognized command.\n", args[1]) |
| + fmt.Print(c.Usage(args[0])) |
| + os.Exit(1) |
| + } |
| + |
| + cmd.Func(args) |
| +} |
| + |
| +// GetFlagDefaults constructs a usage string for the flags of a FlagSet. |
| +func GetFlagDefaults(f *flag.FlagSet) string { |
|
rudominer
2016/02/20 05:53:49
Seems like a strange name for this function. How a
azani
2016/02/22 17:37:39
Done.
|
| + b := bytes.Buffer{} |
| + |
| + // isZeroValue guesses if the string represents the zero value for a flag. |
|
rudominer
2016/02/20 05:53:49
explain why--how is this used?
azani
2016/02/22 17:37:39
Done.
|
| + isZeroValue := func(value string) bool { |
| + switch value { |
| + case "false": |
| + return true |
| + case "": |
| + return true |
| + case "0": |
| + return true |
| + } |
| + return false |
| + } |
| + |
| + f.VisitAll(func(f *flag.Flag) { |
|
rudominer
2016/02/20 05:53:49
Please add comment explaining what is happening he
azani
2016/02/22 17:37:39
Done.
|
| + s := fmt.Sprintf(" -%s", f.Name) // Two spaces before -; see next two comments. |
| + // Four spaces before the tab triggers good alignment |
| + // for both 4- and 8-space tab stops. |
| + s += "\n \t" |
| + s += f.Usage |
| + if !isZeroValue(f.DefValue) { |
| + s += fmt.Sprintf(" (default %q)", f.DefValue) |
| + } |
| + b.WriteString(fmt.Sprint(s, "\n")) |
| + }) |
| + |
| + return b.String() |
| +} |