in agent/session/plugin/cli/command.go [150:238]
func (c *Command) executeInner(ctx *Context, args []string) error {
//
// fmt.Printf(">>> Execute Command: %s args=%v\n", c.Name, args)
parser := NewParser(args, ctx)
//
// get next arg
nextArg, _, err := parser.ReadNextArg()
if err != nil {
return err
}
//
// if next arg is help, run help
if nextArg == "help" {
ctx.help = true
return c.executeInner(ctx, parser.GetRemains())
}
//
// if next args is not empty, try find sub commands
if nextArg != "" {
//
// if has sub command, run it
subCommand := c.GetSubCommand(nextArg)
if subCommand != nil {
ctx.EnterCommand(subCommand)
return subCommand.executeInner(ctx, parser.GetRemains())
}
//
// no sub command and command.Run == nil
// raise error
if c.Run == nil {
// c.executeHelp(ctx, args, fmt.Errorf("unknown command: %s", nextArg))
return NewInvalidCommandError(nextArg, ctx)
}
}
// cmd is find by args, try run cmd.Run
// parse remain args
remainArgs, err := parser.ReadAll()
if err != nil {
return fmt.Errorf("parse failed %s", err)
}
//
// check flags
err = ctx.CheckFlags()
if err != nil {
return err
}
if HelpFlag(ctx.Flags()).IsAssigned() {
ctx.help = true
}
callArgs := make([]string, 0)
if nextArg != "" {
callArgs = append(callArgs, nextArg)
}
for _, s := range remainArgs {
if s != "help" {
callArgs = append(callArgs, s)
} else {
ctx.help = true
}
}
if ctx.completion != nil {
if c.AutoComplete != nil {
ss := c.AutoComplete(ctx, callArgs)
for _, s := range ss {
Printf(ctx.Writer(), "%s\n", s)
}
} else {
c.ExecuteComplete(ctx, callArgs)
}
return nil
}
if ctx.help {
c.executeHelp(ctx, callArgs)
return nil
} else if c.Run == nil {
c.executeHelp(ctx, callArgs)
return nil
} else {
return c.Run(ctx, callArgs)
}
}