Skip to content

Commit

Permalink
Merge pull request #1829 from dearchap/issue_1797
Browse files Browse the repository at this point in the history
Feat:(issue_1797) Add Args for app/cmd/subcmd to avoid argument... be…
  • Loading branch information
dearchap committed Dec 22, 2023
2 parents ca5c42f + dbe3417 commit c667cc0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type App struct {
Usage string
// Text to override the USAGE section of help
UsageText string
// Whether this command supports arguments
Args bool
// Description of the program argument format.
ArgsUsage string
// Version of the program
Expand Down
5 changes: 5 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func ExampleApp_Run_appHelp() {
app := &App{
Name: "greet",
Version: "0.1.0",
Args: true,
Description: "This is how we describe greet the app",
Authors: []*Author{
{Name: "Harrison", Email: "harrison@lolwut.com"},
Expand Down Expand Up @@ -156,6 +157,7 @@ func ExampleApp_Run_commandHelp() {

app := &App{
Name: "greet",
Args: true,
Flags: []Flag{
&StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
},
Expand Down Expand Up @@ -190,6 +192,7 @@ func ExampleApp_Run_commandHelp() {
func ExampleApp_Run_noAction() {
app := App{}
app.Name = "greet"
app.Args = true
_ = app.Run([]string{"greet"})
// Output:
// NAME:
Expand All @@ -208,6 +211,7 @@ func ExampleApp_Run_noAction() {
func ExampleApp_Run_subcommandNoAction() {
app := &App{
Name: "greet",
Args: true,
Commands: []*Command{
{
Name: "describeit",
Expand Down Expand Up @@ -2017,6 +2021,7 @@ func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
Name: "foo",
Usage: "foo commands",
Description: "This is a description",
Args: true,
Subcommands: []*Command{subCmd},
}
app.Commands = []*Command{cmd}
Expand Down
2 changes: 2 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Command struct {
UsageText string
// A longer explanation of how the command works
Description string
// Whether this command supports arguments
Args bool
// A short description of the arguments of this command
ArgsUsage string
// The category the command is part of
Expand Down
8 changes: 6 additions & 2 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var AppHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}

VERSION:
{{.Version}}{{end}}{{end}}{{if .Description}}
Expand Down Expand Up @@ -136,7 +136,7 @@ var SubcommandHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Description}}

DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
Expand Down Expand Up @@ -253,6 +253,8 @@ type App struct {
Usage string
// Text to override the USAGE section of help
UsageText string
// Whether this command supports arguments
Args bool
// Description of the program argument format.
ArgsUsage string
// Version of the program
Expand Down Expand Up @@ -523,6 +525,8 @@ type Command struct {
UsageText string
// A longer explanation of how the command works
Description string
// Whether this command supports arguments
Args bool
// A short description of the arguments of this command
ArgsUsage string
// The category the command is part of
Expand Down
1 change: 1 addition & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,7 @@ func TestCategorizedHelp(t *testing.T) {
output := new(bytes.Buffer)
app := &App{
Name: "cli.test",
Args: true,
Writer: output,
Action: func(ctx *Context) error { return nil },
Flags: []Flag{
Expand Down
6 changes: 3 additions & 3 deletions template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cli

var helpNameTemplate = `{{$v := offset .HelpName 6}}{{wrap .HelpName 3}}{{if .Usage}} - {{wrap .Usage $v}}{{end}}`
var usageTemplate = `{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}`
var usageTemplate = `{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}[arguments...]{{end}}{{end}}`
var descriptionTemplate = `{{wrap .Description 3}}`
var authorsTemplate = `{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{range $index, $author := .Authors}}{{if $index}}
Expand Down Expand Up @@ -35,7 +35,7 @@ var AppHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}
USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
VERSION:
{{.Version}}{{end}}{{end}}{{if .Description}}
Expand Down Expand Up @@ -83,7 +83,7 @@ var SubcommandHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}
USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Description}}
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
Expand Down
8 changes: 6 additions & 2 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var AppHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}

VERSION:
{{.Version}}{{end}}{{end}}{{if .Description}}
Expand Down Expand Up @@ -136,7 +136,7 @@ var SubcommandHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{if .Args}}[arguments...]{{end}}{{end}}{{end}}{{if .Description}}

DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
Expand Down Expand Up @@ -253,6 +253,8 @@ type App struct {
Usage string
// Text to override the USAGE section of help
UsageText string
// Whether this command supports arguments
Args bool
// Description of the program argument format.
ArgsUsage string
// Version of the program
Expand Down Expand Up @@ -523,6 +525,8 @@ type Command struct {
UsageText string
// A longer explanation of how the command works
Description string
// Whether this command supports arguments
Args bool
// A short description of the arguments of this command
ArgsUsage string
// The category the command is part of
Expand Down

0 comments on commit c667cc0

Please sign in to comment.