Add tests as requested.
This commit is contained in:
parent
baa33cb888
commit
dd3849a7e6
16
context.go
16
context.go
@ -186,23 +186,9 @@ func (a Args) First() string {
|
|||||||
return a.Get(0)
|
return a.Get(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Last - Return the last argument, or else a blank string
|
|
||||||
func (a Args) Last() string {
|
|
||||||
return a.Get(len(a) - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Head - Return the rest of the arguments (not the last one)
|
|
||||||
// or else an empty string slice
|
|
||||||
func (a Args) Head() Args {
|
|
||||||
if len(a) == 1 {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return []string(a)[:len(a)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tail returns the rest of the arguments (not the first one)
|
// Tail returns the rest of the arguments (not the first one)
|
||||||
// or else an empty string slice
|
// or else an empty string slice
|
||||||
func (a Args) Tail() Args {
|
func (a Args) Tail() []string {
|
||||||
if len(a) >= 2 {
|
if len(a) >= 2 {
|
||||||
return []string(a)[1:]
|
return []string(a)[1:]
|
||||||
}
|
}
|
||||||
|
26
help.go
26
help.go
@ -120,7 +120,7 @@ type helpPrinterCustom func(w io.Writer, templ string, data interface{}, customF
|
|||||||
// func(w io.Writer, templ string, data interface{})
|
// func(w io.Writer, templ string, data interface{})
|
||||||
var HelpPrinter helpPrinter = printHelp
|
var HelpPrinter helpPrinter = printHelp
|
||||||
|
|
||||||
// HelPrinterCustom is same as HelpPrinter but
|
// HelpPrinterCustom is same as HelpPrinter but
|
||||||
// takes a custom function for template function map.
|
// takes a custom function for template function map.
|
||||||
var HelpPrinterCustom helpPrinterCustom = printHelpCustom
|
var HelpPrinterCustom helpPrinterCustom = printHelpCustom
|
||||||
|
|
||||||
@ -134,18 +134,20 @@ func ShowAppHelpAndExit(c *Context, exitCode int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ShowAppHelp is an action that displays the help.
|
// ShowAppHelp is an action that displays the help.
|
||||||
func ShowAppHelp(c *Context) error {
|
func ShowAppHelp(c *Context) (err error) {
|
||||||
if c.App.CustomAppHelpTemplate != "" {
|
if c.App.CustomAppHelpTemplate == "" {
|
||||||
if c.App.ExtraInfo != nil {
|
|
||||||
HelpPrinterCustom(c.App.Writer, c.App.CustomAppHelpTemplate, c.App, map[string]interface{}{
|
|
||||||
"ExtraInfo": c.App.ExtraInfo,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
HelpPrinter(c.App.Writer, c.App.CustomAppHelpTemplate, c.App)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
|
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
customAppData := func() map[string]interface{} {
|
||||||
|
if c.App.ExtraInfo == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return map[string]interface{}{
|
||||||
|
"ExtraInfo": c.App.ExtraInfo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HelpPrinterCustom(c.App.Writer, c.App.CustomAppHelpTemplate, c.App, customAppData())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +180,7 @@ func ShowCommandHelp(ctx *Context, command string) error {
|
|||||||
for _, c := range ctx.App.Commands {
|
for _, c := range ctx.App.Commands {
|
||||||
if c.HasName(command) {
|
if c.HasName(command) {
|
||||||
if c.CustomHelpTemplate != "" {
|
if c.CustomHelpTemplate != "" {
|
||||||
HelpPrinter(ctx.App.Writer, c.CustomHelpTemplate, c)
|
HelpPrinterCustom(ctx.App.Writer, c.CustomHelpTemplate, c, nil)
|
||||||
} else {
|
} else {
|
||||||
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
|
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
|
||||||
}
|
}
|
||||||
|
120
help_test.go
120
help_test.go
@ -3,6 +3,8 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -256,6 +258,49 @@ func TestShowSubcommandHelp_CommandAliases(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShowCommandHelp_Customtemplate(t *testing.T) {
|
||||||
|
app := &App{
|
||||||
|
Commands: []Command{
|
||||||
|
{
|
||||||
|
Name: "frobbly",
|
||||||
|
Action: func(ctx *Context) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
HelpName: "foo frobbly",
|
||||||
|
CustomHelpTemplate: `NAME:
|
||||||
|
{{.HelpName}} - {{.Usage}}
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
{{.HelpName}} [FLAGS] TARGET [TARGET ...]
|
||||||
|
|
||||||
|
FLAGS:
|
||||||
|
{{range .VisibleFlags}}{{.}}
|
||||||
|
{{end}}
|
||||||
|
EXAMPLES:
|
||||||
|
1. Frobbly runs with this param locally.
|
||||||
|
$ {{.HelpName}} wobbly
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
output := &bytes.Buffer{}
|
||||||
|
app.Writer = output
|
||||||
|
app.Run([]string{"foo", "help", "frobbly"})
|
||||||
|
|
||||||
|
if strings.Contains(output.String(), "2. Frobbly runs without this param locally.") {
|
||||||
|
t.Errorf("expected output to exclude \"2. Frobbly runs without this param locally.\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "1. Frobbly runs with this param locally.") {
|
||||||
|
t.Errorf("expected output to include \"1. Frobbly runs with this param locally.\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "$ foo frobbly wobbly") {
|
||||||
|
t.Errorf("expected output to include \"$ foo frobbly wobbly\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestShowAppHelp_HiddenCommand(t *testing.T) {
|
func TestShowAppHelp_HiddenCommand(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []Command{
|
||||||
@ -287,3 +332,78 @@ func TestShowAppHelp_HiddenCommand(t *testing.T) {
|
|||||||
t.Errorf("expected output to include \"frobbly\"; got: %q", output.String())
|
t.Errorf("expected output to include \"frobbly\"; got: %q", output.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShowAppHelp_CustomAppTemplate(t *testing.T) {
|
||||||
|
app := &App{
|
||||||
|
Commands: []Command{
|
||||||
|
{
|
||||||
|
Name: "frobbly",
|
||||||
|
Action: func(ctx *Context) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "secretfrob",
|
||||||
|
Hidden: true,
|
||||||
|
Action: func(ctx *Context) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExtraInfo: func() map[string]string {
|
||||||
|
platform := fmt.Sprintf("OS: %s | Arch: %s", runtime.GOOS, runtime.GOARCH)
|
||||||
|
goruntime := fmt.Sprintf("Version: %s | CPUs: %d", runtime.Version(), runtime.NumCPU())
|
||||||
|
return map[string]string{
|
||||||
|
"PLATFORM": platform,
|
||||||
|
"RUNTIME": goruntime,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
CustomAppHelpTemplate: `NAME:
|
||||||
|
{{.Name}} - {{.Usage}}
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
{{.Name}} {{if .VisibleFlags}}[FLAGS] {{end}}COMMAND{{if .VisibleFlags}} [COMMAND FLAGS | -h]{{end}} [ARGUMENTS...]
|
||||||
|
|
||||||
|
COMMANDS:
|
||||||
|
{{range .VisibleCommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
||||||
|
{{end}}{{if .VisibleFlags}}
|
||||||
|
GLOBAL FLAGS:
|
||||||
|
{{range .VisibleFlags}}{{.}}
|
||||||
|
{{end}}{{end}}
|
||||||
|
VERSION:
|
||||||
|
2.0.0
|
||||||
|
{{"\n"}}{{range $key, $value := ExtraInfo}}
|
||||||
|
{{$key}}:
|
||||||
|
{{$value}}
|
||||||
|
{{end}}`,
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "PLATFORM:") ||
|
||||||
|
!strings.Contains(output.String(), "OS:") ||
|
||||||
|
!strings.Contains(output.String(), "Arch:") {
|
||||||
|
t.Errorf("expected output to include \"PLATFORM:, OS: and Arch:\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "RUNTIME:") ||
|
||||||
|
!strings.Contains(output.String(), "Version:") ||
|
||||||
|
!strings.Contains(output.String(), "CPUs:") {
|
||||||
|
t.Errorf("expected output to include \"RUNTIME:, Version: and CPUs:\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "VERSION:") ||
|
||||||
|
!strings.Contains(output.String(), "2.0.0") {
|
||||||
|
t.Errorf("expected output to include \"VERSION:, 2.0.0\"; got: %q", output.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user