Skip to content

Commit 07f2236

Browse files
authored
Merge pull request #2245 from dearchap/issue_2244
Fix:(issue_2244) Dont check req flags for help and completion commands
2 parents c32fd20 + f92e992 commit 07f2236

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

command_run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context
141141
var rargs Args = &stringSliceArgs{v: osArgs}
142142
var args Args = &stringSliceArgs{rargs.Tail()}
143143

144-
if cmd.isCompletionCommand {
145-
tracef("completion command detected, skipping pre-parse (cmd=%[1]q)", cmd.Name)
144+
if cmd.isCompletionCommand || cmd.Name == helpName {
145+
tracef("special command detected, skipping pre-parse (cmd=%[1]q)", cmd.Name)
146146
cmd.parsedArgs = args
147147
return ctx, cmd.Action(ctx, cmd)
148148
}

completion_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestCompletionEnable(t *testing.T) {
2121
cmd := &Command{
2222
EnableShellCompletion: true,
2323
Flags: []Flag{
24-
&BoolFlag{
24+
&StringFlag{
2525
Name: "goo",
2626
Required: true,
2727
},

help_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestShowSubcommandHelpAndExit(t *testing.T) {
108108
require.Equal(t, 42, lastExitCode)
109109
}
110110

111-
func Test_Help_RequiredFlagsNoDefault(t *testing.T) {
111+
func Test_HelpFlag_RequiredFlagsNoDefault(t *testing.T) {
112112
output := new(bytes.Buffer)
113113

114114
cmd := &Command{
@@ -136,6 +136,34 @@ GLOBAL OPTIONS:
136136
"expected output to include usage text")
137137
}
138138

139+
func Test_HelpCommand_RequiredFlagsNoDefault(t *testing.T) {
140+
output := new(bytes.Buffer)
141+
142+
cmd := &Command{
143+
Flags: []Flag{
144+
&Int64Flag{Name: "foo", Aliases: []string{"f"}, Required: true},
145+
},
146+
Arguments: AnyArguments,
147+
Writer: output,
148+
}
149+
150+
_ = cmd.Run(buildTestContext(t), []string{"test", "help"})
151+
152+
expected := `NAME:
153+
test - A new cli application
154+
155+
USAGE:
156+
test [global options] [arguments...]
157+
158+
GLOBAL OPTIONS:
159+
--foo int, -f int
160+
--help, -h show help
161+
`
162+
163+
assert.Contains(t, output.String(), expected,
164+
"expected output to include usage text")
165+
}
166+
139167
func Test_Help_Custom_Flags(t *testing.T) {
140168
oldFlag := HelpFlag
141169
defer func() {

0 commit comments

Comments
 (0)