Skip to content

Commit c667cc0

Browse files
authored
Merge pull request #1829 from dearchap/issue_1797
Feat:(issue_1797) Add Args for app/cmd/subcmd to avoid argument... be…
2 parents ca5c42f + dbe3417 commit c667cc0

File tree

7 files changed

+25
-7
lines changed

7 files changed

+25
-7
lines changed

app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type App struct {
3939
Usage string
4040
// Text to override the USAGE section of help
4141
UsageText string
42+
// Whether this command supports arguments
43+
Args bool
4244
// Description of the program argument format.
4345
ArgsUsage string
4446
// Version of the program

app_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func ExampleApp_Run_appHelp() {
101101
app := &App{
102102
Name: "greet",
103103
Version: "0.1.0",
104+
Args: true,
104105
Description: "This is how we describe greet the app",
105106
Authors: []*Author{
106107
{Name: "Harrison", Email: "[email protected]"},
@@ -156,6 +157,7 @@ func ExampleApp_Run_commandHelp() {
156157

157158
app := &App{
158159
Name: "greet",
160+
Args: true,
159161
Flags: []Flag{
160162
&StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
161163
},
@@ -190,6 +192,7 @@ func ExampleApp_Run_commandHelp() {
190192
func ExampleApp_Run_noAction() {
191193
app := App{}
192194
app.Name = "greet"
195+
app.Args = true
193196
_ = app.Run([]string{"greet"})
194197
// Output:
195198
// NAME:
@@ -208,6 +211,7 @@ func ExampleApp_Run_noAction() {
208211
func ExampleApp_Run_subcommandNoAction() {
209212
app := &App{
210213
Name: "greet",
214+
Args: true,
211215
Commands: []*Command{
212216
{
213217
Name: "describeit",
@@ -2017,6 +2021,7 @@ func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
20172021
Name: "foo",
20182022
Usage: "foo commands",
20192023
Description: "This is a description",
2024+
Args: true,
20202025
Subcommands: []*Command{subCmd},
20212026
}
20222027
app.Commands = []*Command{cmd}

command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type Command struct {
2020
UsageText string
2121
// A longer explanation of how the command works
2222
Description string
23+
// Whether this command supports arguments
24+
Args bool
2325
// A short description of the arguments of this command
2426
ArgsUsage string
2527
// The category the command is part of

godoc-current.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var AppHelpTemplate = `NAME:
3535
{{template "helpNameTemplate" .}}
3636

3737
USAGE:
38-
{{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}}
38+
{{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}}
3939

4040
VERSION:
4141
{{.Version}}{{end}}{{end}}{{if .Description}}
@@ -136,7 +136,7 @@ var SubcommandHelpTemplate = `NAME:
136136
{{template "helpNameTemplate" .}}
137137

138138
USAGE:
139-
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}}
139+
{{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}}
140140

141141
DESCRIPTION:
142142
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
@@ -253,6 +253,8 @@ type App struct {
253253
Usage string
254254
// Text to override the USAGE section of help
255255
UsageText string
256+
// Whether this command supports arguments
257+
Args bool
256258
// Description of the program argument format.
257259
ArgsUsage string
258260
// Version of the program
@@ -523,6 +525,8 @@ type Command struct {
523525
UsageText string
524526
// A longer explanation of how the command works
525527
Description string
528+
// Whether this command supports arguments
529+
Args bool
526530
// A short description of the arguments of this command
527531
ArgsUsage string
528532
// The category the command is part of

help_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,7 @@ func TestCategorizedHelp(t *testing.T) {
16581658
output := new(bytes.Buffer)
16591659
app := &App{
16601660
Name: "cli.test",
1661+
Args: true,
16611662
Writer: output,
16621663
Action: func(ctx *Context) error { return nil },
16631664
Flags: []Flag{

template.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cli
22

33
var helpNameTemplate = `{{$v := offset .HelpName 6}}{{wrap .HelpName 3}}{{if .Usage}} - {{wrap .Usage $v}}{{end}}`
4-
var usageTemplate = `{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}`
4+
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}}`
55
var descriptionTemplate = `{{wrap .Description 3}}`
66
var authorsTemplate = `{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
77
{{range $index, $author := .Authors}}{{if $index}}
@@ -35,7 +35,7 @@ var AppHelpTemplate = `NAME:
3535
{{template "helpNameTemplate" .}}
3636
3737
USAGE:
38-
{{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}}
38+
{{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}}
3939
4040
VERSION:
4141
{{.Version}}{{end}}{{end}}{{if .Description}}
@@ -83,7 +83,7 @@ var SubcommandHelpTemplate = `NAME:
8383
{{template "helpNameTemplate" .}}
8484
8585
USAGE:
86-
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}}
86+
{{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}}
8787
8888
DESCRIPTION:
8989
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}

testdata/godoc-v2.x.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var AppHelpTemplate = `NAME:
3535
{{template "helpNameTemplate" .}}
3636

3737
USAGE:
38-
{{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}}
38+
{{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}}
3939

4040
VERSION:
4141
{{.Version}}{{end}}{{end}}{{if .Description}}
@@ -136,7 +136,7 @@ var SubcommandHelpTemplate = `NAME:
136136
{{template "helpNameTemplate" .}}
137137

138138
USAGE:
139-
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}}
139+
{{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}}
140140

141141
DESCRIPTION:
142142
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
@@ -253,6 +253,8 @@ type App struct {
253253
Usage string
254254
// Text to override the USAGE section of help
255255
UsageText string
256+
// Whether this command supports arguments
257+
Args bool
256258
// Description of the program argument format.
257259
ArgsUsage string
258260
// Version of the program
@@ -523,6 +525,8 @@ type Command struct {
523525
UsageText string
524526
// A longer explanation of how the command works
525527
Description string
528+
// Whether this command supports arguments
529+
Args bool
526530
// A short description of the arguments of this command
527531
ArgsUsage string
528532
// The category the command is part of

0 commit comments

Comments
 (0)