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..4066edbedc952c0147f488fc005e6abdb13b954b |
| --- /dev/null |
| +++ b/mojom/mojom_parser/mojom_main.go |
| @@ -0,0 +1,99 @@ |
| +// 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" |
| + "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") |
|
rudominer
2016/02/19 20:42:42
You forgot to put the name of the command into the
azani
2016/02/19 23:23:24
Done.
|
| + fmt.Print(c.Usage(args[0])) |
| + os.Exit(1) |
| + } |
| + |
| + cmd.Func(args) |
| +} |