Delve程式碼分析筆記(4)——構建command tree

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Delve使用cobra來構造command tree。先看root command,也就是dlv

func New() *cobra.Command {    ......    // Main dlv root command.    RootCommand = &cobra.Command{        Use:   "dlv",        Short: "Delve is a debugger for the Go programming language.",        Long:  dlvCommandLongDesc,    }    RootCommand.PersistentFlags().StringVarP(&Addr, "listen", "l", "localhost:0", "Debugging server listen address.")    RootCommand.PersistentFlags().BoolVarP(&Log, "log", "", false, "Enable debugging server logging.")    RootCommand.PersistentFlags().BoolVarP(&Headless, "headless", "", false, "Run debug server only, in headless mode.")    RootCommand.PersistentFlags().BoolVarP(&AcceptMulti, "accept-multiclient", "", false, "Allows a headless server to accept multiple client connections. Note that the server API is not reentrant and clients will have to coordinate")    RootCommand.PersistentFlags().IntVar(&ApiVersion, "api-version", 1, "Selects API version when headless")    RootCommand.PersistentFlags().StringVar(&InitFile, "init", "", "Init file, executed by the terminal client.")    RootCommand.PersistentFlags().StringVar(&BuildFlags, "build-flags", buildFlagsDefault, "Build flags, to be passed to the compiler.")    ......}

因為dlv command沒有實現run函數,所以單獨運行dlv命令只會列印cobra幫忙產生的預設輸出:

# dlvDelve is a source level debugger for Go programs.......Usage:  dlv [command]Available Commands:  version     Prints version.  ......Flags:      --accept-multiclient[=false]: Allows a headless server to accept multiple client connections. Note that the server API is not reentrant and clients will have to coordinate    ......

依次為Long descriptionUsageAvailable Commands等等。

再以trace subcommand為例,看如何把subcommand加到root command裡:

......// 'trace' subcommand.traceCommand := &cobra.Command{    Use:   "trace [package] regexp",    Short: "Compile and begin tracing program.",    Long:  "Trace program execution. Will set a tracepoint on every function matching the provided regular expression and output information when tracepoint is hit.",    Run:   traceCmd,}traceCommand.Flags().IntVarP(&traceAttachPid, "pid", "p", 0, "Pid to attach to.")traceCommand.Flags().IntVarP(&traceStackDepth, "stack", "s", 0, "Show stack trace with given depth.")RootCommand.AddCommand(traceCommand)......

Cobra提供兩種flags
a)Persistent Flags:對當前命令及其子命令都有效;
b)Local Flags:只對當前命令有效。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.