You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this is intentional but if the command does not have a Run function set up, the PersistentPreRun function will not be triggered on this command.
I had this issue where I don't need to Run anything for a bunch of my subcommands but I still want to be able to show --version for the subcommand via PersistentPreRun.
If this is intentional, can we update the user guide to reflect this? If not, I can try to fix this issue. Just spent hours debugging until realized might have something to do with Run.
Steps to reproduce
Setup the project with go mod init example-project
Setup external modules by go mod tidy
Get the cobra package
Build and run
Run ./executable --version will print out
PersistentPreRun in rootcmd runs
inside the flag
Run ./executable child --version will print out
PersistentPreRun in rootcmd runs
inside the flag
Run ./executable grandchild --version will print out
PersistentPreRun in rootcmd runs
inside the flag
However, if you comment out any of the Run function from any of the command, the output will be the content from --help
Hugo child is a very fast static site generator
Usage:
Flags:
-h, --help helpfor child
Global Flags:
-v, --version Show version
Additional help topics:
hugo child grandchild Hugo grand child is a very fast static site generator
Code to reproduce
package main
import (
"fmt""os""github.com/spf13/cobra"
)
varshowVersionboolvarrootCmd=&cobra.Command{
Use: "hugo",
Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with love by spf13 and friends in Go. Complete documentation is available at https://gohugo.io/documentation/`,
Run: func(cmd*cobra.Command, args []string) {
// Do Stuff Here
},
PersistentPreRun: func(cmd*cobra.Command, args []string) {
// Do Stuff Herefmt.Println("PersistentPreRun in rootcmd runs")
ifshowVersion {
fmt.Println("Inside the flag")
}
},
}
varchildCmd=&cobra.Command{
Use: "child",
Short: "Hugo child is a very fast static site generator",
Run: func(cmd*cobra.Command, args []string) {
// Do Stuff Here
},
}
vargrandChildCmd=&cobra.Command{
Use: "grandchild",
Short: "Hugo grand child is a very fast static site generator",
Run: func(cmd*cobra.Command, args []string) {
// Do Stuff Here
},
}
funcExecute() {
iferr:=rootCmd.Execute(); err!=nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
funcinit() {
rootCmd.AddCommand(childCmd)
childCmd.AddCommand(grandChildCmd)
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "Show version")
}
funcmain() {
Execute()
}
The text was updated successfully, but these errors were encountered:
haoming29
changed the title
PersistentPreRun was only executed if the command have a Run function
PersistentPreRun was only executed if the command has a Run function
Oct 5, 2023
This is the expected behaviour as per the code. If you want to always print something for every single command maybe you can just print it in the main() function
I'm not sure if this is intentional but if the command does not have a
Run
function set up, thePersistentPreRun
function will not be triggered on this command.I had this issue where I don't need to
Run
anything for a bunch of my subcommands but I still want to be able to show--version
for the subcommand viaPersistentPreRun
.If this is intentional, can we update the user guide to reflect this? If not, I can try to fix this issue. Just spent hours debugging until realized might have something to do with Run.
Steps to reproduce
Setup the project with
go mod init example-project
Setup external modules by
go mod tidy
Get the cobra package
Build and run
Run
./executable --version
will print outPersistentPreRun in rootcmd runs inside the flag
./executable child --version
will print outPersistentPreRun in rootcmd runs inside the flag
./executable grandchild --version
will print outPersistentPreRun in rootcmd runs inside the flag
However, if you comment out any of the Run function from any of the command, the output will be the content from --help
Code to reproduce
The text was updated successfully, but these errors were encountered: