Merge remote-tracking branch 'origin/master' into psmit-hidden_command

This commit is contained in:
Dan Buch 2016-05-08 18:44:38 -04:00
commit 97d2df6dd7
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
6 changed files with 37 additions and 8 deletions

View File

@ -5,8 +5,9 @@ go:
- 1.1.2 - 1.1.2
- 1.2.2 - 1.2.2
- 1.3.3 - 1.3.3
- 1.4.2 - 1.4
- 1.5.1 - 1.5.4
- 1.6.2
- tip - tip
matrix: matrix:

View File

@ -2,6 +2,7 @@
[![Build Status](https://travis-ci.org/codegangsta/cli.svg?branch=master)](https://travis-ci.org/codegangsta/cli) [![Build Status](https://travis-ci.org/codegangsta/cli.svg?branch=master)](https://travis-ci.org/codegangsta/cli)
[![GoDoc](https://godoc.org/github.com/codegangsta/cli?status.svg)](https://godoc.org/github.com/codegangsta/cli) [![GoDoc](https://godoc.org/github.com/codegangsta/cli?status.svg)](https://godoc.org/github.com/codegangsta/cli)
[![codebeat](https://codebeat.co/badges/0a8f30aa-f975-404b-b878-5fab3ae1cc5f)](https://codebeat.co/projects/github-com-codegangsta-cli) [![codebeat](https://codebeat.co/badges/0a8f30aa-f975-404b-b878-5fab3ae1cc5f)](https://codebeat.co/projects/github-com-codegangsta-cli)
[![Go Report Card](https://goreportcard.com/badge/codegangsta/cli)](https://goreportcard.com/report/codegangsta/cli)
# cli # cli
@ -46,6 +47,9 @@ func main() {
This app will run and show help text, but is not very useful. Let's give an action to execute and some help documentation: This app will run and show help text, but is not very useful. Let's give an action to execute and some help documentation:
<!-- {
"output": "boom! I say!"
} -->
``` go ``` go
package main package main
@ -77,6 +81,9 @@ Being a programmer can be a lonely job. Thankfully by the power of automation th
Start by creating a directory named `greet`, and within it, add a file, `greet.go` with the following code in it: Start by creating a directory named `greet`, and within it, add a file, `greet.go` with the following code in it:
<!-- {
"output": "Hello friend!"
} -->
``` go ``` go
package main package main
@ -210,6 +217,7 @@ Sometimes it's useful to specify a flag's value within the usage string itself.
indicated with back quotes. indicated with back quotes.
For example this: For example this:
```go ```go
cli.StringFlag{ cli.StringFlag{
Name: "config, c", Name: "config, c",
@ -504,6 +512,9 @@ templates are exposed as variables `AppHelpTemplate`, `CommandHelpTemplate`, and
is possible by assigning a compatible func to the `cli.HelpPrinter` variable, is possible by assigning a compatible func to the `cli.HelpPrinter` variable,
e.g.: e.g.:
<!-- {
"output": "Ha HA. I pwnd the help!!1"
} -->
``` go ``` go
package main package main

18
app.go
View File

@ -82,6 +82,8 @@ type App struct {
Email string Email string
// Writer writer to write output to // Writer writer to write output to
Writer io.Writer Writer io.Writer
// ErrWriter writes error output
ErrWriter io.Writer
// Other custom info // Other custom info
Metadata map[string]interface{} Metadata map[string]interface{}
@ -243,11 +245,11 @@ func (a *App) Run(arguments []string) (err error) {
// DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling // DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling
func (a *App) RunAndExitOnError() { func (a *App) RunAndExitOnError() {
fmt.Fprintf(os.Stderr, fmt.Fprintf(a.errWriter(),
"DEPRECATED cli.App.RunAndExitOnError. %s See %s\n", "DEPRECATED cli.App.RunAndExitOnError. %s See %s\n",
contactSysadmin, runAndExitOnErrorDeprecationURL) contactSysadmin, runAndExitOnErrorDeprecationURL)
if err := a.Run(os.Args); err != nil { if err := a.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(a.errWriter(), err)
OsExiter(1) OsExiter(1)
} }
} }
@ -422,6 +424,16 @@ func (a *App) hasFlag(flag Flag) bool {
return false return false
} }
func (a *App) errWriter() io.Writer {
// When the app ErrWriter is nil use the package level one.
if a.ErrWriter == nil {
return ErrWriter
}
return a.ErrWriter
}
func (a *App) appendFlag(flag Flag) { func (a *App) appendFlag(flag Flag) {
if !a.hasFlag(flag) { if !a.hasFlag(flag) {
a.Flags = append(a.Flags, flag) a.Flags = append(a.Flags, flag)
@ -467,7 +479,7 @@ func HandleAction(action interface{}, context *Context) (err error) {
vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)}) vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)})
if len(vals) == 0 { if len(vals) == 0 {
fmt.Fprintf(os.Stderr, fmt.Fprintf(ErrWriter,
"DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n", "DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n",
contactSysadmin, appActionDeprecationURL) contactSysadmin, appActionDeprecationURL)
return nil return nil

View File

@ -2,12 +2,17 @@ package cli
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
) )
var OsExiter = os.Exit var OsExiter = os.Exit
// ErrWriter is used to write errors to the user. This can be anything
// implementing the io.Writer interface and defaults to os.Stderr.
var ErrWriter io.Writer = os.Stderr
type MultiError struct { type MultiError struct {
Errors []error Errors []error
} }
@ -69,7 +74,7 @@ func HandleExitCoder(err error) {
if exitErr, ok := err.(ExitCoder); ok { if exitErr, ok := err.(ExitCoder); ok {
if err.Error() != "" { if err.Error() != "" {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(ErrWriter, err)
} }
OsExiter(exitErr.ExitCode()) OsExiter(exitErr.ExitCode())
return return

View File

@ -220,7 +220,7 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
err := newVal.Set(s) err := newVal.Set(s)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(ErrWriter, err.Error())
} }
} }
f.Value = newVal f.Value = newVal

View File

@ -194,7 +194,7 @@ func printHelp(out io.Writer, templ string, data interface{}) {
// If the writer is closed, t.Execute will fail, and there's nothing // If the writer is closed, t.Execute will fail, and there's nothing
// we can do to recover. // we can do to recover.
if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" { if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
fmt.Fprintf(os.Stderr, "CLI TEMPLATE ERROR: %#v\n", err) fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
} }
return return
} }