package commandkit import ( "sort" internalcobra "gitea.maximumdirect.net/eric/CommandKit/internal/cobra" ) func (a *App) parse(args []string, options internalcobra.ParseOptions) internalcobra.Result { if a == nil { return internalcobra.Result{Failure: &internalcobra.Failure{Message: "cannot parse commands on a nil app"}} } return internalcobra.Parse(a.bridgeTree(), args, options) } func (a *App) bridgeTree() *internalcobra.Command { if a == nil { return nil } if a.commandTree == nil { a.commandTree = a.buildBridgeTree() } return a.commandTree } func (a *App) buildBridgeTree() *internalcobra.Command { root := &internalcobra.Command{Name: a.identity.Name} names := make([]string, 0, len(a.commands)) for name, node := range a.commands { if !node.reserved || node.command.payload != nil { names = append(names, name) } } sort.Strings(names) for _, name := range names { root.Children = append(root.Children, bridgeCommand(a.commands[name].command)) } return root } func bridgeCommand(command Command) *internalcobra.Command { declaration := &internalcobra.Command{ Name: command.name, Summary: command.summary, Description: command.description, } if command.payload != nil { declaration.Runnable = command.payload.hasRun() declaration.Flags = command.payload.bridgeFlags() declaration.Arguments = command.payload.bridgeArguments() } children := append([]Command(nil), command.children...) sort.Slice(children, func(i, j int) bool { return children[i].name < children[j].name }) for _, child := range children { declaration.Children = append(declaration.Children, bridgeCommand(child)) } return declaration }