婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > golang中命令行庫cobra的使用方法示例

golang中命令行庫cobra的使用方法示例

熱門標簽:江西轉化率高的羿智云外呼系統 中國地圖標注省會高清 浙江高速公路地圖標注 廣州呼叫中心外呼系統 西部云谷一期地圖標注 地圖標注的汽車標 南通如皋申請開通400電話 學海導航地圖標注 高德地圖標注口訣

簡介

Cobra既是一個用來創建強大的現代CLI命令行的golang庫,也是一個生成程序應用和命令行文件的程序。下面是Cobra使用的一個演示:

Cobra提供的功能

  • 簡易的子命令行模式,如 app server, app fetch等等
  • 完全兼容posix命令行模式
  • 嵌套子命令subcommand
  • 支持全局,局部,串聯flags
  • 使用Cobra很容易的生成應用程序和命令,使用cobra create appname和cobra add cmdname
  • 如果命令輸入錯誤,將提供智能建議,如 app srver,將提示srver沒有,是否是app server
  • 自動生成commands和flags的幫助信息
  • 自動生成詳細的help信息,如app help
  • 自動識別-h,--help幫助flag
  • 自動生成應用程序在bash下命令自動完成功能
  • 自動生成應用程序的man手冊
  • 命令行別名
  • 自定義help和usage信息
  • 可選的緊密集成的viper apps

如何使用

上面所有列出的功能我沒有一一去使用,下面我來簡單介紹一下如何使用Cobra,基本能夠滿足一般命令行程序的需求,如果需要更多功能,可以研究一下源碼github。

安裝cobra

Cobra是非常容易使用的,使用go get來安裝最新版本的庫。當然這個庫還是相對比較大的,可能需要安裝它可能需要相當長的時間,這取決于你的速網。安裝完成后,打開GOPATH目錄,bin目錄下應該有已經編譯好的cobra.exe程序,當然你也可以使用源代碼自己生成一個最新的cobra程序。

> go get -v github.com/spf13/cobra/cobra

使用cobra生成應用程序

假設現在我們要開發一個基于CLIs的命令程序,名字為demo。首先打開CMD,切換到GOPATH的src目錄下[^1],執行如下指令:
[^1]:cobra.exe只能在GOPATH目錄下執行

src> ..\bin\cobra.exe init demo 
Your Cobra application is ready at
C:\Users\liubo5\Desktop\transcoding_tool\src\demo
Give it a try by going there and running `go run main.go`
Add commands to it by running `cobra add [cmdname]`

在src目錄下會生成一個demo的文件夾,如下:

▾ demo
    ▾ cmd/
        root.go
    main.go

如果你的demo程序沒有subcommands,那么cobra生成應用程序的操作就結束了。

如何實現沒有子命令的CLIs程序

接下來就是可以繼續demo的功能設計了。例如我在demo下面新建一個包,名稱為imp。如下:

▾ demo
    ▾ cmd/
        root.go
    ▾ imp/
        imp.go
        imp_test.go
    main.go

imp.go文件的代碼如下:

package imp

import(
 "fmt"
)

func Show(name string, age int) {
 fmt.Printf("My Name is %s, My age is %d\n", name, age)
}

demo程序成命令行接收兩個參數name和age,然后打印出來。打開cobra自動生成的main.go文件查看:

// Copyright © 2016 NAME HERE EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "demo/cmd"

func main() {
 cmd.Execute()
}

可以看出main函數執行cmd包,所以我們只需要在cmd包內調用imp包就能實現demo程序的需求。接著打開root.go文件查看:

// Copyright © 2016 NAME HERE EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
 "fmt"
 "os"

 "github.com/spf13/cobra"
 "github.com/spf13/viper"
)

var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = cobra.Command{
 Use: "demo",
 Short: "A brief description of your application",
 Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
 if err := RootCmd.Execute(); err != nil {
  fmt.Println(err)
  os.Exit(-1)
 }
}

func init() {
 cobra.OnInitialize(initConfig)

 // Here you will define your flags and configuration settings.
 // Cobra supports Persistent Flags, which, if defined here,
 // will be global for your application.

 RootCmd.PersistentFlags().StringVar(cfgFile, "config", "", "config file (default is $HOME/.demo.yaml)")
 // Cobra also supports local flags, which will only run
 // when this action is called directly.
 RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
 if cfgFile != "" { // enable ability to specify config file via flag
  viper.SetConfigFile(cfgFile)
 }

 viper.SetConfigName(".demo") // name of config file (without extension)
 viper.AddConfigPath("$HOME") // adding home directory as first search path
 viper.AutomaticEnv()   // read in environment variables that match

 // If a config file is found, read it in.
 if err := viper.ReadInConfig(); err == nil {
  fmt.Println("Using config file:", viper.ConfigFileUsed())
 }
}

從源代碼來看cmd包進行了一些初始化操作并提供了Execute接口。十分簡單,其中viper是cobra集成的配置文件讀取的庫,這里不需要使用,我們可以注釋掉(不注釋可能生成的應用程序很大約10M,這里沒喲用到最好是注釋掉)。cobra的所有命令都是通過cobra.Command這個結構體實現的。為了實現demo功能,顯然我們需要修改RootCmd。修改后的代碼如下:

// Copyright © 2016 NAME HERE EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
 "fmt"
 "os"

 "github.com/spf13/cobra"
 // "github.com/spf13/viper"
 "demo/imp"
)

//var cfgFile string
var name string
var age int

// RootCmd represents the base command when called without any subcommands
var RootCmd = cobra.Command{
 Use: "demo",
 Short: "A test demo",
 Long: `Demo is a test appcation for print things`,
 // Uncomment the following line if your bare application
 // has an action associated with it:
 Run: func(cmd *cobra.Command, args []string) {
  if len(name) == 0 {
   cmd.Help()
   return
  }
  imp.Show(name, age)
 },
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
 if err := RootCmd.Execute(); err != nil {
  fmt.Println(err)
  os.Exit(-1)
 }
}

func init() {
 // cobra.OnInitialize(initConfig)

 // Here you will define your flags and configuration settings.
 // Cobra supports Persistent Flags, which, if defined here,
 // will be global for your application.

 // RootCmd.PersistentFlags().StringVar(cfgFile, "config", "", "config file (default is $HOME/.demo.yaml)")
 // Cobra also supports local flags, which will only run
 // when this action is called directly.
 // RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
 RootCmd.Flags().StringVarP(name, "name", "n", "", "person's name")
 RootCmd.Flags().IntVarP(age, "age", "a", 0, "person's age")
}

// initConfig reads in config file and ENV variables if set.
//func initConfig() {
// if cfgFile != "" { // enable ability to specify config file via flag
//  viper.SetConfigFile(cfgFile)
// }

// viper.SetConfigName(".demo") // name of config file (without extension)
// viper.AddConfigPath("$HOME") // adding home directory as first search path
// viper.AutomaticEnv()   // read in environment variables that match

// // If a config file is found, read it in.
// if err := viper.ReadInConfig(); err == nil {
//  fmt.Println("Using config file:", viper.ConfigFileUsed())
// }
//}

到此demo的功能已經實現了,我們編譯運行一下看看實際效果:

>demo.exe
Demo is a test appcation for print things

Usage:
  demo [flags]

Flags:
  -a, --age int       person's age
  -h, --help          help for demo
  -n, --name string   person's name

>demo -n borey --age 26
My Name is borey, My age is 26

如何實現帶有子命令的CLIs程序

在執行cobra.exe init demo之后,繼續使用cobra為demo添加子命令test:

src\demo>..\..\bin\cobra add test
test created at C:\Users\liubo5\Desktop\transcoding_tool\src\demo\cmd\test.go

在src目錄下demo的文件夾下生成了一個cmd\test.go文件,如下:

▾ demo
    ▾ cmd/
        root.go
        test.go
    main.go

接下來的操作就和上面修改root.go文件一樣去配置test子命令。效果如下:

src\demo>demo
Demo is a test appcation for print things

Usage:
 demo [flags]
 demo [command]

Available Commands:
 test  A brief description of your command

Flags:
 -a, --age int  person's age
 -h, --help   help for demo
 -n, --name string person's name

Use "demo [command] --help" for more information about a command.

可以看出demo既支持直接使用標記flag,又能使用子命令

src\demo>demo test -h
A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

Usage:
 demo test [flags]

調用test命令輸出信息,這里沒有對默認信息進行修改。

src\demo>demo tst
Error: unknown command "tst" for "demo"

Did you mean this?
  test

Run 'demo --help' for usage.
unknown command "tst" for "demo"

Did you mean this?
  test

這是錯誤命令提示功能

OVER

Cobra的使用就介紹到這里,更新細節可去github詳細研究一下。這里只是一個簡單的使用入門介紹,如果有錯誤之處,敬請指出,謝謝~

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • golang執行命令操作 exec.Command
  • Golang中基礎的命令行模塊urfave/cli的用法說明
  • golang執行命令獲取執行結果狀態(推薦)
  • Golang命令行進行debug調試操作
  • 利用Golang如何調用Linux命令詳解
  • Golang匯編命令解讀及使用

標簽:吐魯番 貴州 曲靖 東營 德宏 常州 保定 許昌

巨人網絡通訊聲明:本文標題《golang中命令行庫cobra的使用方法示例》,本文關鍵詞  golang,中,命令行,庫,cobra,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《golang中命令行庫cobra的使用方法示例》相關的同類信息!
  • 本頁收集關于golang中命令行庫cobra的使用方法示例的相關信息資訊供網民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    国产成人av电影在线播放| 欧美日韩亚州综合| 国产亚洲欧美色| 国产精品亚洲第一区在线暖暖韩国| 欧美一级欧美一级在线播放| 日本vs亚洲vs韩国一区三区二区| 91精品国产福利| 国产麻豆一精品一av一免费 | 在线日韩一区二区| 水野朝阳av一区二区三区| 日韩午夜电影在线观看| 国产一区视频导航| 成人免费视频在线观看| 欧洲另类一二三四区| 免费xxxx性欧美18vr| 国产三级欧美三级| 一本色道a无线码一区v| 日本成人在线不卡视频| 久久精品无码一区二区三区| 色诱视频网站一区| 人人精品人人爱| 欧美国产精品中文字幕| 在线看一区二区| 精品一区二区免费看| 最好看的中文字幕久久| 91精品国产品国语在线不卡| 成人免费va视频| 丝瓜av网站精品一区二区| 久久亚洲精华国产精华液| 精品国产免费久久| 91日韩精品一区| 卡一卡二国产精品| 亚洲综合自拍偷拍| 2017欧美狠狠色| 欧美最猛性xxxxx直播| 国产在线一区二区综合免费视频| 1024国产精品| 2022国产精品视频| 欧美日韩在线免费视频| 粉嫩绯色av一区二区在线观看| 亚洲成av人综合在线观看| 中文字幕巨乱亚洲| 欧美成人r级一区二区三区| 色琪琪一区二区三区亚洲区| 精品一区二区在线播放| 亚洲国产精品久久不卡毛片| 国产精品天天看| 欧美xxxx在线观看| 91精品国产黑色紧身裤美女| 色视频欧美一区二区三区| 国产成人h网站| 美女久久久精品| 亚洲成人高清在线| 亚洲综合在线电影| 中文字幕一区二区三区在线不卡| 2017欧美狠狠色| 日韩精品专区在线影院重磅| 欧美综合天天夜夜久久| 91视视频在线观看入口直接观看www| 狠狠色综合播放一区二区| 日本亚洲天堂网| 亚洲电影视频在线| 一区二区三区四区在线| 18涩涩午夜精品.www| 国产日韩精品一区| 国产亚洲欧美色| 日本一区二区三区电影| 久久久久久免费网| 久久丝袜美腿综合| 久久综合精品国产一区二区三区| 日韩三级免费观看| 欧美成人乱码一区二区三区| 欧美成人一区二区三区在线观看 | 日韩一区二区三区免费看| 91久久精品午夜一区二区| 色呦呦网站一区| 欧美三区在线视频| 欧美日韩精品欧美日韩精品一综合| 在线观看日产精品| 欧美日韩一区三区四区| 6080日韩午夜伦伦午夜伦| 91精品欧美综合在线观看最新| 欧美日韩精品一区二区在线播放| 欧美老肥妇做.爰bbww视频| 在线成人午夜影院| 日韩精品资源二区在线| 久久久www成人免费毛片麻豆| 国产免费观看久久| 中文字幕欧美一区| 亚洲精品成人在线| 婷婷综合另类小说色区| 欧美日韩一区小说| 日韩三级在线免费观看| 久久久影视传媒| 国产精品成人免费在线| 亚洲精品国产第一综合99久久 | 91精品免费在线| 日韩精品一区二区在线| 国产日韩欧美电影| 一区二区三区免费观看| 亚洲国产精品影院| 美国av一区二区| 不卡视频一二三四| 欧美日韩视频不卡| 久久影院午夜论| 亚洲乱码精品一二三四区日韩在线| 亚洲无人区一区| 国产成人综合在线播放| 色综合久久天天| 日韩欧美精品在线视频| 1024亚洲合集| 国产在线播放一区二区三区| 91麻豆福利精品推荐| 91精品国产综合久久精品图片| 久久久另类综合| 香蕉av福利精品导航| 国产寡妇亲子伦一区二区| 欧美亚洲禁片免费| 国产欧美日韩综合精品一区二区| 亚洲成人一区在线| 成人动漫一区二区| 91精选在线观看| 亚洲精品国产第一综合99久久 | 久久久久综合网| 午夜欧美视频在线观看| 国产精品69毛片高清亚洲| 亚洲精品乱码久久久久久久久| 国产最新精品精品你懂的| 色又黄又爽网站www久久| 久久综合狠狠综合久久综合88| 一区二区三区 在线观看视频| 韩国毛片一区二区三区| 精品视频一区二区不卡| 中文在线一区二区| 国内精品国产成人国产三级粉色 | 欧美亚洲免费在线一区| 欧美极品aⅴ影院| 国产综合色视频| 91精品欧美综合在线观看最新| 亚洲天堂2014| a级精品国产片在线观看| 日韩色在线观看| 五月天亚洲精品| 欧美日韩黄色影视| 一区二区三区加勒比av| 99国产一区二区三精品乱码| 欧美激情一区二区三区在线| 日韩avvvv在线播放| 欧美日韩一本到| 亚洲一区在线视频观看| av欧美精品.com| 久久一区二区视频| 麻豆精品在线观看| 69av一区二区三区| 日日摸夜夜添夜夜添精品视频| 色狠狠一区二区| 亚洲精品日产精品乱码不卡| 成人白浆超碰人人人人| 国产精品女主播av| 成人av小说网| 亚洲欧洲国产日韩| 91美女视频网站| 亚洲欧美视频一区| 一本久久a久久免费精品不卡| 亚洲精品免费在线观看| 91福利国产成人精品照片| 一区二区三区在线观看网站| 91久久一区二区| 日韩不卡免费视频| 日韩精品一区二区三区swag | 日本欧美在线看| 日韩色在线观看| 国产精品影音先锋| 成人欧美一区二区三区黑人麻豆 | 夜夜亚洲天天久久| 欧美日韩日日骚| 日韩电影在线观看一区| 欧美一级一区二区| 韩国av一区二区三区四区 | 亚洲日本护士毛茸茸| 在线观看三级视频欧美| 日韩激情视频网站| 久久综合网色—综合色88| 国产.精品.日韩.另类.中文.在线.播放| 国产亚洲欧美一区在线观看| 91在线丨porny丨国产| 亚洲成人动漫一区| 久久老女人爱爱| 色综合一个色综合| 天堂午夜影视日韩欧美一区二区| 欧美成人r级一区二区三区| 成人午夜免费视频| 亚洲与欧洲av电影| 精品日韩99亚洲| 色综合天天做天天爱| 日韩成人一区二区| 国产精品麻豆欧美日韩ww| 欧美性大战久久| 高清在线成人网| 五月天激情小说综合|