Add UsageOnMissing option to Kong
Adds an UsageOnMissing option which configures Kong to omit errors and display usage when a command is invalid due to a missing argument. This can be useful if a user wishes for the top-level command to print usage when no args are supplied, or if they would prefer child commands which have their own subcommands to print usage specific to that command. Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
This commit is contained in:
@@ -187,6 +187,9 @@ func (c *Context) Validate() error { // nolint: gocyclo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := checkMissingChildren(node); err != nil {
|
if err := checkMissingChildren(node); err != nil {
|
||||||
|
if c.Kong.usageOnMissing {
|
||||||
|
return c.PrintUsage(false)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := checkMissingPositionals(positionals, node.Positional); err != nil {
|
if err := checkMissingPositionals(positionals, node.Positional); err != nil {
|
||||||
@@ -624,6 +627,9 @@ 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 nil
|
||||||
|
}
|
||||||
return fmt.Errorf("no command selected")
|
return fmt.Errorf("no command selected")
|
||||||
}
|
}
|
||||||
return c.RunNode(node, binds...)
|
return c.RunNode(node, binds...)
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ type Kong struct {
|
|||||||
|
|
||||||
noDefaultHelp bool
|
noDefaultHelp bool
|
||||||
usageOnError bool
|
usageOnError bool
|
||||||
|
usageOnMissing bool
|
||||||
help HelpPrinter
|
help HelpPrinter
|
||||||
helpFormatter HelpValueFormatter
|
helpFormatter HelpValueFormatter
|
||||||
helpOptions HelpOptions
|
helpOptions HelpOptions
|
||||||
|
|||||||
@@ -216,6 +216,14 @@ 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