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

Unified 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: Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: go/src/infra/tools/cr/lib/subcommand/subcommand.go
diff --git a/go/src/infra/tools/cr/lib/subcommand/subcommand.go b/go/src/infra/tools/cr/lib/subcommand/subcommand.go
new file mode 100644
index 0000000000000000000000000000000000000000..4604fb3ab09bb9263f3dd955f054ee6d686eb27d
--- /dev/null
+++ b/go/src/infra/tools/cr/lib/subcommand/subcommand.go
@@ -0,0 +1,46 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/*
+Library for defining subcommands in a structured way.
+*/
+
+package subcommand
+
+import (
+ "flag"
+ "fmt"
+)
+
+type Subcommand struct {
+ ShortHelp string
+ LongHelp string
+ flagFn func(*flag.FlagSet)
+ runFn func(*flag.FlagSet) error
+}
+
+func New(shortHelp string, longHelp string, flagFn func(*flag.FlagSet), runFn func(*flag.FlagSet) error) *Subcommand {
+ return &Subcommand{shortHelp, longHelp, flagFn, runFn}
+}
+
+func (c *Subcommand) Help(flags *flag.FlagSet) {
+ fmt.Println(c.ShortHelp)
+ fmt.Println("")
+ fmt.Println(c.LongHelp)
+ fmt.Println("")
+ flags.PrintDefaults()
+}
+
+func (c *Subcommand) InitFlags(flags *flag.FlagSet) {
+ if c.flagFn != nil {
+ c.flagFn(flags)
+ }
+}
+
+func (c *Subcommand) Run(flags *flag.FlagSet) error {
+ if c.runFn != nil {
+ return c.runFn(flags)
+ }
+ return nil
+}

Powered by Google App Engine
This is Rietveld 408576698