Fix HideHelp
This commit is contained in:
parent
c8147a4845
commit
1a1b9cd563
8
app.go
8
app.go
@ -275,7 +275,9 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
|
||||
cCtx := NewContext(a, set, &Context{Context: ctx})
|
||||
if nerr != nil {
|
||||
_, _ = fmt.Fprintln(a.Writer, nerr)
|
||||
_ = ShowAppHelp(cCtx)
|
||||
if !a.HideHelp {
|
||||
_ = ShowAppHelp(cCtx)
|
||||
}
|
||||
return nerr
|
||||
}
|
||||
cCtx.shellComplete = shellComplete
|
||||
@ -296,7 +298,9 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
|
||||
fmt.Fprintf(a.Writer, suggestion)
|
||||
}
|
||||
}
|
||||
_ = ShowAppHelp(cCtx)
|
||||
if !a.HideHelp {
|
||||
_ = ShowAppHelp(cCtx)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
58
app_test.go
58
app_test.go
@ -1873,31 +1873,67 @@ func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApp_Run_Help(t *testing.T) {
|
||||
var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}}
|
||||
|
||||
for _, args := range helpArguments {
|
||||
t.Run(fmt.Sprintf("checking with arguments %v", args), func(t *testing.T) {
|
||||
var tests = []struct {
|
||||
helpArguments []string
|
||||
hideHelp bool
|
||||
wantContains string
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
helpArguments: []string{"boom", "--help"},
|
||||
hideHelp: false,
|
||||
wantContains: "boom - make an explosive entrance",
|
||||
},
|
||||
{
|
||||
helpArguments: []string{"boom", "-h"},
|
||||
hideHelp: false,
|
||||
wantContains: "boom - make an explosive entrance",
|
||||
},
|
||||
{
|
||||
helpArguments: []string{"boom", "help"},
|
||||
hideHelp: false,
|
||||
wantContains: "boom - make an explosive entrance",
|
||||
},
|
||||
{
|
||||
helpArguments: []string{"boom", "--help"},
|
||||
hideHelp: true,
|
||||
wantErr: fmt.Errorf("flag: help requested"),
|
||||
},
|
||||
{
|
||||
helpArguments: []string{"boom", "-h"},
|
||||
hideHelp: true,
|
||||
wantErr: fmt.Errorf("flag: help requested"),
|
||||
},
|
||||
{
|
||||
helpArguments: []string{"boom", "help"},
|
||||
hideHelp: true,
|
||||
wantContains: "boom I say!",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(fmt.Sprintf("checking with arguments %v", tt.helpArguments), func(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
app := &App{
|
||||
Name: "boom",
|
||||
Usage: "make an explosive entrance",
|
||||
Writer: buf,
|
||||
Name: "boom",
|
||||
Usage: "make an explosive entrance",
|
||||
Writer: buf,
|
||||
HideHelp: tt.hideHelp,
|
||||
Action: func(c *Context) error {
|
||||
buf.WriteString("boom I say!")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
err := app.Run(args)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
err := app.Run(tt.helpArguments)
|
||||
if err != nil && err.Error() != tt.wantErr.Error() {
|
||||
t.Errorf("want err: %s, did note %s\n", tt.wantErr, err)
|
||||
}
|
||||
|
||||
output := buf.String()
|
||||
|
||||
if !strings.Contains(output, "boom - make an explosive entrance") {
|
||||
if !strings.Contains(output, tt.wantContains) {
|
||||
t.Errorf("want help to contain %q, did not: \n%q", "boom - make an explosive entrance", output)
|
||||
}
|
||||
})
|
||||
|
@ -125,7 +125,9 @@ func (c *Command) Run(ctx *Context) (err error) {
|
||||
fmt.Fprintf(cCtx.App.Writer, suggestion)
|
||||
}
|
||||
}
|
||||
_ = ShowCommandHelp(cCtx, c.Name)
|
||||
if !c.HideHelp {
|
||||
_ = ShowCommandHelp(cCtx, c.Name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@ -135,7 +137,9 @@ func (c *Command) Run(ctx *Context) (err error) {
|
||||
|
||||
cerr := cCtx.checkRequiredFlags(c.Flags)
|
||||
if cerr != nil {
|
||||
_ = ShowCommandHelp(cCtx, c.Name)
|
||||
if !c.HideHelp {
|
||||
_ = ShowCommandHelp(cCtx, c.Name)
|
||||
}
|
||||
return cerr
|
||||
}
|
||||
|
||||
|
@ -124,7 +124,7 @@ func ExampleApp_Suggest() {
|
||||
app := &App{
|
||||
Name: "greet",
|
||||
Suggest: true,
|
||||
HideHelp: true,
|
||||
HideHelp: false,
|
||||
HideHelpCommand: true,
|
||||
CustomAppHelpTemplate: "(this space intentionally left blank)\n",
|
||||
Flags: []Flag{
|
||||
@ -149,7 +149,6 @@ func ExampleApp_Suggest_command() {
|
||||
app := &App{
|
||||
Name: "greet",
|
||||
Suggest: true,
|
||||
HideHelp: true,
|
||||
HideHelpCommand: true,
|
||||
CustomAppHelpTemplate: "(this space intentionally left blank)\n",
|
||||
Flags: []Flag{
|
||||
@ -162,6 +161,7 @@ func ExampleApp_Suggest_command() {
|
||||
Commands: []*Command{
|
||||
{
|
||||
Name: "neighbors",
|
||||
HideHelp: false,
|
||||
CustomHelpTemplate: "(this space intentionally left blank)\n",
|
||||
Flags: []Flag{
|
||||
&BoolFlag{Name: "smiling"},
|
||||
|
Loading…
Reference in New Issue
Block a user