Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 /* | |
| 6 Library for defining subcommands in a structured way. | |
| 7 */ | |
| 8 | |
| 9 // Package subcommand provides a simple framework for defining subcommands. | |
| 10 package subcommand | |
| 11 | |
| 12 import ( | |
| 13 "flag" | |
| 14 "fmt" | |
| 15 "os" | |
| 16 "text/tabwriter" | |
| 17 ) | |
| 18 | |
| 19 // Subcommand represents a single subcommand, with short and long versions of | |
| 20 // its help (usage) text, a function which adds command-line flags to a | |
| 21 // pre-existing flag.FlagSet, and a function which will be called to actually | |
| 22 // execute the subcommand. | |
| 23 type Subcommand struct { | |
| 24 shortHelp string | |
| 25 longHelp string | |
| 26 flagFn func(*flag.FlagSet) | |
| 27 runFn func(*flag.FlagSet) error | |
| 28 } | |
| 29 | |
| 30 // Subcommands is a map of the string names of all registered subcommands to | |
| 31 // the Subcommand structs holding their vital data. | |
| 32 var Subcommands map[string]*Subcommand | |
|
seanmccullough1
2016/05/06 00:43:54
var Subcommands = make(map[string]*Subcommand)
an
agable
2016/05/10 00:34:11
...I could have sworn I tried exactly that, and on
| |
| 33 | |
| 34 // TabulateSubcommands prints out all registered Subcommand names and their | |
| 35 // short help strings. If no formatter is supplied, it uses a sane default. | |
|
seanmccullough1
2016/05/06 00:43:54
Might want to mention that it flushes formatter be
agable
2016/05/10 00:34:11
Yeah, removed the parameter.
| |
| 36 func TabulateSubcommands(formatter *tabwriter.Writer) { | |
| 37 if formatter == nil { | |
| 38 formatter = tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) | |
| 39 } | |
| 40 for key, value := range Subcommands { | |
| 41 fmt.Fprintf(formatter, "\t%v\t%v\n", key, value.shortHelp) | |
| 42 } | |
| 43 formatter.Flush() | |
| 44 } | |
| 45 | |
| 46 // New creates a new Subcommand struct and returns a reference to it. | |
| 47 func New(name string, shortHelp string, longHelp string, flagFn func(*flag.FlagS et), runFn func(*flag.FlagSet) error) *Subcommand { | |
| 48 ret := &Subcommand{shortHelp, longHelp, flagFn, runFn} | |
| 49 Subcommands[name] = ret | |
|
seanmccullough1
2016/05/06 00:43:54
bonus points: check Subcommands for existence of n
agable
2016/05/10 00:34:11
Done
| |
| 50 return ret | |
| 51 } | |
| 52 | |
| 53 // Help prints the short and long help messages, as well as the autogenerated | |
| 54 // flags documentation. | |
| 55 func (c *Subcommand) Help(flags *flag.FlagSet) { | |
| 56 fmt.Printf("%s\n\n%s\n\n", c.shortHelp, c.longHelp) | |
| 57 flags.PrintDefaults() | |
| 58 } | |
| 59 | |
| 60 // InitFlags adds the subcommand's flags to a FlagSet, if flagFn is defined. | |
| 61 func (c *Subcommand) InitFlags(flags *flag.FlagSet) { | |
| 62 if c.flagFn != nil { | |
| 63 c.flagFn(flags) | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 // Run executes the subcommand's runFn, if it is defined. | |
| 68 func (c *Subcommand) Run(flags *flag.FlagSet) error { | |
| 69 if c.runFn != nil { | |
| 70 return c.runFn(flags) | |
| 71 } | |
| 72 return nil | |
| 73 } | |
| 74 | |
| 75 func init() { | |
| 76 Subcommands = map[string]*Subcommand{} | |
| 77 } | |
| OLD | NEW |