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..88886377940fc06c4edf1832d9e6eed5124453ff |
| --- /dev/null |
| +++ b/mojom/mojom_parser/mojom_main.go |
| @@ -0,0 +1,93 @@ |
| +// 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 ( |
| + "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] |
|
rudominer
2016/02/19 05:49:16
put "command" and "arguments" in angle brackets to
azani
2016/02/19 19:28:29
Done.
|
| +// |
| +// To see the implementation of the commands, look at parse_cmd.go and |
| +// fmt_cmd.go. |
|
rudominer
2016/02/19 05:49:16
Instead of this statement I would prefer a continu
azani
2016/02/19 19:28:30
Done.
|
| +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 { |
| + usage := "Mojom is a tool for managing .mojom files.\n\n" |
|
rudominer
2016/02/19 05:49:16
Instead of "Mojom" you should toolName here.
azani
2016/02/19 19:28:30
Done.
|
| + usage += "Usage:\n\n" |
| + usage += fmt.Sprintf("\t%s command [arguments]\n\n", toolName) |
|
rudominer
2016/02/19 05:49:16
Put angle brackets around "command" (and maybe als
azani
2016/02/19 19:28:30
Done.
|
| + usage += "The commands are:\n\n" |
| + |
| + for _, name := range c.commandNames { |
| + usage += fmt.Sprintf("\t%s\t%s\n", name, c.commandMap[name].Desc) |
|
rudominer
2016/02/19 05:49:16
use a byte.buffer to build up the usage string. se
azani
2016/02/19 19:28:29
Done.
|
| + } |
| + return usage |
| +} |
| + |
| +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") |
| + fmt.Print(c.Usage(args[0])) |
| + os.Exit(1) |
| + } |
| + |
| + cmd.Func(args) |
| +} |