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

Prevent panic on flagSet access from custom BashComplete #946

Merged
merged 5 commits into from
Nov 28, 2019
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
7 changes: 4 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Command) Run(ctx *Context) (err error) {
c.UseShortOptionHandling = true
}

set, err := c.parseFlags(ctx.Args())
set, err := c.parseFlags(ctx.Args(), ctx.shellComplete)

context := NewContext(ctx.App, set, ctx)
context.Command = c
Expand Down Expand Up @@ -174,7 +174,7 @@ func (c *Command) useShortOptionHandling() bool {
return c.UseShortOptionHandling
}

func (c *Command) parseFlags(args Args) (*flag.FlagSet, error) {
func (c *Command) parseFlags(args Args, shellComplete bool) (*flag.FlagSet, error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure if this name/concept is right as an argument, kept it for the sake of consistency but happy to adjust if someone has a better idea!

set, err := c.newFlagSet()
if err != nil {
return nil, err
Expand All @@ -185,7 +185,8 @@ func (c *Command) parseFlags(args Args) (*flag.FlagSet, error) {
}

err = parseIter(set, c, args.Tail())
if err != nil {
// Continue parsing flags on failure during shell completion
if err != nil && !shellComplete {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically allows for the context to continue to be parsed. Malformed flags will still be ignored and not part of the set, which is what v2 pre-release builds did.

Copy link
Member

@coilysiren coilysiren Nov 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐⭐⭐ (must change) @unRob can you add in here a short description of what parseIter does, and a detailed description of why it's ok to skip errors from it when doing shell completion?

FYI cc @rliebz

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH! Actually! I have a better idea: can you change this code to pass shellComplete into the parseIter function, ignore the errors inside the function itself, and add the descriptions I just requested inside the function's docstring?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

  • Changed the signature of parseIter to accept a fourth parameter shellComplete bool to deal with nil errors within.
  • Decided to keep the shellComplete name, played with ignoreErrors instead, but it felt like a misnomer since set.Parse won't actually error on it.
  • Modified a couple of consumers to pass shellComplete from their own context as well.
  • Added a couple of lines to the docstring, hope my wording conveys the use for passing that argument.

return nil, err
}

Expand Down
47 changes: 47 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"bytes"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -329,3 +330,49 @@ func TestCommandSkipFlagParsing(t *testing.T) {
expect(t, args, c.expectedArgs)
}
}

func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) {
cases := []struct {
testArgs args
expectedOut string
}{
{testArgs: args{"--undefined"}, expectedOut: "found 0 args"},
{testArgs: args{"--number"}, expectedOut: "found 0 args"},
{testArgs: args{"--number", "fourty-two"}, expectedOut: "found 0 args"},
{testArgs: args{"--number", "42"}, expectedOut: "found 0 args"},
{testArgs: args{"--number", "42", "newArg"}, expectedOut: "found 1 args"},
}

for _, c := range cases {
var outputBuffer bytes.Buffer
app := &App{
Writer: &outputBuffer,
EnableBashCompletion: true,
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Flags: []Flag{
&IntFlag{
Name: "number",
Usage: "A number to parse",
},
},
BashComplete: func(c *Context) {
fmt.Fprintf(c.App.Writer, "found %d args", c.NArg())
},
},
},
}

osArgs := args{"foo", "bar"}
osArgs = append(osArgs, c.testArgs...)
osArgs = append(osArgs, "--generate-bash-completion")

err := app.Run(osArgs)
stdout := outputBuffer.String()
expect(t, err, nil)
expect(t, stdout, c.expectedOut)
}

}