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.command.record != nil && node.command.record.absent { continue } if !node.reserved || node.command.record.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 { record := command.record declaration := &internalcobra.Command{ Name: record.name, Summary: record.summary, Description: record.description, } if record.payload != nil { declaration.Runnable = record.payload.hasRun() declaration.Flags = record.payload.bridgeFlags() declaration.Arguments = record.payload.bridgeArguments() } children := append([]Command(nil), record.children...) sort.Slice(children, func(i, j int) bool { return children[i].record.name < children[j].record.name }) for _, child := range children { if child.record == nil || child.record.absent { continue } declaration.Children = append(declaration.Children, bridgeCommand(child)) } return declaration }