Do not run parent node on missing child err with usageOnMissing option

Refactors usageOnMissing option to not run parent commands after
printing usage when a child node is missing.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
This commit is contained in:
hasheddan
2020-10-04 19:27:12 -05:00
committed by Alec Thomas
parent 5b3fd5476a
commit a062611ecf
3 changed files with 40 additions and 12 deletions
+31
View File
@@ -1,5 +1,10 @@
package kong
import (
"fmt"
"strings"
)
// ParseError is the error type returned by Kong.Parse().
//
// It contains the parse Context that triggered the error.
@@ -10,3 +15,29 @@ 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
}