Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1069) Dont show [arguments] in usage when there are no arguments defined #2001

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Argument interface {
Usage() string
}

// AnyArguments to differentiate between no arguments(nil) vs aleast one
var AnyArguments = []Argument{nil}
dearchap marked this conversation as resolved.
Show resolved Hide resolved

type ArgumentBase[T any, C any, VC ValueCreator[T, C]] struct {
Name string `json:"name"` // the name of this argument
Value T `json:"value"` // the default value of this argument
Expand Down
5 changes: 3 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func ExampleCommand_Run_appHelp() {
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
},
Arguments: cli.AnyArguments,
Commands: []*cli.Command{
{
Name: "describeit",
Expand Down Expand Up @@ -191,7 +192,7 @@ func ExampleCommand_Run_commandHelp() {
}

func ExampleCommand_Run_noAction() {
cmd := &cli.Command{Name: "greet"}
cmd := &cli.Command{Name: "greet"} //, Arguments: cli.AnyArguments}
dearchap marked this conversation as resolved.
Show resolved Hide resolved

// Simulate the command line arguments
os.Args = []string{"greet"}
Expand All @@ -202,7 +203,7 @@ func ExampleCommand_Run_noAction() {
// greet - A new cli application
//
// USAGE:
// greet [global options] [arguments...]
// greet [global options]
//
// GLOBAL OPTIONS:
// --help, -h show help
Expand Down
7 changes: 5 additions & 2 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var (
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
)
var AnyArguments = []Argument{nil}
AnyArguments to differentiate between no arguments(nil) vs aleast one

var CommandHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}

Expand Down Expand Up @@ -84,7 +87,7 @@ var RootCommandHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}} {{if .VisibleFlags}}[global options]{{end}}{{if .VisibleCommands}} [command [command options]]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}} {{if .VisibleFlags}}[global options]{{end}}{{if .VisibleCommands}} [command [command options]]{{end}}{{if .ArgsUsage}} {{.ArgsUsage}}{{else}}{{if .Arguments}} [arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}

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

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}} {{if .VisibleCommands}}[command [command options]] {{end}}{{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Category}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}}{{if .VisibleCommands}} [command [command options]] {{end}}{{if .ArgsUsage}} {{.ArgsUsage}}{{else}}{{if .Arguments}} [arguments...]{{end}}{{end}}{{end}}{{if .Category}}

CATEGORY:
{{.Category}}{{end}}{{if .Description}}
Expand Down
7 changes: 4 additions & 3 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func Test_Help_RequiredFlagsNoDefault(t *testing.T) {
Flags: []Flag{
&IntFlag{Name: "foo", Aliases: []string{"f"}, Required: true},
},
Writer: output,
Arguments: AnyArguments,
Writer: output,
}

_ = cmd.Run(buildTestContext(t), []string{"test", "-h"})
Expand Down Expand Up @@ -716,7 +717,7 @@ func TestShowSubcommandHelp_GlobalOptions(t *testing.T) {
foo frobbly

USAGE:
foo frobbly [command [command options]]
foo frobbly [command [command options]]

OPTIONS:
--bar value
Expand Down Expand Up @@ -1719,7 +1720,7 @@ func TestCategorizedHelp(t *testing.T) {
application

USAGE:
cli.test [global options] [arguments...]
cli.test [global options]

GLOBAL OPTIONS:
--help, -h show help
Expand Down
6 changes: 3 additions & 3 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cli
var (
helpNameTemplate = `{{$v := offset .FullName 6}}{{wrap .FullName 3}}{{if .Usage}} - {{wrap .Usage $v}}{{end}}`
argsTemplate = `{{if .Arguments}}{{range .Arguments}}{{.Usage}}{{end}}{{end}}`
usageTemplate = `{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}}{{if .VisibleFlags}} [command [command options]]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{template "argsTemplate" .}}{{end}}{{end}}`
usageTemplate = `{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}}{{if .VisibleFlags}} [command [command options]]{{end}}{{if .ArgsUsage}} {{.ArgsUsage}}{{else}}{{if .Arguments}} {{template "argsTemplate" .}}{{end}}{{end}}{{end}}`
descriptionTemplate = `{{wrap .Description 3}}`
authorsTemplate = `{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{range $index, $author := .Authors}}{{if $index}}
Expand Down Expand Up @@ -45,7 +45,7 @@ var RootCommandHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}} {{if .VisibleFlags}}[global options]{{end}}{{if .VisibleCommands}} [command [command options]]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}} {{if .VisibleFlags}}[global options]{{end}}{{if .VisibleCommands}} [command [command options]]{{end}}{{if .ArgsUsage}} {{.ArgsUsage}}{{else}}{{if .Arguments}} [arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}

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

USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}} {{if .VisibleCommands}}[command [command options]] {{end}}{{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Category}}
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}}{{if .VisibleCommands}} [command [command options]] {{end}}{{if .ArgsUsage}} {{.ArgsUsage}}{{else}}{{if .Arguments}} [arguments...]{{end}}{{end}}{{end}}{{if .Category}}

CATEGORY:
{{.Category}}{{end}}{{if .Description}}
Expand Down
Loading