main
Tristan Zajonc 9 years ago
parent ecb0b5ac0a
commit aced6e8739

@ -13,6 +13,8 @@ import (
type App struct {
// The name of the program. Defaults to os.Args[0]
Name string
// Name of command for help, defaults to Name
HelpName string
// Description of the program.
Usage string
// Description of the program argument format.
@ -69,8 +71,8 @@ func compileTime() time.Time {
func NewApp() *App {
return &App{
Name: os.Args[0],
HelpName: os.Args[0],
Usage: "A new cli application",
ArgsUsage: "[arguments...]",
Version: "0.0.0",
BashComplete: DefaultAppComplete,
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})
}
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
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
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
if a.EnableBashCompletion {
a.appendFlag(BashCompletionFlag)

@ -90,10 +90,10 @@ func ExampleAppHelp() {
app.Run(os.Args)
// Output:
// NAME:
// describeit - use it to see a description
// greet describeit - use it to see a description
//
// USAGE:
// command describeit [arguments...]
// greet describeit [arguments...]
//
// DESCRIPTION:
// This is how we describe describeit the function
@ -737,7 +737,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
app := NewApp()
buf := new(bytes.Buffer)
app.Writer = buf
app.Name = "command"
subCmd := Command{
Name: "bar",
Usage: "does bar things",
@ -755,7 +755,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
}
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)
}
if !strings.Contains(output, "command foo bar [arguments...]") {

@ -39,9 +39,8 @@ type Command struct {
// Boolean to hide built-in help command
HideHelp bool
// Name of parent command for help, defaults to app.Name and parent
// command.Name.
ParentName string
// Name of command for help, defaults to full command name
HelpName string
commandNamePath []string
}
@ -158,14 +157,12 @@ func (c Command) startApp(ctx *Context) error {
// set the name and usage
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 != "" {
app.Usage = c.Description
} else {
app.Usage = c.Usage
}
if c.ArgsUsage == "" {
c.ArgsUsage = "[arguments...]"
}
// set CommandNotFound
app.CommandNotFound = ctx.App.CommandNotFound
@ -199,7 +196,6 @@ func (c Command) startApp(ctx *Context) error {
var newCmds []Command
for _, cc := range app.Commands {
cc.ParentName = app.Name
cc.commandNamePath = []string{c.Name, cc.Name}
newCmds = append(newCmds, cc)
}

@ -15,7 +15,7 @@ var AppHelpTemplate = `NAME:
{{.Name}} - {{.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}}
VERSION:
{{.Version}}
@ -38,10 +38,10 @@ COPYRIGHT:
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
var CommandHelpTemplate = `NAME:
{{.ParentName}} {{.Name}} - {{.Usage}}
{{.HelpName}} - {{.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}}{{end}}{{if .Flags}}
@ -55,10 +55,10 @@ OPTIONS:
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
var SubcommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
{{.HelpName}} - {{.Usage}}
USAGE:
{{.Name}} command{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}
{{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}

Loading…
Cancel
Save