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