From 097bba54ecc3ca993ab0e2703330b6e78d219ea3 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Tue, 27 Oct 2020 17:32:34 +1100 Subject: [PATCH] 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. --- context.go | 14 +++++++------- error.go | 31 ------------------------------- kong.go | 20 ++++++++------------ options.go | 8 -------- 4 files changed, 15 insertions(+), 58 deletions(-) diff --git a/context.go b/context.go index f07f537..42bb76b 100644 --- a/context.go +++ b/context.go @@ -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. diff --git a/error.go b/error.go index 36f512b..30b8858 100644 --- a/error.go +++ b/error.go @@ -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 -} diff --git a/kong.go b/kong.go index d46915e..b94620a 100644 --- a/kong.go +++ b/kong.go @@ -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} diff --git a/options.go b/options.go index 1369b6b..5bd36c3 100644 --- a/options.go +++ b/options.go @@ -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 {