Merge remote-tracking branch 'origin/master' into exit-nonzero-for-unknown-subcommand
This commit is contained in:
commit
5a5fa990e1
@ -5,6 +5,9 @@
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
### Added
|
### Added
|
||||||
- Pluggable flag-level help text rendering via `cli.DefaultFlagStringFunc`
|
- Pluggable flag-level help text rendering via `cli.DefaultFlagStringFunc`
|
||||||
|
- `context.GlobalBoolT` was added as an analogue to `context.GlobalBool`
|
||||||
|
- Support for hiding commands by setting `Hidden: true` -- this will hide the
|
||||||
|
commands in help output
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- `Float64Flag`, `IntFlag`, and `DurationFlag` default values are no longer
|
- `Float64Flag`, `IntFlag`, and `DurationFlag` default values are no longer
|
||||||
|
69
app.go
69
app.go
@ -82,8 +82,12 @@ 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{}
|
||||||
|
|
||||||
|
didSetup bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tries to find out when this binary was compiled.
|
// Tries to find out when this binary was compiled.
|
||||||
@ -111,8 +115,16 @@ func NewApp() *App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
|
// Setup runs initialization code to ensure all data structures are ready for
|
||||||
func (a *App) Run(arguments []string) (err error) {
|
// `Run` or inspection prior to `Run`. It is internally called by `Run`, but
|
||||||
|
// will return early if setup has already happened.
|
||||||
|
func (a *App) Setup() {
|
||||||
|
if a.didSetup {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
a.didSetup = true
|
||||||
|
|
||||||
if a.Author != "" || a.Email != "" {
|
if a.Author != "" || a.Email != "" {
|
||||||
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
||||||
}
|
}
|
||||||
@ -148,6 +160,11 @@ func (a *App) Run(arguments []string) (err error) {
|
|||||||
if !a.HideVersion {
|
if !a.HideVersion {
|
||||||
a.appendFlag(VersionFlag)
|
a.appendFlag(VersionFlag)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
|
||||||
|
func (a *App) Run(arguments []string) (err error) {
|
||||||
|
a.Setup()
|
||||||
|
|
||||||
// parse flags
|
// parse flags
|
||||||
set := flagSet(a.Name, a.Flags)
|
set := flagSet(a.Name, a.Flags)
|
||||||
@ -228,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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -357,11 +374,41 @@ func (a *App) Command(name string) *Command {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returnes the array containing all the categories with the commands they contain
|
// Categories returns a slice containing all the categories with the commands they contain
|
||||||
func (a *App) Categories() CommandCategories {
|
func (a *App) Categories() CommandCategories {
|
||||||
return a.categories
|
return a.categories
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VisibleCategories returns a slice of categories and commands that are
|
||||||
|
// Hidden=false
|
||||||
|
func (a *App) VisibleCategories() []*CommandCategory {
|
||||||
|
ret := []*CommandCategory{}
|
||||||
|
for _, category := range a.categories {
|
||||||
|
if visible := func() *CommandCategory {
|
||||||
|
for _, command := range category.Commands {
|
||||||
|
if !command.Hidden {
|
||||||
|
return category
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}(); visible != nil {
|
||||||
|
ret = append(ret, visible)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
||||||
|
func (a *App) VisibleCommands() []Command {
|
||||||
|
ret := []Command{}
|
||||||
|
for _, command := range a.Commands {
|
||||||
|
if !command.Hidden {
|
||||||
|
ret = append(ret, command)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
// VisibleFlags returns a slice of the Flags with Hidden=false
|
// VisibleFlags returns a slice of the Flags with Hidden=false
|
||||||
func (a *App) VisibleFlags() []Flag {
|
func (a *App) VisibleFlags() []Flag {
|
||||||
return visibleFlags(a.Flags)
|
return visibleFlags(a.Flags)
|
||||||
@ -377,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)
|
||||||
@ -422,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
|
||||||
|
145
app_test.go
145
app_test.go
@ -304,6 +304,48 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
|
|||||||
expect(t, args[2], "notAFlagAtAll")
|
expect(t, args[2], "notAFlagAtAll")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApp_VisibleCommands(t *testing.T) {
|
||||||
|
app := NewApp()
|
||||||
|
app.Commands = []Command{
|
||||||
|
Command{
|
||||||
|
Name: "frob",
|
||||||
|
HelpName: "foo frob",
|
||||||
|
Action: func(_ *Context) error { return nil },
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "frib",
|
||||||
|
HelpName: "foo frib",
|
||||||
|
Hidden: true,
|
||||||
|
Action: func(_ *Context) error { return nil },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
app.Setup()
|
||||||
|
expected := []Command{
|
||||||
|
app.Commands[0],
|
||||||
|
app.Commands[2], // help
|
||||||
|
}
|
||||||
|
actual := app.VisibleCommands()
|
||||||
|
expect(t, len(expected), len(actual))
|
||||||
|
for i, actualCommand := range actual {
|
||||||
|
expectedCommand := expected[i]
|
||||||
|
|
||||||
|
if expectedCommand.Action != nil {
|
||||||
|
// comparing func addresses is OK!
|
||||||
|
expect(t, fmt.Sprintf("%p", expectedCommand.Action), fmt.Sprintf("%p", actualCommand.Action))
|
||||||
|
}
|
||||||
|
|
||||||
|
// nil out funcs, as they cannot be compared
|
||||||
|
// (https://github.com/golang/go/issues/8554)
|
||||||
|
expectedCommand.Action = nil
|
||||||
|
actualCommand.Action = nil
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(expectedCommand, actualCommand) {
|
||||||
|
t.Errorf("expected\n%#v\n!=\n%#v", expectedCommand, actualCommand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApp_Float64Flag(t *testing.T) {
|
func TestApp_Float64Flag(t *testing.T) {
|
||||||
var meters float64
|
var meters float64
|
||||||
|
|
||||||
@ -1129,6 +1171,109 @@ func TestApp_Run_Categories(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApp_VisibleCategories(t *testing.T) {
|
||||||
|
app := NewApp()
|
||||||
|
app.Name = "visible-categories"
|
||||||
|
app.Commands = []Command{
|
||||||
|
Command{
|
||||||
|
Name: "command1",
|
||||||
|
Category: "1",
|
||||||
|
HelpName: "foo command1",
|
||||||
|
Hidden: true,
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "command2",
|
||||||
|
Category: "2",
|
||||||
|
HelpName: "foo command2",
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "command3",
|
||||||
|
Category: "3",
|
||||||
|
HelpName: "foo command3",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := []*CommandCategory{
|
||||||
|
&CommandCategory{
|
||||||
|
Name: "2",
|
||||||
|
Commands: []Command{
|
||||||
|
app.Commands[1],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&CommandCategory{
|
||||||
|
Name: "3",
|
||||||
|
Commands: []Command{
|
||||||
|
app.Commands[2],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
app.Setup()
|
||||||
|
expect(t, expected, app.VisibleCategories())
|
||||||
|
|
||||||
|
app = NewApp()
|
||||||
|
app.Name = "visible-categories"
|
||||||
|
app.Commands = []Command{
|
||||||
|
Command{
|
||||||
|
Name: "command1",
|
||||||
|
Category: "1",
|
||||||
|
HelpName: "foo command1",
|
||||||
|
Hidden: true,
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "command2",
|
||||||
|
Category: "2",
|
||||||
|
HelpName: "foo command2",
|
||||||
|
Hidden: true,
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "command3",
|
||||||
|
Category: "3",
|
||||||
|
HelpName: "foo command3",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected = []*CommandCategory{
|
||||||
|
&CommandCategory{
|
||||||
|
Name: "3",
|
||||||
|
Commands: []Command{
|
||||||
|
app.Commands[2],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
app.Setup()
|
||||||
|
expect(t, expected, app.VisibleCategories())
|
||||||
|
|
||||||
|
app = NewApp()
|
||||||
|
app.Name = "visible-categories"
|
||||||
|
app.Commands = []Command{
|
||||||
|
Command{
|
||||||
|
Name: "command1",
|
||||||
|
Category: "1",
|
||||||
|
HelpName: "foo command1",
|
||||||
|
Hidden: true,
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "command2",
|
||||||
|
Category: "2",
|
||||||
|
HelpName: "foo command2",
|
||||||
|
Hidden: true,
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "command3",
|
||||||
|
Category: "3",
|
||||||
|
HelpName: "foo command3",
|
||||||
|
Hidden: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected = []*CommandCategory{}
|
||||||
|
|
||||||
|
app.Setup()
|
||||||
|
expect(t, expected, app.VisibleCategories())
|
||||||
|
}
|
||||||
|
|
||||||
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
app.Action = func(c *Context) error { return nil }
|
app.Action = func(c *Context) error { return nil }
|
||||||
|
11
category.go
11
category.go
@ -28,3 +28,14 @@ func (c CommandCategories) AddCommand(category string, command Command) CommandC
|
|||||||
}
|
}
|
||||||
return append(c, &CommandCategory{Name: category, Commands: []Command{command}})
|
return append(c, &CommandCategory{Name: category, Commands: []Command{command}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
||||||
|
func (c *CommandCategory) VisibleCommands() []Command {
|
||||||
|
ret := []Command{}
|
||||||
|
for _, command := range c.Commands {
|
||||||
|
if !command.Hidden {
|
||||||
|
ret = append(ret, command)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
@ -48,6 +48,8 @@ type Command struct {
|
|||||||
SkipFlagParsing bool
|
SkipFlagParsing bool
|
||||||
// Boolean to hide built-in help command
|
// Boolean to hide built-in help command
|
||||||
HideHelp bool
|
HideHelp bool
|
||||||
|
// Boolean to hide this command from help or completion
|
||||||
|
Hidden bool
|
||||||
|
|
||||||
// Full name of command for help, defaults to full command name, including parent commands.
|
// Full name of command for help, defaults to full command name, including parent commands.
|
||||||
HelpName string
|
HelpName string
|
||||||
|
@ -104,6 +104,14 @@ func (c *Context) GlobalBool(name string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Looks up the value of a global bool flag, returns true if no bool flag exists
|
||||||
|
func (c *Context) GlobalBoolT(name string) bool {
|
||||||
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupBoolT(name, fs)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Looks up the value of a global string flag, returns "" if no string flag exists
|
// Looks up the value of a global string flag, returns "" if no string flag exists
|
||||||
func (c *Context) GlobalString(name string) string {
|
func (c *Context) GlobalString(name string) string {
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
@ -82,6 +82,30 @@ func TestContext_BoolT(t *testing.T) {
|
|||||||
expect(t, c.BoolT("myflag"), true)
|
expect(t, c.BoolT("myflag"), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext_GlobalBool(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
|
||||||
|
globalSet := flag.NewFlagSet("test-global", 0)
|
||||||
|
globalSet.Bool("myflag", false, "doc")
|
||||||
|
globalCtx := NewContext(nil, globalSet, nil)
|
||||||
|
|
||||||
|
c := NewContext(nil, set, globalCtx)
|
||||||
|
expect(t, c.GlobalBool("myflag"), false)
|
||||||
|
expect(t, c.GlobalBool("nope"), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_GlobalBoolT(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
|
||||||
|
globalSet := flag.NewFlagSet("test-global", 0)
|
||||||
|
globalSet.Bool("myflag", true, "doc")
|
||||||
|
globalCtx := NewContext(nil, globalSet, nil)
|
||||||
|
|
||||||
|
c := NewContext(nil, set, globalCtx)
|
||||||
|
expect(t, c.GlobalBoolT("myflag"), true)
|
||||||
|
expect(t, c.GlobalBoolT("nope"), false)
|
||||||
|
}
|
||||||
|
|
||||||
func TestContext_Args(t *testing.T) {
|
func TestContext_Args(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
|
@ -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
|
||||||
|
@ -34,7 +34,7 @@ func TestHandleExitCoder_ExitCoder(t *testing.T) {
|
|||||||
|
|
||||||
defer func() { OsExiter = os.Exit }()
|
defer func() { OsExiter = os.Exit }()
|
||||||
|
|
||||||
HandleExitCoder(NewExitError("galactic perimiter breach", 9))
|
HandleExitCoder(NewExitError("galactic perimeter breach", 9))
|
||||||
|
|
||||||
expect(t, exitCode, 9)
|
expect(t, exitCode, 9)
|
||||||
expect(t, called, true)
|
expect(t, called, true)
|
||||||
@ -51,7 +51,7 @@ func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) {
|
|||||||
|
|
||||||
defer func() { OsExiter = os.Exit }()
|
defer func() { OsExiter = os.Exit }()
|
||||||
|
|
||||||
exitErr := NewExitError("galactic perimiter breach", 9)
|
exitErr := NewExitError("galactic perimeter breach", 9)
|
||||||
err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr)
|
err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr)
|
||||||
HandleExitCoder(err)
|
HandleExitCoder(err)
|
||||||
|
|
||||||
|
2
flag.go
2
flag.go
@ -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
|
||||||
|
29
help.go
29
help.go
@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -21,15 +22,15 @@ VERSION:
|
|||||||
{{.Version}}
|
{{.Version}}
|
||||||
{{end}}{{end}}{{if len .Authors}}
|
{{end}}{{end}}{{if len .Authors}}
|
||||||
AUTHOR(S):
|
AUTHOR(S):
|
||||||
{{range .Authors}}{{ . }}{{end}}
|
{{range .Authors}}{{.}}{{end}}
|
||||||
{{end}}{{if .Commands}}
|
{{end}}{{if .VisibleCommands}}
|
||||||
COMMANDS:{{range .Categories}}{{if .Name}}
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
||||||
{{.Name}}{{ ":" }}{{end}}{{range .Commands}}
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
||||||
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
|
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{"\t"}}{{.Usage}}{{end}}
|
||||||
{{end}}{{end}}{{if .VisibleFlags}}
|
{{end}}{{end}}{{if .VisibleFlags}}
|
||||||
GLOBAL OPTIONS:
|
GLOBAL OPTIONS:
|
||||||
{{range .VisibleFlags}}{{.}}
|
{{range .VisibleFlags}}{{.}}
|
||||||
{{end}}{{end}}{{if .Copyright }}
|
{{end}}{{end}}{{if .Copyright}}
|
||||||
COPYRIGHT:
|
COPYRIGHT:
|
||||||
{{.Copyright}}
|
{{.Copyright}}
|
||||||
{{end}}
|
{{end}}
|
||||||
@ -52,7 +53,7 @@ DESCRIPTION:
|
|||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
{{range .VisibleFlags}}{{.}}
|
{{range .VisibleFlags}}{{.}}
|
||||||
{{end}}{{ end }}
|
{{end}}{{end}}
|
||||||
`
|
`
|
||||||
|
|
||||||
// The text template for the subcommand help topic.
|
// The text template for the subcommand help topic.
|
||||||
@ -64,9 +65,9 @@ var SubcommandHelpTemplate = `NAME:
|
|||||||
USAGE:
|
USAGE:
|
||||||
{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
||||||
|
|
||||||
COMMANDS:{{range .Categories}}{{if .Name}}
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
||||||
{{.Name}}{{ ":" }}{{end}}{{range .Commands}}
|
{{.Name}}:{{end}}{{range .VisibleCommands}}
|
||||||
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}{{end}}
|
{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{"\t"}}{{.Usage}}{{end}}
|
||||||
{{end}}{{if .VisibleFlags}}
|
{{end}}{{if .VisibleFlags}}
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
{{range .VisibleFlags}}{{.}}
|
{{range .VisibleFlags}}{{.}}
|
||||||
@ -119,6 +120,9 @@ func ShowAppHelp(c *Context) {
|
|||||||
// Prints the list of subcommands as the default app completion method
|
// Prints the list of subcommands as the default app completion method
|
||||||
func DefaultAppComplete(c *Context) {
|
func DefaultAppComplete(c *Context) {
|
||||||
for _, command := range c.App.Commands {
|
for _, command := range c.App.Commands {
|
||||||
|
if command.Hidden {
|
||||||
|
continue
|
||||||
|
}
|
||||||
for _, name := range command.Names() {
|
for _, name := range command.Names() {
|
||||||
fmt.Fprintln(c.App.Writer, name)
|
fmt.Fprintln(c.App.Writer, name)
|
||||||
}
|
}
|
||||||
@ -188,7 +192,10 @@ func printHelp(out io.Writer, templ string, data interface{}) {
|
|||||||
err := t.Execute(w, data)
|
err := t.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 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 could send this to os.Stderr if we need.
|
// we can do to recover.
|
||||||
|
if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
|
||||||
|
fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Flush()
|
w.Flush()
|
||||||
|
32
help_test.go
32
help_test.go
@ -168,3 +168,35 @@ func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) {
|
|||||||
t.Fatalf("expected exit value = 3, got %d instead", exitErr.exitCode)
|
t.Fatalf("expected exit value = 3, got %d instead", exitErr.exitCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShowAppHelp_HiddenCommand(t *testing.T) {
|
||||||
|
app := &App{
|
||||||
|
Commands: []Command{
|
||||||
|
Command{
|
||||||
|
Name: "frobbly",
|
||||||
|
Action: func(ctx *Context) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Command{
|
||||||
|
Name: "secretfrob",
|
||||||
|
Hidden: true,
|
||||||
|
Action: func(ctx *Context) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
app.Writer = output
|
||||||
|
app.Run([]string{"app", "--help"})
|
||||||
|
|
||||||
|
if strings.Contains(output.String(), "secretfrob") {
|
||||||
|
t.Errorf("expected output to exclude \"secretfrob\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "frobbly") {
|
||||||
|
t.Errorf("expected output to include \"frobbly\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user