Add generated help and version commands
This commit is contained in:
16
parser.go
16
parser.go
@@ -10,10 +10,17 @@ func (a *App) parse(args []string, options internalcobra.ParseOptions) internalc
|
||||
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
|
||||
}
|
||||
root := &internalcobra.Command{Name: a.identity.Name}
|
||||
names := make([]string, 0, len(a.commands))
|
||||
for name, node := range a.commands {
|
||||
if !node.reserved {
|
||||
if !node.reserved || node.command.payload != nil {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
@@ -21,7 +28,7 @@ func (a *App) parse(args []string, options internalcobra.ParseOptions) internalc
|
||||
for _, name := range names {
|
||||
root.Children = append(root.Children, bridgeCommand(a.commands[name].command))
|
||||
}
|
||||
return internalcobra.Parse(root, args, options)
|
||||
return root
|
||||
}
|
||||
|
||||
func bridgeCommand(command Command) *internalcobra.Command {
|
||||
@@ -33,8 +40,11 @@ func bridgeCommand(command Command) *internalcobra.Command {
|
||||
if command.payload != nil {
|
||||
declaration.Runnable = command.payload.hasRun()
|
||||
declaration.Flags = command.payload.bridgeFlags()
|
||||
declaration.Arguments = command.payload.bridgeArguments()
|
||||
}
|
||||
for _, child := range command.children {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user