Chromium Code Reviews| 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 "fmt" | |
| 9 "os" | |
| 10 ) | |
| 11 | |
| 12 // This file contains the main() for the mojom tool binary. | |
| 13 // The mojom tool is used to process .mojom files. | |
| 14 // | |
| 15 // The tool is invoked as follows: | |
| 16 // | |
| 17 // 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.
| |
| 18 // | |
| 19 // To see the implementation of the commands, look at parse_cmd.go and | |
| 20 // 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.
| |
| 21 func main() { | |
| 22 commands := NewCommandSet() | |
| 23 commands.AddCommand("parse", parseCmd, "Parses mojom files.") | |
| 24 commands.AddCommand("fmt", fmtCmd, "Formats a mojom file.") | |
| 25 commands.AddHelpCommand() | |
| 26 commands.RunCommand(os.Args) | |
| 27 } | |
| 28 | |
| 29 func ErrorExit(message string) { | |
| 30 fmt.Fprintf(os.Stderr, "%s\n", message) | |
| 31 os.Exit(1) | |
| 32 } | |
| 33 | |
| 34 type command struct { | |
| 35 Name string | |
| 36 Func func([]string) | |
| 37 Desc string | |
| 38 } | |
| 39 | |
| 40 type commandSet struct { | |
| 41 commandNames []string | |
| 42 commandMap map[string]command | |
| 43 } | |
| 44 | |
| 45 func NewCommandSet() *commandSet { | |
| 46 commandSet := new(commandSet) | |
| 47 commandSet.commandMap = make(map[string]command) | |
| 48 return commandSet | |
| 49 } | |
| 50 | |
| 51 func (c *commandSet) AddCommand(name string, f func([]string), desc string) { | |
| 52 if _, ok := c.commandMap[name]; ok { | |
| 53 panic(fmt.Sprintf("Tried to add a second command with the name: %s", name)) | |
| 54 } | |
| 55 c.commandNames = append(c.commandNames, name) | |
| 56 c.commandMap[name] = command{name, f, desc} | |
| 57 } | |
| 58 | |
| 59 func (c *commandSet) Usage(toolName string) string { | |
| 60 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.
| |
| 61 usage += "Usage:\n\n" | |
| 62 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.
| |
| 63 usage += "The commands are:\n\n" | |
| 64 | |
| 65 for _, name := range c.commandNames { | |
| 66 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.
| |
| 67 } | |
| 68 return usage | |
| 69 } | |
| 70 | |
| 71 func (c *commandSet) AddHelpCommand() { | |
| 72 helpCmd := func(args []string) { | |
| 73 fmt.Print(c.Usage(args[0])) | |
| 74 } | |
| 75 c.AddCommand("help", helpCmd, "Prints out this help message.") | |
| 76 } | |
| 77 | |
| 78 func (c *commandSet) RunCommand(args []string) { | |
| 79 if len(args) < 2 { | |
| 80 fmt.Println("No command specified.") | |
| 81 fmt.Print(c.Usage(args[0])) | |
| 82 os.Exit(1) | |
| 83 } | |
| 84 | |
| 85 cmd, ok := c.commandMap[args[1]] | |
| 86 if !ok { | |
| 87 fmt.Printf("%s is not a recognized command.\n") | |
| 88 fmt.Print(c.Usage(args[0])) | |
| 89 os.Exit(1) | |
| 90 } | |
| 91 | |
| 92 cmd.Func(args) | |
| 93 } | |
| OLD | NEW |