Skip to content

Commit f72fa77

Browse files
fix: disable bash completion if double dash is included in arguments
- #1932
1 parent 8e2384c commit f72fa77

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

help.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ func ShowCommandHelpAndExit(c *Context, command string, code int) {
248248

249249
// ShowCommandHelp prints help for the given command
250250
func ShowCommandHelp(ctx *Context, command string) error {
251-
252251
commands := ctx.App.Commands
253252
if ctx.Command.Subcommands != nil {
254253
commands = ctx.Command.Subcommands
@@ -337,15 +336,13 @@ func ShowCommandCompletions(ctx *Context, command string) {
337336
DefaultCompleteWithFlags(c)(ctx)
338337
}
339338
}
340-
341339
}
342340

343341
// printHelpCustom is the default implementation of HelpPrinterCustom.
344342
//
345343
// The customFuncs map will be combined with a default template.FuncMap to
346344
// allow using arbitrary functions in template rendering.
347345
func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
348-
349346
const maxLineLength = 10000
350347

351348
funcMap := template.FuncMap{
@@ -450,6 +447,15 @@ func checkShellCompleteFlag(a *App, arguments []string) (bool, []string) {
450447
return false, arguments
451448
}
452449

450+
for _, arg := range arguments {
451+
// If arguments include "--", shell completion is disabled
452+
// because after "--" only positional arguments are accepted.
453+
// https://unix.stackexchange.com/a/11382
454+
if arg == "--" {
455+
return false, arguments
456+
}
457+
}
458+
453459
return true, arguments[:pos]
454460
}
455461

@@ -499,7 +505,6 @@ func wrap(input string, offset int, wrapAt int) string {
499505
ss = append(ss, wrapped)
500506
} else {
501507
ss = append(ss, padding+wrapped)
502-
503508
}
504509

505510
}

help_test.go

+65-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"reflect"
910
"runtime"
1011
"strings"
1112
"testing"
@@ -1349,7 +1350,6 @@ func TestWrap(t *testing.T) {
13491350
}
13501351

13511352
func TestWrappedHelp(t *testing.T) {
1352-
13531353
// Reset HelpPrinter after this test.
13541354
defer func(old helpPrinter) {
13551355
HelpPrinter = old
@@ -1359,7 +1359,8 @@ func TestWrappedHelp(t *testing.T) {
13591359
app := &App{
13601360
Writer: output,
13611361
Flags: []Flag{
1362-
&BoolFlag{Name: "foo",
1362+
&BoolFlag{
1363+
Name: "foo",
13631364
Aliases: []string{"h"},
13641365
Usage: "here's a really long help text line, let's see where it wraps. blah blah blah and so on.",
13651366
},
@@ -1443,7 +1444,6 @@ COPYRIGHT:
14431444
}
14441445

14451446
func TestWrappedCommandHelp(t *testing.T) {
1446-
14471447
// Reset HelpPrinter after this test.
14481448
defer func(old helpPrinter) {
14491449
HelpPrinter = old
@@ -1504,7 +1504,6 @@ OPTIONS:
15041504
}
15051505

15061506
func TestWrappedSubcommandHelp(t *testing.T) {
1507-
15081507
// Reset HelpPrinter after this test.
15091508
defer func(old helpPrinter) {
15101509
HelpPrinter = old
@@ -1573,7 +1572,6 @@ OPTIONS:
15731572
}
15741573

15751574
func TestWrappedHelpSubcommand(t *testing.T) {
1576-
15771575
// Reset HelpPrinter after this test.
15781576
defer func(old helpPrinter) {
15791577
HelpPrinter = old
@@ -1714,3 +1712,65 @@ GLOBAL OPTIONS:
17141712
output.String(), expected)
17151713
}
17161714
}
1715+
1716+
func Test_checkShellCompleteFlag(t *testing.T) {
1717+
t.Parallel()
1718+
tests := []struct {
1719+
name string
1720+
app *App
1721+
arguments []string
1722+
wantShellCompletion bool
1723+
wantArgs []string
1724+
}{
1725+
{
1726+
name: "disable bash completion",
1727+
arguments: []string{"--generate-bash-completion"},
1728+
app: &App{},
1729+
wantShellCompletion: false,
1730+
wantArgs: []string{"--generate-bash-completion"},
1731+
},
1732+
{
1733+
name: "--generate-bash-completion isn't used",
1734+
arguments: []string{"foo"},
1735+
app: &App{
1736+
EnableBashCompletion: true,
1737+
},
1738+
wantShellCompletion: false,
1739+
wantArgs: []string{"foo"},
1740+
},
1741+
{
1742+
name: "arguments include double dash",
1743+
arguments: []string{"--", "foo", "--generate-bash-completion"},
1744+
app: &App{
1745+
EnableBashCompletion: true,
1746+
},
1747+
wantShellCompletion: false,
1748+
wantArgs: []string{"--", "foo", "--generate-bash-completion"},
1749+
},
1750+
{
1751+
name: "--generate-bash-completion",
1752+
arguments: []string{"foo", "--generate-bash-completion"},
1753+
app: &App{
1754+
EnableBashCompletion: true,
1755+
},
1756+
wantShellCompletion: true,
1757+
wantArgs: []string{"foo"},
1758+
},
1759+
}
1760+
1761+
for _, tt := range tests {
1762+
tt := tt
1763+
t.Run(tt.name, func(t *testing.T) {
1764+
t.Parallel()
1765+
shellCompletion, args := checkShellCompleteFlag(tt.app, tt.arguments)
1766+
if tt.wantShellCompletion != shellCompletion {
1767+
t.Errorf("Unexpected shell completion, got:\n%v\nexpected: %v",
1768+
shellCompletion, tt.wantShellCompletion)
1769+
}
1770+
if !reflect.DeepEqual(tt.wantArgs, args) {
1771+
t.Errorf("Unexpected arguments, got:\n%v\nexpected: %v",
1772+
args, tt.wantArgs)
1773+
}
1774+
})
1775+
}
1776+
}

0 commit comments

Comments
 (0)