fix tests
This commit is contained in:
parent
ecb0b5ac0a
commit
aced6e8739
18
app.go
18
app.go
@ -13,6 +13,8 @@ import (
|
|||||||
type App struct {
|
type App struct {
|
||||||
// The name of the program. Defaults to os.Args[0]
|
// The name of the program. Defaults to os.Args[0]
|
||||||
Name string
|
Name string
|
||||||
|
// Name of command for help, defaults to Name
|
||||||
|
HelpName string
|
||||||
// Description of the program.
|
// Description of the program.
|
||||||
Usage string
|
Usage string
|
||||||
// Description of the program argument format.
|
// Description of the program argument format.
|
||||||
@ -69,8 +71,8 @@ func compileTime() time.Time {
|
|||||||
func NewApp() *App {
|
func NewApp() *App {
|
||||||
return &App{
|
return &App{
|
||||||
Name: os.Args[0],
|
Name: os.Args[0],
|
||||||
|
HelpName: os.Args[0],
|
||||||
Usage: "A new cli application",
|
Usage: "A new cli application",
|
||||||
ArgsUsage: "[arguments...]",
|
|
||||||
Version: "0.0.0",
|
Version: "0.0.0",
|
||||||
BashComplete: DefaultAppComplete,
|
BashComplete: DefaultAppComplete,
|
||||||
Action: helpCommand.Action,
|
Action: helpCommand.Action,
|
||||||
@ -85,6 +87,13 @@ func (a *App) Run(arguments []string) (err error) {
|
|||||||
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newCmds := []Command{}
|
||||||
|
for _, c := range a.Commands {
|
||||||
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
||||||
|
newCmds = append(newCmds, c)
|
||||||
|
}
|
||||||
|
a.Commands = newCmds
|
||||||
|
|
||||||
// append help to commands
|
// append help to commands
|
||||||
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
||||||
a.Commands = append(a.Commands, helpCommand)
|
a.Commands = append(a.Commands, helpCommand)
|
||||||
@ -188,6 +197,13 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newCmds := []Command{}
|
||||||
|
for _, c := range a.Commands {
|
||||||
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
||||||
|
newCmds = append(newCmds, c)
|
||||||
|
}
|
||||||
|
a.Commands = newCmds
|
||||||
|
|
||||||
// append flags
|
// append flags
|
||||||
if a.EnableBashCompletion {
|
if a.EnableBashCompletion {
|
||||||
a.appendFlag(BashCompletionFlag)
|
a.appendFlag(BashCompletionFlag)
|
||||||
|
@ -90,10 +90,10 @@ func ExampleAppHelp() {
|
|||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
// Output:
|
// Output:
|
||||||
// NAME:
|
// NAME:
|
||||||
// describeit - use it to see a description
|
// greet describeit - use it to see a description
|
||||||
//
|
//
|
||||||
// USAGE:
|
// USAGE:
|
||||||
// command describeit [arguments...]
|
// greet describeit [arguments...]
|
||||||
//
|
//
|
||||||
// DESCRIPTION:
|
// DESCRIPTION:
|
||||||
// This is how we describe describeit the function
|
// This is how we describe describeit the function
|
||||||
@ -737,7 +737,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
|
|||||||
app := NewApp()
|
app := NewApp()
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
app.Writer = buf
|
app.Writer = buf
|
||||||
|
app.Name = "command"
|
||||||
subCmd := Command{
|
subCmd := Command{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Usage: "does bar things",
|
Usage: "does bar things",
|
||||||
@ -755,7 +755,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
output := buf.String()
|
output := buf.String()
|
||||||
if !strings.Contains(output, "foo bar - does bar things") {
|
if !strings.Contains(output, "command foo bar - does bar things") {
|
||||||
t.Errorf("expected full path to subcommand: %s", output)
|
t.Errorf("expected full path to subcommand: %s", output)
|
||||||
}
|
}
|
||||||
if !strings.Contains(output, "command foo bar [arguments...]") {
|
if !strings.Contains(output, "command foo bar [arguments...]") {
|
||||||
|
10
command.go
10
command.go
@ -39,9 +39,8 @@ type Command struct {
|
|||||||
// Boolean to hide built-in help command
|
// Boolean to hide built-in help command
|
||||||
HideHelp bool
|
HideHelp bool
|
||||||
|
|
||||||
// Name of parent command for help, defaults to app.Name and parent
|
// Name of command for help, defaults to full command name
|
||||||
// command.Name.
|
HelpName string
|
||||||
ParentName string
|
|
||||||
commandNamePath []string
|
commandNamePath []string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,14 +157,12 @@ func (c Command) startApp(ctx *Context) error {
|
|||||||
|
|
||||||
// set the name and usage
|
// set the name and usage
|
||||||
app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
|
app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
|
||||||
|
app.HelpName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
|
||||||
if c.Description != "" {
|
if c.Description != "" {
|
||||||
app.Usage = c.Description
|
app.Usage = c.Description
|
||||||
} else {
|
} else {
|
||||||
app.Usage = c.Usage
|
app.Usage = c.Usage
|
||||||
}
|
}
|
||||||
if c.ArgsUsage == "" {
|
|
||||||
c.ArgsUsage = "[arguments...]"
|
|
||||||
}
|
|
||||||
|
|
||||||
// set CommandNotFound
|
// set CommandNotFound
|
||||||
app.CommandNotFound = ctx.App.CommandNotFound
|
app.CommandNotFound = ctx.App.CommandNotFound
|
||||||
@ -199,7 +196,6 @@ func (c Command) startApp(ctx *Context) error {
|
|||||||
|
|
||||||
var newCmds []Command
|
var newCmds []Command
|
||||||
for _, cc := range app.Commands {
|
for _, cc := range app.Commands {
|
||||||
cc.ParentName = app.Name
|
|
||||||
cc.commandNamePath = []string{c.Name, cc.Name}
|
cc.commandNamePath = []string{c.Name, cc.Name}
|
||||||
newCmds = append(newCmds, cc)
|
newCmds = append(newCmds, cc)
|
||||||
}
|
}
|
||||||
|
10
help.go
10
help.go
@ -15,7 +15,7 @@ var AppHelpTemplate = `NAME:
|
|||||||
{{.Name}} - {{.Usage}}
|
{{.Name}} - {{.Usage}}
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
{{.Name}} {{if .Flags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{.ArgsUsage}}
|
{{.HelpName}} {{if .Flags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
||||||
{{if .Version}}
|
{{if .Version}}
|
||||||
VERSION:
|
VERSION:
|
||||||
{{.Version}}
|
{{.Version}}
|
||||||
@ -38,10 +38,10 @@ COPYRIGHT:
|
|||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
var CommandHelpTemplate = `NAME:
|
var CommandHelpTemplate = `NAME:
|
||||||
{{.ParentName}} {{.Name}} - {{.Usage}}
|
{{.HelpName}} - {{.Usage}}
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
{{.ParentName}} {{.Name}}{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}{{if .Description}}
|
{{.HelpName}}{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Description}}
|
||||||
|
|
||||||
DESCRIPTION:
|
DESCRIPTION:
|
||||||
{{.Description}}{{end}}{{if .Flags}}
|
{{.Description}}{{end}}{{if .Flags}}
|
||||||
@ -55,10 +55,10 @@ OPTIONS:
|
|||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
var SubcommandHelpTemplate = `NAME:
|
var SubcommandHelpTemplate = `NAME:
|
||||||
{{.Name}} - {{.Usage}}
|
{{.HelpName}} - {{.Usage}}
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
{{.Name}} command{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}
|
{{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
||||||
|
|
||||||
COMMANDS:
|
COMMANDS:
|
||||||
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
||||||
|
Loading…
Reference in New Issue
Block a user