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:
+7
-7
@@ -683,14 +683,8 @@ func (c *Context) Run(binds ...interface{}) (err error) {
|
|||||||
defer catch(&err)
|
defer catch(&err)
|
||||||
node := c.Selected()
|
node := c.Selected()
|
||||||
if node == nil {
|
if node == nil {
|
||||||
if c.Kong.usageOnMissing {
|
|
||||||
return c.PrintUsage(false)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("no command selected")
|
return fmt.Errorf("no command selected")
|
||||||
}
|
}
|
||||||
if c.Kong.usageOnMissing && isMissingChildError(c.Error) {
|
|
||||||
return c.PrintUsage(false)
|
|
||||||
}
|
|
||||||
return c.RunNode(node, binds...)
|
return c.RunNode(node, binds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -759,7 +753,13 @@ func checkMissingChildren(node *Node) error {
|
|||||||
return nil
|
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.
|
// If we're missing any positionals and they're required, return an error.
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
package kong
|
package kong
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ParseError is the error type returned by Kong.Parse().
|
// ParseError is the error type returned by Kong.Parse().
|
||||||
//
|
//
|
||||||
// It contains the parse Context that triggered the error.
|
// It contains the parse Context that triggered the error.
|
||||||
@@ -15,29 +10,3 @@ type ParseError struct {
|
|||||||
|
|
||||||
// Cause returns the original cause of the error.
|
// Cause returns the original cause of the error.
|
||||||
func (p *ParseError) Cause() error { return p.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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -47,14 +47,13 @@ type Kong struct {
|
|||||||
resolvers []Resolver
|
resolvers []Resolver
|
||||||
registry *Registry
|
registry *Registry
|
||||||
|
|
||||||
noDefaultHelp bool
|
noDefaultHelp bool
|
||||||
usageOnError bool
|
usageOnError bool
|
||||||
usageOnMissing bool
|
help HelpPrinter
|
||||||
help HelpPrinter
|
helpFormatter HelpValueFormatter
|
||||||
helpFormatter HelpValueFormatter
|
helpOptions HelpOptions
|
||||||
helpOptions HelpOptions
|
helpFlag *Flag
|
||||||
helpFlag *Flag
|
vars Vars
|
||||||
vars Vars
|
|
||||||
|
|
||||||
// Set temporarily by Options. These are applied after build().
|
// Set temporarily by Options. These are applied after build().
|
||||||
postBuildOptions []Option
|
postBuildOptions []Option
|
||||||
@@ -219,10 +218,7 @@ func (k *Kong) Parse(args []string) (ctx *Context, err error) {
|
|||||||
return nil, &ParseError{error: err, Context: ctx}
|
return nil, &ParseError{error: err, Context: ctx}
|
||||||
}
|
}
|
||||||
if err = ctx.Validate(); err != nil {
|
if err = ctx.Validate(); err != nil {
|
||||||
if !k.usageOnError || !isMissingChildError(err) {
|
return nil, &ParseError{error: err, Context: ctx}
|
||||||
return nil, &ParseError{error: err, Context: ctx}
|
|
||||||
}
|
|
||||||
ctx.Error = err
|
|
||||||
}
|
}
|
||||||
if err = k.applyHook(ctx, "AfterApply"); err != nil {
|
if err = k.applyHook(ctx, "AfterApply"); err != nil {
|
||||||
return nil, &ParseError{error: err, Context: ctx}
|
return nil, &ParseError{error: err, Context: ctx}
|
||||||
|
|||||||
@@ -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.
|
// ClearResolvers clears all existing resolvers.
|
||||||
func ClearResolvers() Option {
|
func ClearResolvers() Option {
|
||||||
return OptionFunc(func(k *Kong) error {
|
return OptionFunc(func(k *Kong) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user