From 0c495e4936b62e3599434bb9938cb5eb4e20ab53 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Wed, 26 Feb 2025 14:37:56 +1100 Subject: [PATCH] feat: add IgnoreDefault optional interface --- context.go | 7 ++++++- help.go | 6 ++++-- kong.go | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index 7b1d482..dcaf099 100644 --- a/context.go +++ b/context.go @@ -550,11 +550,16 @@ func (c *Context) trace(node *Node) (err error) { //nolint: gocyclo return c.maybeSelectDefault(flags, node) } +// IgnoreDefault can be implemented by flags that want to be applied before any default commands. +type IgnoreDefault interface { + IgnoreDefault() +} + // End of the line, check for a default command, but only if we're not displaying help, // otherwise we'd only ever display the help for the default command. func (c *Context) maybeSelectDefault(flags []*Flag, node *Node) error { for _, flag := range flags { - if flag.Name == "help" && flag.Set { + if _, ok := flag.Target.Interface().(IgnoreDefault); ok && flag.Set { return nil } } diff --git a/help.go b/help.go index 28290f6..8da1555 100644 --- a/help.go +++ b/help.go @@ -14,9 +14,11 @@ const ( ) // Help flag. -type helpValue bool +type helpFlag bool -func (h helpValue) BeforeReset(ctx *Context) error { +func (h helpFlag) IgnoreDefault() {} + +func (h helpFlag) BeforeReset(ctx *Context) error { options := ctx.Kong.helpOptions options.Summary = false err := ctx.Kong.help(options, ctx) diff --git a/kong.go b/kong.go index b85e145..a5e3d99 100644 --- a/kong.go +++ b/kong.go @@ -283,7 +283,7 @@ func (k *Kong) extraFlags() []*Flag { if k.noDefaultHelp { return nil } - var helpTarget helpValue + var helpTarget helpFlag value := reflect.ValueOf(&helpTarget).Elem() helpFlag := &Flag{ Short: 'h',