-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Prevent panic on flagSet access from custom BashComplete #946
Conversation
Codecov Report
@@ Coverage Diff @@
## master #946 +/- ##
==========================================
+ Coverage 72.89% 73.37% +0.47%
==========================================
Files 32 32
Lines 2439 2441 +2
==========================================
+ Hits 1778 1791 +13
+ Misses 550 540 -10
+ Partials 111 110 -1
Continue to review full report at Codecov.
|
@@ -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) { |
There was a problem hiding this comment.
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!
command.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 parametershellComplete bool
to deal with nil errors within. - Decided to keep the
shellComplete
name, played withignoreErrors
instead, but it felt like a misnomer sinceset.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.
command.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add description to that function's docstring, and delete extraneous space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love it! thanks so much!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks 🙏
Fixes #944