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_1916) Add additional check for -- #1919

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 4 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ type Command struct {
didSetupDefaults bool
// whether in shell completion mode
shellCompletion bool
// run args
runArgs []string
}

// FullName returns the full name of the command.
Expand Down Expand Up @@ -184,6 +186,8 @@ func (cmd *Command) setupDefaults(osArgs []string) {

cmd.didSetupDefaults = true

cmd.runArgs = osArgs

isRoot := cmd.parent == nil
tracef("isRoot? %[1]v (cmd=%[2]q)", isRoot, cmd.Name)

Expand Down
8 changes: 8 additions & 0 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func TestCompletionSubcommand(t *testing.T) {
out.String(), "-g",
"Expected output to contain flag %[1]q", "-g",
)

out.Reset()

r.NoError(cmd.Run(buildTestContext(t), []string{"foo", "bar", "xyz", "--", "--generate-shell-completion"}))
r.Containsf(
out.String(), "-g",
"Expected output to contain flag %[1]q", "-g",
)
}

func TestCompletionInvalidShell(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ func DefaultCompleteWithFlags(ctx context.Context, cmd *Command) {
lastArg = args[argsLen-1]
}

if len(cmd.runArgs) > 1 && cmd.runArgs[len(cmd.runArgs)-1] == "-" {
dearchap marked this conversation as resolved.
Show resolved Hide resolved
tracef("using last arg from cmd.runArgs [%v]", cmd.runArgs)
lastArg = "-"
}

if strings.HasPrefix(lastArg, "-") {
tracef("printing flag suggestion for flag[%v] on command %[1]q", lastArg, cmd.Name)
printFlagSuggestions(lastArg, cmd.Flags, cmd.Root().Writer)
Expand Down
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string, shellComple
err := set.Parse(args)
if !ip.useShortOptionHandling() || err == nil {
if shellComplete {
tracef("returning nil due to shellComplete=true")
tracef("returning nil due to shellComplete=true err=%v", err)

return nil
}
Expand Down
Loading