@@ -12,7 +12,7 @@ var version = "unknown"
1212var versionCmd = & cobra.Command {
1313 Use : "version" ,
1414 Short : "Print the version number of oshiv CLI" ,
15- Long : ` Print the version number of oshiv CLI` ,
15+ Long : " Print the version number of oshiv CLI" ,
1616 Run : func (cmd * cobra.Command , args []string ) {
1717 printVersion ()
1818 },
@@ -26,14 +26,16 @@ func printVersion() {
2626func init () {
2727 // Add the version command for long form (e.g., `oshiv version`)
2828 rootCmd .AddCommand (versionCmd )
29- rootCmd .PersistentFlags ().BoolP ("version" , "v" , false , "Print the version number of oshiv CLI" )
30- rootCmd .Flags ().BoolP ("version" , "v" , false , "Print the short version number of oshiv CLI" )
3129
32- cobra . OnInitialize ( checkVersionFlag )
33- }
30+ // Register a global persistent flag to support short form (e.g., `oshiv -v` )
31+ rootCmd . PersistentFlags (). BoolP ( "version" , "v" , false , "Print the version number of oshiv CLI" )
3432
35- func checkVersionFlag () {
36- if versionFlag , _ := rootCmd .Flags ().GetBool ("version" ); versionFlag {
37- printVersion ()
33+ // Override the persistent pre-run hook to check for the `-v` flag
34+ rootCmd .PersistentPreRun = func (cmd * cobra.Command , args []string ) {
35+ if versionFlag , _ := cmd .Flags ().GetBool ("version" ); versionFlag {
36+ printVersion ()
37+ // Exit to avoid running another command if -v is passed
38+ cobra .CheckErr (nil )
39+ }
3840 }
3941}
0 commit comments