feat: add IgnoreDefault optional interface
This commit is contained in:
+6
-1
@@ -550,11 +550,16 @@ func (c *Context) trace(node *Node) (err error) { //nolint: gocyclo
|
|||||||
return c.maybeSelectDefault(flags, node)
|
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,
|
// 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.
|
// otherwise we'd only ever display the help for the default command.
|
||||||
func (c *Context) maybeSelectDefault(flags []*Flag, node *Node) error {
|
func (c *Context) maybeSelectDefault(flags []*Flag, node *Node) error {
|
||||||
for _, flag := range flags {
|
for _, flag := range flags {
|
||||||
if flag.Name == "help" && flag.Set {
|
if _, ok := flag.Target.Interface().(IgnoreDefault); ok && flag.Set {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,11 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Help flag.
|
// 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 := ctx.Kong.helpOptions
|
||||||
options.Summary = false
|
options.Summary = false
|
||||||
err := ctx.Kong.help(options, ctx)
|
err := ctx.Kong.help(options, ctx)
|
||||||
|
|||||||
@@ -283,7 +283,7 @@ func (k *Kong) extraFlags() []*Flag {
|
|||||||
if k.noDefaultHelp {
|
if k.noDefaultHelp {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var helpTarget helpValue
|
var helpTarget helpFlag
|
||||||
value := reflect.ValueOf(&helpTarget).Elem()
|
value := reflect.ValueOf(&helpTarget).Elem()
|
||||||
helpFlag := &Flag{
|
helpFlag := &Flag{
|
||||||
Short: 'h',
|
Short: 'h',
|
||||||
|
|||||||
Reference in New Issue
Block a user