func (c *SuperCommand) FindClosestSubCommand(name string) (string, Command, bool) {
	// Exit early if there are no subcmds
	if len(c.subcmds) == 0 {
		return "", nil, false
	}

	// Attempt to find the closest match of a substring.
	type Indexed = struct {
		Name  string
		Value int
	}
	matches := make([]Indexed, 0, len(c.subcmds))
	for cmdName := range c.subcmds {
		matches = append(matches, Indexed{
			Name:  cmdName,
			Value: levenshteinDistance(name, cmdName),
		})
	}
	// Find the smallest levenshtein distance. If two values are the same,
	// fallback to sorting on the name, which should give predictable results.
	sort.Slice(matches, func(i, j int) bool {
		if matches[i].Value < matches[j].Value {
			return true
		}
		if matches[i].Value > matches[j].Value {
			return false
		}
		return matches[i].Name < matches[j].Name
	})
	matchedName := matches[0].Name
	matchedValue := matches[0].Value

	// If the matched value is less than the length+1 of the string, fail the
	// match.
	if _, ok := c.subcmds[matchedName]; ok && matchedName != "" && matchedValue < len(matchedName)+1 {
		return matchedName, c.subcmds[matchedName].command, true
	}
	return "", nil, false
}
// FindClosestSubCommand attempts to find a sub command by a given name.
// This is used to help locate potential commands where the name isn't an
// exact match.
// If the resulting fuzzy match algorithm returns a value that is itself too
// far away from the size of the word, we disgard that and say a match isn't
// relavent i.e. "foo" "barsomethingfoo" would not match
FindClosestSubCommand attempts to find a sub command by a given name.
This is used to help locate potential commands where the name isn't an
exact match.
If the resulting fuzzy match algorithm returns a value that is itself too
far away from the size of the word, we disgard that and say a match isn't
relavent i.e. "foo" "barsomethingfoo" would not match
// FindClosestSubCommand attempts to find a sub command by a given name . // This is used to help locate potential commands where the name isn ' t an // exact match . // If the resulting fuzzy match algorithm returns a value that is itself too // far away from the size of the word we disgard that and say a match isn ' t // relavent i . e . " foo " " barsomethingfoo " would not match
func (c *SuperCommand) FindClosestSubCommand(name string) (string, Command, bool) {
	if len(c.subcmds) == 0 {
		return "", nil, false
	}
	type Indexed = struct {
		Name  string
		Value int
	}
	matches := make([]Indexed, 0, len(c.subcmds))
	for cmdName := range c.subcmds {
		matches = append(matches, Indexed{
			Name:  cmdName,
			Value: levenshteinDistance(name, cmdName),
		})
	}
	sort.Slice(matches, func(i, j int) bool {
		if matches[i].Value < matches[j].Value {
			return true
		}
		if matches[i].Value > matches[j].Value {
			return false
		}
		return matches[i].Name < matches[j].Name
	})
	matchedName := matches[0].Name
	matchedValue := matches[0].Value
	if _, ok := c.subcmds[matchedName]; ok && matchedName != "" && matchedValue < len(matchedName)+1 {
		return matchedName, c.subcmds[matchedName].command, true
	}
	return "", nil, false
}
func ( c * SuperCommand ) FindClosestSubCommand ( name string ) ( string , Command , bool ) { if len ( c . subcmds ) == 0 { return " " , nil , false } type Indexed = struct { Name string Value int } matches := make ( [ ] Indexed , 0 , len ( c . subcmds ) ) for cmdName := range c . subcmds { matches = append ( matches , Indexed { Name : cmdName , Value : levenshteinDistance ( name , cmdName ) , } ) } sort . Slice ( matches , func ( i , j int ) bool { if matches [ i ] . Value < matches [ j ] . Value { return true } if matches [ i ] . Value > matches [ j ] . Value { return false } return matches [ i ] . Name < matches [ j ] . Name } ) matchedName := matches [ 0 ] . Name matchedValue := matches [ 0 ] . Value if _ , ok := c . subcmds [ matchedName ] ; ok && matchedName != " " && matchedValue < len ( matchedName ) + 1 { return matchedName , c . subcmds [ matchedName ] . command , true } return " " , nil , false }
FindClosestSubCommand attempts to find a sub command by a given name.
FindClosestSubCommand attempts to find a sub command by a given name .
// Exit early if there are no subcmds // Attempt to find the closest match of a substring. // Find the smallest levenshtein distance. If two values are the same, // fallback to sorting on the name, which should give predictable results. // If the matched value is less than the length+1 of the string, fail the // match.