Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: go/src/infra/tools/cr/lib/subcommand/subcommand.go

Issue 1929153002: Add beginnings of new cr command (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Style and documentation Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 )
16
17 // Subcommand represents a single subcommand, with short and long versions of
18 // its help (usage) text, a function which adds command-line flags to a
19 // pre-existing flag.FlagSet, and a function which will be called to actually
20 // execute the subcommand.
21 type Subcommand struct {
seanmccullough1 2016/04/29 00:54:36 SubCmd would be a fine name too.
22 ShortHelp string
seanmccullough1 2016/04/29 00:54:36 godoc for ShortHelp or don't export it (doesn't ap
agable 2016/05/05 23:59:43 main.go directly access ShortHelp to print the lis
23 LongHelp string
24 flagFn func(*flag.FlagSet)
25 runFn func(*flag.FlagSet) error
26 }
27
28 // New creates a new Subcommand struct and returns a reference to it.
29 func New(shortHelp string, longHelp string, flagFn func(*flag.FlagSet), runFn fu nc(*flag.FlagSet) error) *Subcommand {
30 return &Subcommand{shortHelp, longHelp, flagFn, runFn}
31 }
32
33 // Help prints the short and long help messages, as well as the autogenerated
34 // flags documentation.
35 func (c *Subcommand) Help(flags *flag.FlagSet) {
36 fmt.Println(c.ShortHelp)
seanmccullough1 2016/04/29 00:54:36 fmt.Printf("%s\n\n%s\n\n", c.ShortHelp, c.LongHelp
agable 2016/05/05 23:59:43 Done.
37 fmt.Println("")
38 fmt.Println(c.LongHelp)
39 fmt.Println("")
40 flags.PrintDefaults()
41 }
42
43 // InitFlags adds the subcommand's flags to a FlagSet, if flagFn is defined.
44 func (c *Subcommand) InitFlags(flags *flag.FlagSet) {
45 if c.flagFn != nil {
46 c.flagFn(flags)
47 }
48 }
49
50 // Run executes the subcommand's runFn, if it is defined.
51 func (c *Subcommand) Run(flags *flag.FlagSet) error {
52 if c.runFn != nil {
53 return c.runFn(flags)
54 }
55 return nil
56 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698