add ArgsUsage to App and Command

This commit is contained in:
Tristan Zajonc 2015-08-03 16:51:11 -07:00
parent 142e6cd241
commit ef65245544
3 changed files with 19 additions and 9 deletions

3
app.go
View File

@ -15,6 +15,8 @@ type App struct {
Name string Name string
// Description of the program. // Description of the program.
Usage string Usage string
// Description of the program argument format.
ArgsUsage string
// Version of the program // Version of the program
Version string Version string
// List of commands to execute // List of commands to execute
@ -68,6 +70,7 @@ func NewApp() *App {
return &App{ return &App{
Name: os.Args[0], Name: 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,

View File

@ -18,6 +18,8 @@ type Command struct {
Usage string Usage string
// A longer explanation of how the command works // A longer explanation of how the command works
Description string Description string
// A short description of the arguments of this command
ArgsUsage string
// The function to call when checking for bash command completions // The function to call when checking for bash command completions
BashComplete func(context *Context) BashComplete func(context *Context)
// An action to execute before any sub-subcommands are run, but after the context is ready // An action to execute before any sub-subcommands are run, but after the context is ready
@ -158,6 +160,9 @@ func (c Command) startApp(ctx *Context) error {
} 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

20
help.go
View File

@ -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}} [arguments...] {{.Name}} {{if .Flags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{.ArgsUsage}}
{{if .Version}} {{if .Version}}
VERSION: VERSION:
{{.Version}} {{.Version}}
@ -41,7 +41,7 @@ var CommandHelpTemplate = `NAME:
{{.FullName}} - {{.Usage}} {{.FullName}} - {{.Usage}}
USAGE: USAGE:
command {{.FullName}}{{if .Flags}} [command options]{{end}} [arguments...]{{if .Description}} command {{.FullName}}{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}{{if .Description}}
DESCRIPTION: DESCRIPTION:
{{.Description}}{{end}}{{if .Flags}} {{.Description}}{{end}}{{if .Flags}}
@ -58,7 +58,7 @@ var SubcommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}} {{.Name}} - {{.Usage}}
USAGE: USAGE:
{{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...] {{.Name}} command{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}
COMMANDS: COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
@ -69,9 +69,10 @@ OPTIONS:
` `
var helpCommand = Command{ var helpCommand = Command{
Name: "help", Name: "help",
Aliases: []string{"h"}, Aliases: []string{"h"},
Usage: "Shows a list of commands or help for one command", Usage: "Shows a list of commands or help for one command",
ArgsUsage: "[command]",
Action: func(c *Context) { Action: func(c *Context) {
args := c.Args() args := c.Args()
if args.Present() { if args.Present() {
@ -83,9 +84,10 @@ var helpCommand = Command{
} }
var helpSubcommand = Command{ var helpSubcommand = Command{
Name: "help", Name: "help",
Aliases: []string{"h"}, Aliases: []string{"h"},
Usage: "Shows a list of commands or help for one command", Usage: "Shows a list of commands or help for one command",
ArgsUsage: "[command]",
Action: func(c *Context) { Action: func(c *Context) {
args := c.Args() args := c.Args()
if args.Present() { if args.Present() {