Skip to content
Permalink
Browse files

Check for parsing errors within parse.go:parseIter

Add description to that function's docstring, and delete extraneous space
  • Loading branch information
unRob committed Nov 27, 2019
1 parent 90a62d7 commit f3295e3cdb6bfe573aca0b01af8ddd9335226264
Showing with 12 additions and 8 deletions.
  1. +2 −2 app.go
  2. +2 −3 command.go
  3. +8 −3 parse.go
4 app.go
@@ -223,7 +223,7 @@ func (a *App) Run(arguments []string) (err error) {
return err
}

err = parseIter(set, a, arguments[1:])
err = parseIter(set, a, arguments[1:], shellComplete)
nerr := normalizeFlags(a.Flags, set)
context := NewContext(a, set, nil)
if nerr != nil {
@@ -349,7 +349,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
return err
}

err = parseIter(set, a, ctx.Args().Tail())
err = parseIter(set, a, ctx.Args().Tail(), ctx.shellComplete)
nerr := normalizeFlags(a.Flags, set)
context := NewContext(a, set, ctx)

@@ -184,9 +184,8 @@ func (c *Command) parseFlags(args Args, shellComplete bool) (*flag.FlagSet, erro
return set, set.Parse(append([]string{"--"}, args.Tail()...))
}

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

@@ -11,19 +11,24 @@ type iterativeParser interface {
}

// To enable short-option handling (e.g., "-it" vs "-i -t") we have to
// iteratively catch parsing errors. This way we achieve LR parsing without
// iteratively catch parsing errors. This way we achieve LR parsing without
// transforming any arguments. Otherwise, there is no way we can discriminate
// combined short options from common arguments that should be left untouched.
func parseIter(set *flag.FlagSet, ip iterativeParser, args []string) error {
// Pass `ignoreErrors` to continue parsing options on failure, for example
// during shell completion when the user-supplied options may be incomplete.
func parseIter(set *flag.FlagSet, ip iterativeParser, args []string, ignoreErrors bool) error {
for {
err := set.Parse(args)
if !ip.useShortOptionHandling() || err == nil {
if ignoreErrors {
return nil
}
return err
}

errStr := err.Error()
trimmed := strings.TrimPrefix(errStr, "flag provided but not defined: -")
if errStr == trimmed {
if !ignoreErrors && errStr == trimmed {
return err
}

0 comments on commit f3295e3

Please sign in to comment.
You can’t perform that action at this time.