Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
93 lines (78 sloc) 2.63 KB
// Copyright 2013 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
// A Command represents a subcommand of rx.
// The specified fields should be specified in the Command literal, usually at
// the top of the file. Due to circular dependencies, the Run command will
// typically have to be set in an init if it uses flags defined by the command.
// The abbreviation should be used for an abbreviation that is not a strict
// prefix of the command name, as those work automatically. Most commands will
// not need one, as the prefixes will work well enough.
type Command struct {
// Run is called when the command is invoked
Run func(cmd *Command, args ...string)
// Flag contains Command-line flags
Flag flag.FlagSet
// These should be set in the literal:
Name string // The name of the command (all lower case, one word)
Usage string // The symbolic, human-readable argument description
Summary string // The short description of the command (short sentence)
Help string // The detailed command information (multiple paragraphs, etc)
Abbrev string // Alternate abbreviation for the command
exit int // Exit code
func (c *Command) Exec(args []string) {
c.Flag.Usage = func() {
helpFunc(c, c.Name)
defer func() {
if r := recover(); r != nil {
if _, ok := r.(fatal); ok {
c.Run(c, c.Flag.Args()...)
if c.exit != 0 {
func (c *Command) BadArgs(errFormat string, args ...interface{}) {
fmt.Fprintf(stdout, "error: "+errFormat+"\n\n", args...)
helpFunc(c, c.Name)
// Errorf prints out a formatted error with the right prefixes.
func (c *Command) Errorf(errFormat string, args ...interface{}) {
fmt.Fprintf(stdout, c.Name+": error: "+errFormat+"\n", args...)
if c.exit == 0 {
c.exit = 1
// Fatalf is like Errorf except the stack unwinds up to the Exec call before
// exiting the application with status code 1.
func (c *Command) Fatalf(errFormat string, args ...interface{}) {
c.Errorf(errFormat, args...)
type fatal struct{}
You can’t perform that action at this time.