Honor help-only input checks before starting features
This commit is contained in:
13
command.go
13
command.go
@@ -17,6 +17,10 @@ import (
|
||||
// before changing an App registry.
|
||||
type Command struct{ record *commandRecord }
|
||||
|
||||
// ErrHelpRequested may be returned by CommandSpec.CheckInput to display command
|
||||
// help without starting invocation features or enforcing their required flags.
|
||||
var ErrHelpRequested = errors.New("command help requested")
|
||||
|
||||
// Declaration is a captured canonical command or application routing rule
|
||||
// accepted by New and App.Register. Use []Declaration when collecting both
|
||||
// kinds. Group and RootGroup children remain Commands; install routing rules
|
||||
@@ -189,7 +193,8 @@ type CommandSpec[I any] struct {
|
||||
Flags []Flag[I]
|
||||
// Arguments declares the command's positional inputs.
|
||||
Arguments []Argument[I]
|
||||
// CheckInput validates declared combinations of parsed inputs.
|
||||
// CheckInput validates declared combinations of parsed inputs. Returning
|
||||
// ErrHelpRequested displays help without running the command or its features.
|
||||
CheckInput func(I) error
|
||||
// Run handles an invocation. It may be nil for a command used as a group.
|
||||
Run func(Invocation, I) Outcome
|
||||
@@ -258,9 +263,15 @@ func (c typedCommand[I]) prepare(flagValues, flagSpellings [][]string, occurrenc
|
||||
input := prepared.build()
|
||||
if c.spec.CheckInput != nil {
|
||||
if err := c.spec.CheckInput(input); err != nil {
|
||||
if errors.Is(err, ErrHelpRequested) {
|
||||
return current.renderHelp(current.route.canonicalPath)
|
||||
}
|
||||
return classifyInputError(err)
|
||||
}
|
||||
}
|
||||
if current.featureEnabled && current.featureRequiredError != nil {
|
||||
return classifyInputError(current.featureRequiredError)
|
||||
}
|
||||
current.startFeatures()
|
||||
if c.privateRun != nil {
|
||||
return c.privateRun(current.commandContext(), input)
|
||||
|
||||
Reference in New Issue
Block a user