42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
|
|
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"}}
|
||
|
|
}
|
||
|
|
root := &internalcobra.Command{Name: a.identity.Name}
|
||
|
|
names := make([]string, 0, len(a.commands))
|
||
|
|
for name, node := range a.commands {
|
||
|
|
if !node.reserved {
|
||
|
|
names = append(names, name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
sort.Strings(names)
|
||
|
|
for _, name := range names {
|
||
|
|
root.Children = append(root.Children, bridgeCommand(a.commands[name].command))
|
||
|
|
}
|
||
|
|
return internalcobra.Parse(root, args, options)
|
||
|
|
}
|
||
|
|
|
||
|
|
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()
|
||
|
|
}
|
||
|
|
for _, child := range command.children {
|
||
|
|
declaration.Children = append(declaration.Children, bridgeCommand(child))
|
||
|
|
}
|
||
|
|
return declaration
|
||
|
|
}
|