Skip to content

Commit

Permalink
custom traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 13, 2022
1 parent 5ba8503 commit 07466f3
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/traverse/traverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package traverse

import "github.com/spf13/cobra"

func Traverse(cmd *cobra.Command, args []string) (*cobra.Command, error) {
return traverse(cmd, args[:len(args)-1])
}

func traverse(cmd *cobra.Command, args []string) (*cobra.Command, error) {
preRun(cmd, args)

if err := cmd.ParseFlags(args); err != nil { // TODO filter errors
return nil, err
} else {
args = cmd.Flags().Args()
}

return cmd, nil
}

func preRun(cmd *cobra.Command, args []string) {
if subcommand, _, err := cmd.Find([]string{"_carapace"}); err != nil {
if subcommand.PreRun != nil {
subcommand.PreRun(cmd, args)
}
}
}

0 comments on commit 07466f3

Please sign in to comment.