Skip to content

child command is not calling the parents PersistentPreRun function #216

Closed
@algrebe

Description

@algrebe

Hi, I've been reading the documentation on the readme that says that the When the subcommand is executed, it will run the root command's PersistentPreRun but not the root command's PersistentPostRun , but the PersistentPreRun isn't getting executed.

package main

import (
    "fmt"
    "github.com/spf13/cobra"
)

func main() {
    var rootCmd = &cobra.Command{
        Use:   "root [sub]",
        Short: "My root command",
        PersistentPreRun: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside rootCmd PersistentPreRun with args: %v\n", args)
        },
        PreRun: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside rootCmd PreRun with args: %v\n", args)
        },
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside rootCmd Run with args: %v\n", args)
        },
        PostRun: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside rootCmd PostRun with args: %v\n", args)
        },
        PersistentPostRun: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside rootCmd PersistentPostRun with args: %v\n", args)
        },
    }

    var subCmd = &cobra.Command{
        Use:   "sub [no options!]",
        Short: "My subcommand",
        PersistentPreRun: func(cmd *cobra.Command, args []string) {
                fmt.Printf("Inside subCmd PersistentPreRun with args: %v\n", args)
        },
        PreRun: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside subCmd PreRun with args: %v\n", args)
        },
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside subCmd Run with args: %v\n", args)
        },
        PostRun: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside subCmd PostRun with args: %v\n", args)
        },
        PersistentPostRun: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Inside subCmd PersistentPostRun with args: %v\n", args)
        },
    }

    rootCmd.AddCommand(subCmd)
    _ = rootCmd.Execute()
}
root@vagrant-ubuntu-vivid-64:/tmp# go version
go version go1.5.2 linux/amd64

root@vagrant-ubuntu-vivid-64:/tmp# go build test.go

root@vagrant-ubuntu-vivid-64:/tmp# ./test 
Inside rootCmd PersistentPreRun with args: []
Inside rootCmd PreRun with args: []
Inside rootCmd Run with args: []
Inside rootCmd PostRun with args: []
Inside rootCmd PersistentPostRun with args: []

root@vagrant-ubuntu-vivid-64:/tmp# ./test sub arg1 arg2 arg3
Inside subCmd PersistentPreRun with args: [arg1 arg2 arg3]
Inside subCmd PreRun with args: [arg1 arg2 arg3]
Inside subCmd Run with args: [arg1 arg2 arg3]
Inside subCmd PostRun with args: [arg1 arg2 arg3]
Inside subCmd PersistentPostRun with args: [arg1 arg2 arg3]

the second example should have also had the output "Inside rootCmd PersistentPreRun" right ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions