MCPcopy
hub / github.com/owenthereal/ccat / Find

Method Find

Godeps/_workspace/src/github.com/spf13/cobra/command.go:379–431  ·  view source on GitHub ↗

find the target command given the args and command tree Meant to be run on the highest node. Only searches down.

(args []string)

Source from the content-addressed store, hash-verified

377// find the target command given the args and command tree
378// Meant to be run on the highest node. Only searches down.
379func (c *Command) Find(args []string) (*Command, []string, error) {
380 if c == nil {
381 return nil, nil, fmt.Errorf("Called find() on a nil Command")
382 }
383
384 // If there are no arguments, return the root command. If the root has no
385 // subcommands, args reflects arguments that should actually be passed to
386 // the root command, so also return the root command.
387 if len(args) == 0 || !c.Root().HasSubCommands() {
388 return c.Root(), args, nil
389 }
390
391 var innerfind func(*Command, []string) (*Command, []string)
392
393 innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
394 if len(innerArgs) > 0 && c.HasSubCommands() {
395 argsWOflags := stripFlags(innerArgs, c)
396 if len(argsWOflags) > 0 {
397 matches := make([]*Command, 0)
398 for _, cmd := range c.commands {
399 if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match
400 return innerfind(cmd, argsMinusFirstX(innerArgs, argsWOflags[0]))
401 } else if EnablePrefixMatching {
402 if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match
403 matches = append(matches, cmd)
404 }
405 for _, x := range cmd.Aliases {
406 if strings.HasPrefix(x, argsWOflags[0]) {
407 matches = append(matches, cmd)
408 }
409 }
410 }
411 }
412
413 // only accept a single prefix match - multiple matches would be ambiguous
414 if len(matches) == 1 {
415 return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))
416 }
417 }
418 }
419
420 return c, innerArgs
421 }
422
423 commandFound, a := innerfind(c, args)
424
425 // If we matched on the root, but we asked for a subcommand, return an error
426 if commandFound.Name() == c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() != args[0] {
427 return nil, a, fmt.Errorf("unknown command %q", a[0])
428 }
429
430 return commandFound, a, nil
431}
432
433func (c *Command) Root() *Command {
434 var findRoot func(*Command) *Command

Callers 2

ExecuteMethod · 0.95
HelpFuncMethod · 0.80

Calls 6

HasSubCommandsMethod · 0.95
RootMethod · 0.95
NameMethod · 0.95
stripFlagsFunction · 0.85
argsMinusFirstXFunction · 0.85
HasAliasMethod · 0.80

Tested by

no test coverage detected