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 "flag" | |
10 "fmt" | |
11 "os" | |
12 ) | |
13 | |
14 // This file contains the main() for the mojom tool binary. | |
15 // The mojom tool is used to process .mojom files. | |
16 // | |
17 // The tool is invoked as follows: | |
18 // | |
19 // mojom <command> [<arguments>] | |
20 // | |
21 // where <commands> is one of: | |
22 // - parse | |
23 // - fmt | |
24 // | |
25 // For further information about each command, see the file named | |
26 // <command>_cmd.go for example "parse_cmd.go" and "fmt_cmd.go". | |
27 func main() { | |
28 commands := NewCommandSet() | |
29 commands.AddCommand("parse", parseCmd, "Parses mojom files.") | |
30 commands.AddCommand("fmt", fmtCmd, "Formats a mojom file.") | |
31 commands.AddHelpCommand() | |
32 commands.RunCommand(os.Args) | |
33 } | |
34 | |
35 func ErrorExit(message string) { | |
36 fmt.Fprintf(os.Stderr, "%s\n", message) | |
37 os.Exit(1) | |
38 } | |
39 | |
40 type command struct { | |
41 Name string | |
42 Func func([]string) | |
43 Desc string | |
44 } | |
45 | |
46 type commandSet struct { | |
47 commandNames []string | |
48 commandMap map[string]command | |
49 } | |
50 | |
51 func NewCommandSet() *commandSet { | |
52 commandSet := new(commandSet) | |
53 commandSet.commandMap = make(map[string]command) | |
54 return commandSet | |
55 } | |
56 | |
57 func (c *commandSet) AddCommand(name string, f func([]string), desc string) { | |
58 if _, ok := c.commandMap[name]; ok { | |
59 panic(fmt.Sprintf("Tried to add a second command with the name: %s", name)) | |
60 } | |
61 c.commandNames = append(c.commandNames, name) | |
62 c.commandMap[name] = command{name, f, desc} | |
63 } | |
64 | |
65 func (c *commandSet) Usage(toolName string) string { | |
66 b := bytes.Buffer{} | |
67 b.WriteString(fmt.Sprintf("%s is a tool for managing .mojom files.\n\n", toolName)) | |
68 b.WriteString("Usage:\n\n") | |
69 b.WriteString(fmt.Sprintf("\t%s <command> [<arguments>]\n\n", toolName)) | |
70 b.WriteString("The commands are:\n\n") | |
71 | |
72 for _, name := range c.commandNames { | |
73 b.WriteString(fmt.Sprintf("\t%s\t%s\n", name, c.commandMap[name] .Desc)) | |
74 } | |
75 return b.String() | |
76 } | |
77 | |
78 func (c *commandSet) AddHelpCommand() { | |
79 helpCmd := func(args []string) { | |
80 fmt.Print(c.Usage(args[0])) | |
81 } | |
82 c.AddCommand("help", helpCmd, "Prints out this help message.") | |
83 } | |
84 | |
85 func (c *commandSet) RunCommand(args []string) { | |
86 if len(args) < 2 { | |
87 fmt.Println("No command specified.") | |
88 fmt.Print(c.Usage(args[0])) | |
89 os.Exit(1) | |
90 } | |
91 | |
92 cmd, ok := c.commandMap[args[1]] | |
93 if !ok { | |
94 fmt.Printf("%s is not a recognized command.\n", args[1]) | |
95 fmt.Print(c.Usage(args[0])) | |
96 os.Exit(1) | |
97 } | |
98 | |
99 cmd.Func(args) | |
100 } | |
101 | |
102 // GetFlagDefaults constructs a usage string for the flags of a FlagSet. | |
103 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.
| |
104 b := bytes.Buffer{} | |
105 | |
106 // isZeroValue guesses if the string represents the zero value for a fla g. | |
rudominer
2016/02/20 05:53:49
explain why--how is this used?
azani
2016/02/22 17:37:39
Done.
| |
107 isZeroValue := func(value string) bool { | |
108 switch value { | |
109 case "false": | |
110 return true | |
111 case "": | |
112 return true | |
113 case "0": | |
114 return true | |
115 } | |
116 return false | |
117 } | |
118 | |
119 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.
| |
120 s := fmt.Sprintf(" -%s", f.Name) // Two spaces before -; see ne xt two comments. | |
121 // Four spaces before the tab triggers good alignment | |
122 // for both 4- and 8-space tab stops. | |
123 s += "\n \t" | |
124 s += f.Usage | |
125 if !isZeroValue(f.DefValue) { | |
126 s += fmt.Sprintf(" (default %q)", f.DefValue) | |
127 } | |
128 b.WriteString(fmt.Sprint(s, "\n")) | |
129 }) | |
130 | |
131 return b.String() | |
132 } | |
OLD | NEW |