Revert UsageOnMissing.

This was breaking UsageOnMissing() when combined with validation of
positional arguments, in that it was allowing positional arguments to be
omitted. It also broke UsageOnMissing() somehow so that it did not work.
This commit is contained in:
Alec Thomas
2020-10-27 17:32:34 +11:00
parent 73064c7b8f
commit 097bba54ec
4 changed files with 15 additions and 58 deletions
+7 -7
View File
@@ -683,14 +683,8 @@ func (c *Context) Run(binds ...interface{}) (err error) {
defer catch(&err)
node := c.Selected()
if node == nil {
if c.Kong.usageOnMissing {
return c.PrintUsage(false)
}
return fmt.Errorf("no command selected")
}
if c.Kong.usageOnMissing && isMissingChildError(c.Error) {
return c.PrintUsage(false)
}
return c.RunNode(node, binds...)
}
@@ -759,7 +753,13 @@ func checkMissingChildren(node *Node) error {
return nil
}
return newMissingChildError(missing)
if len(missing) > 5 {
missing = append(missing[:5], "...")
}
if len(missing) == 1 {
return fmt.Errorf("expected %s", missing[0])
}
return fmt.Errorf("expected one of %s", strings.Join(missing, ", "))
}
// If we're missing any positionals and they're required, return an error.
-31
View File
@@ -1,10 +1,5 @@
package kong
import (
"fmt"
"strings"
)
// ParseError is the error type returned by Kong.Parse().
//
// It contains the parse Context that triggered the error.
@@ -15,29 +10,3 @@ type ParseError struct {
// Cause returns the original cause of the error.
func (p *ParseError) Cause() error { return p.error }
type missingChildError struct {
missing []string
}
func (m *missingChildError) Error() string {
if len(m.missing) > 5 {
m.missing = append(m.missing[:5], "...")
}
if len(m.missing) == 1 {
return fmt.Sprintf("expected %s", m.missing[0])
}
return fmt.Sprintf("expected one of %s", strings.Join(m.missing, ", "))
}
func newMissingChildError(missing []string) *missingChildError {
return &missingChildError{missing}
}
func isMissingChildError(err error) bool {
if err == nil {
return false
}
_, ok := err.(*missingChildError)
return ok
}
+8 -12
View File
@@ -47,14 +47,13 @@ type Kong struct {
resolvers []Resolver
registry *Registry
noDefaultHelp bool
usageOnError bool
usageOnMissing bool
help HelpPrinter
helpFormatter HelpValueFormatter
helpOptions HelpOptions
helpFlag *Flag
vars Vars
noDefaultHelp bool
usageOnError bool
help HelpPrinter
helpFormatter HelpValueFormatter
helpOptions HelpOptions
helpFlag *Flag
vars Vars
// Set temporarily by Options. These are applied after build().
postBuildOptions []Option
@@ -219,10 +218,7 @@ func (k *Kong) Parse(args []string) (ctx *Context, err error) {
return nil, &ParseError{error: err, Context: ctx}
}
if err = ctx.Validate(); err != nil {
if !k.usageOnError || !isMissingChildError(err) {
return nil, &ParseError{error: err, Context: ctx}
}
ctx.Error = err
return nil, &ParseError{error: err, Context: ctx}
}
if err = k.applyHook(ctx, "AfterApply"); err != nil {
return nil, &ParseError{error: err, Context: ctx}
-8
View File
@@ -216,14 +216,6 @@ func UsageOnError() Option {
})
}
// UsageOnMissing configures Kong to display usage and exit successfully if command is missing a child argument.
func UsageOnMissing() Option {
return OptionFunc(func(k *Kong) error {
k.usageOnMissing = true
return nil
})
}
// ClearResolvers clears all existing resolvers.
func ClearResolvers() Option {
return OptionFunc(func(k *Kong) error {