github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/cmd/dlv/cmds/helphelpers/help.go (about) 1 package helphelpers 2 3 import ( 4 "github.com/spf13/cobra" 5 "github.com/spf13/pflag" 6 ) 7 8 // Prepare prepares cmd flag set for the invocation of its usage function by 9 // hiding flags that we want cobra to parse but we don't want to show to the 10 // user. 11 // We do this because not all flags associated with the root command are 12 // valid for all subcommands but we don't want to move them out of the root 13 // command and into subcommands, since that would change how cobra parses 14 // the command line. 15 // 16 // For example: 17 // 18 // dlv --headless debug 19 // 20 // must parse successfully even though the headless flag is not applicable 21 // to the 'connect' subcommand. 22 // 23 // Prepare is a destructive command, cmd can not be reused after it has been 24 // called. 25 func Prepare(cmd *cobra.Command) { 26 switch cmd.Name() { 27 case "dlv", "help", "run", "version": 28 hideAllFlags(cmd) 29 case "attach": 30 hideFlag(cmd, "build-flags") 31 hideFlag(cmd, "disable-aslr") 32 hideFlag(cmd, "redirect") 33 hideFlag(cmd, "wd") 34 case "connect": 35 hideFlag(cmd, "accept-multiclient") 36 hideFlag(cmd, "allow-non-terminal-interactive") 37 hideFlag(cmd, "api-version") 38 hideFlag(cmd, "build-flags") 39 hideFlag(cmd, "check-go-version") 40 hideFlag(cmd, "disable-aslr") 41 hideFlag(cmd, "headless") 42 hideFlag(cmd, "listen") 43 hideFlag(cmd, "only-same-user") 44 hideFlag(cmd, "redirect") 45 hideFlag(cmd, "wd") 46 case "dap": 47 hideFlag(cmd, "headless") 48 hideFlag(cmd, "accept-multiclient") 49 hideFlag(cmd, "init") 50 hideFlag(cmd, "backend") 51 hideFlag(cmd, "build-flags") 52 hideFlag(cmd, "wd") 53 hideFlag(cmd, "redirect") 54 hideFlag(cmd, "api-version") 55 hideFlag(cmd, "allow-non-terminal-interactive") 56 case "debug", "test": 57 // All flags apply 58 case "exec": 59 hideFlag(cmd, "build-flags") 60 case "replay", "core": 61 hideFlag(cmd, "backend") 62 hideFlag(cmd, "build-flags") 63 hideFlag(cmd, "disable-aslr") 64 hideFlag(cmd, "redirect") 65 hideFlag(cmd, "wd") 66 case "trace": 67 hideFlag(cmd, "accept-multiclient") 68 hideFlag(cmd, "allow-non-terminal-interactive") 69 hideFlag(cmd, "api-version") 70 hideFlag(cmd, "headless") 71 hideFlag(cmd, "init") 72 hideFlag(cmd, "listen") 73 hideFlag(cmd, "only-same-user") 74 } 75 } 76 77 func hideAllFlags(cmd *cobra.Command) { 78 cmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) { 79 flag.Hidden = true 80 }) 81 cmd.Flags().VisitAll(func(flag *pflag.Flag) { 82 flag.Hidden = true 83 }) 84 } 85 86 func hideFlag(cmd *cobra.Command, name string) { 87 if cmd == nil { 88 return 89 } 90 flag := cmd.Flags().Lookup(name) 91 if flag != nil { 92 flag.Hidden = true 93 return 94 } 95 hideFlag(cmd.Parent(), name) 96 }