Check for parsing errors within parse.go:parseIter
Add description to that function's docstring, and delete extraneous space
This commit is contained in:
parent
90a62d7b0c
commit
f3295e3cdb
4
app.go
4
app.go
@ -223,7 +223,7 @@ func (a *App) Run(arguments []string) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = parseIter(set, a, arguments[1:])
|
err = parseIter(set, a, arguments[1:], shellComplete)
|
||||||
nerr := normalizeFlags(a.Flags, set)
|
nerr := normalizeFlags(a.Flags, set)
|
||||||
context := NewContext(a, set, nil)
|
context := NewContext(a, set, nil)
|
||||||
if nerr != nil {
|
if nerr != nil {
|
||||||
@ -349,7 +349,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = parseIter(set, a, ctx.Args().Tail())
|
err = parseIter(set, a, ctx.Args().Tail(), ctx.shellComplete)
|
||||||
nerr := normalizeFlags(a.Flags, set)
|
nerr := normalizeFlags(a.Flags, set)
|
||||||
context := NewContext(a, set, ctx)
|
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()...))
|
return set, set.Parse(append([]string{"--"}, args.Tail()...))
|
||||||
}
|
}
|
||||||
|
|
||||||
err = parseIter(set, c, args.Tail())
|
err = parseIter(set, c, args.Tail(), shellComplete)
|
||||||
// Continue parsing flags on failure during shell completion
|
if err != nil {
|
||||||
if err != nil && !shellComplete {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
parse.go
9
parse.go
@ -14,16 +14,21 @@ type iterativeParser interface {
|
|||||||
// 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
|
// transforming any arguments. Otherwise, there is no way we can discriminate
|
||||||
// combined short options from common arguments that should be left untouched.
|
// 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 {
|
for {
|
||||||
err := set.Parse(args)
|
err := set.Parse(args)
|
||||||
if !ip.useShortOptionHandling() || err == nil {
|
if !ip.useShortOptionHandling() || err == nil {
|
||||||
|
if ignoreErrors {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
errStr := err.Error()
|
errStr := err.Error()
|
||||||
trimmed := strings.TrimPrefix(errStr, "flag provided but not defined: -")
|
trimmed := strings.TrimPrefix(errStr, "flag provided but not defined: -")
|
||||||
if errStr == trimmed {
|
if !ignoreErrors && errStr == trimmed {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user