![cobra logo](https://cloud.githubusercontent.com/assets/173412/10886352/ad566232-814f-11e5-9cd0-aa101788c117.png)
Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files.
Many of the most widely used Go projects are built using Cobra including:
* [Kubernetes](http://kubernetes.io/)
* [Hugo](http://gohugo.io)
* [rkt](https://github.com/coreos/rkt)
* [etcd](https://github.com/coreos/etcd)
* [Moby (former Docker)](https://github.com/moby/moby)
* [Docker (distribution)](https://github.com/docker/distribution)
* [OpenShift](https://www.openshift.com/)
* [Delve](https://github.com/derekparker/delve)
* [GopherJS](http://www.gopherjs.org/)
* [CockroachDB](http://www.cockroachlabs.com/)
* [Bleve](http://www.blevesearch.com/)
* [ProjectAtomic (enterprise)](http://www.projectatomic.io/)
* [GiantSwarm's swarm](https://github.com/giantswarm/cli)
* [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack)
* [rclone](http://rclone.org/)
* [nehm](https://github.com/bogem/nehm)
* [Pouch](https://github.com/alibaba/pouch)
[![Build Status](https://travis-ci.org/spf13/cobra.svg "Travis CI status")](https://travis-ci.org/spf13/cobra)
[![CircleCI status](https://circleci.com/gh/spf13/cobra.png?circle-token=:circle-token "CircleCI status")](https://circleci.com/gh/spf13/cobra)
[![GoDoc](https://godoc.org/github.com/spf13/cobra?status.svg)](https://godoc.org/github.com/spf13/cobra)
# Table of Contents
- [Overview](#overview)
- [Concepts](#concepts)
* [Commands](#commands)
* [Flags](#flags)
- [Installing](#installing)
- [Getting Started](#getting-started)
* [Using the Cobra Generator](#using-the-cobra-generator)
* [Using the Cobra Library](#using-the-cobra-library)
* [Working with Flags](#working-with-flags)
* [Positional and Custom Arguments](#positional-and-custom-arguments)
* [Example](#example)
* [Help Command](#help-command)
* [Usage Message](#usage-message)
* [PreRun and PostRun Hooks](#prerun-and-postrun-hooks)
* [Suggestions when "unknown command" happens](#suggestions-when-unknown-command-happens)
* [Generating documentation for your command](#generating-documentation-for-your-command)
* [Generating bash completions](#generating-bash-completions)
- [Contributing](#contributing)
- [License](#license)
# Overview
Cobra is a library providing a simple interface to create powerful modern CLI
interfaces similar to git & go tools.
Cobra is also an application that will generate your application scaffolding to rapidly
develop a Cobra-based application.
Cobra provides:
* Easy subcommand-based CLIs: `app server`, `app fetch`, etc.
* Fully POSIX-compliant flags (including short & long versions)
* Nested subcommands
* Global, local and cascading flags
* Easy generation of applications & commands with `cobra init appname` & `cobra add cmdname`
* Intelligent suggestions (`app srver`... did you mean `app server`?)
* Automatic help generation for commands and flags
* Automatic help flag recognition of `-h`, `--help`, etc.
* Automatically generated bash autocomplete for your application
* Automatically generated man pages for your application
* Command aliases so you can change things without breaking them
* The flexibility to define your own help, usage, etc.
* Optional tight integration with [viper](http://github.com/spf13/viper) for 12-factor apps
# Concepts
Cobra is built on a structure of commands, arguments & flags.
**Commands** represent actions, **Args** are things and **Flags** are modifiers for those actions.
The best applications will read like sentences when used. Users will know how
to use the application because they will natively understand how to use it.
The pattern to follow is
`APPNAME VERB NOUN --ADJECTIVE.`
or
`APPNAME COMMAND ARG --FLAG`
A few good real world examples may better illustrate this point.
In the following example, 'server' is a command, and 'port' is a flag:
hugo server --port=1313
In this command we are telling Git to clone the url bare.
git clone URL --bare
## Commands
Command is the central point of the application. Each interaction that
the application supports will be contained in a Command. A command can
have children commands and optionally run an action.
In the example above, 'server' is the command.
[More about cobra.Command](https://godoc.org/github.com/spf13/cobra#Command)
## Flags
A flag is a way to modify the behavior of a command. Cobra supports
fully POSIX-compliant flags as well as the Go [flag package](https://golang.org/pkg/flag/).
A Cobra command can define flags that persist through to children commands
and flags that are only available to that command.
In the example above, 'port' is the flag.
Flag functionality is provided by the [pflag
library](https://github.com/spf13/pflag), a fork of the flag standard library
which maintains the same interface while adding POSIX compliance.
# Installing
Using Cobra is easy. First, use `go get` to install the latest version
of the library. This command will install the `cobra` generator executable
along with the library and its dependencies:
go get -u github.com/spf13/cobra/cobra
Next, include Cobra in your application:
```go
import "github.com/spf13/cobra"
```
# Getting Started
While you are welcome to provide your own organization, typically a Cobra-based
application will follow the following organizational structure:
```
▾ appName/
▾ cmd/
add.go
your.go
commands.go
here.go
main.go
```
In a Cobra app, typically the main.go file is very bare. It serves one purpose: initializing Cobra.
```go
package main
import (
"fmt"
"os"
"{pathToYourApp}/cmd"
)
func main() {
cmd.Execute()
}
```
## Using the Cobra Generator
Cobra provides its own program that will create your application and add any
commands you want. It's the easiest way to incorporate Cobra into your application.
[Here](https://github.com/spf13/cobra/blob/master/cobra/README.md) you can find more information about it.
## Using the Cobra Library
To manually implement Cobra you need to create a bare main.go file and a rootCmd file.
You will optionally provide additional commands as you see fit.
### Create rootCmd
Cobra doesn't require any special constructors. Simply create your commands.
Ideally you place this in app/cmd/root.go:
```go
var rootCmd = &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 http://hugo.spf13.com`,
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
```
You will additionally define flags and handle configuration in your init() function.
For example cmd/root.go:
```go
import (
"fmt"
"os"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
rootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")
rootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
viper.BindPFlag("projectbase", rootCmd.PersistentFlags().Lookup("projectbase"))
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
viper.SetDefault("au
没有合适的资源?快使用搜索试试~ 我知道了~
cloud-controller-manager是阿里云Alibaba Cloud的外部Kubernetes云控制器管理器实现
共2000个文件
go:1623个
md:318个
sh:19个
0 下载量 91 浏览量
2024-03-01
10:07:28
上传
评论
收藏 52.86MB ZIP 举报
温馨提示
cloud-controller-manager是阿里云(Alibaba Cloud)的外部 Kubernetes 云控制器管理器实现。通过运行,您可以利用阿里云上的许多云服务构建 kubernetes 集群。
资源推荐
资源详情
资源评论
收起资源包目录
cloud-controller-manager是阿里云Alibaba Cloud的外部Kubernetes云控制器管理器实现 (2000个子文件)
thetest.pb.go 1.04MB
thetest.pb.go 988KB
thetest.pb.go 764KB
thetest.pb.go 687KB
thetestpb_test.go 489KB
thetestpb_test.go 489KB
thetestpb_test.go 439KB
thetestpb_test.go 439KB
theproto3.pb.go 327KB
theproto3.pb.go 300KB
mapsproto2.pb.go 257KB
mapsproto2.pb.go 234KB
theproto3.pb.go 215KB
theproto3.pb.go 188KB
test.pb.go 178KB
mapsproto2.pb.go 162KB
data_codec.go 155KB
one.pb.go 151KB
test.pb.go 143KB
one.pb.go 143KB
types.pb.go 143KB
data_ffjson.go 142KB
mapsproto2.pb.go 140KB
one.pb.go 131KB
types.pb.go 128KB
one.pb.go 124KB
descriptor.pb.go 115KB
unrecognized.pb.go 114KB
generator.go 108KB
packed.pb.go 97KB
one.pb.go 96KB
descriptor.pb.go 93KB
generator.go 93KB
casttype.pb.go 92KB
one.pb.go 92KB
types.pb.go 90KB
casttype.pb.go 88KB
one.pb.go 84KB
one.pb.go 80KB
api_test.go 79KB
conformance.pb.go 76KB
types.pb.go 75KB
example.pb.go 74KB
conformance.pb.go 74KB
all_test.go 71KB
casttype.pb.go 71KB
table_marshal.go 70KB
casttype.pb.go 66KB
all_test.go 66KB
theproto3pb_test.go 65KB
theproto3pb_test.go 65KB
fuzz.pb.go 64KB
unrecognizedgroup.pb.go 62KB
theproto3pb_test.go 58KB
theproto3pb_test.go 58KB
format_test.go 56KB
castvalue.pb.go 54KB
typespb_test.go 53KB
typespb_test.go 53KB
requiredexample.pb.go 53KB
castvalue.pb.go 52KB
map.pb.go 51KB
descriptors.go 51KB
unrecognizedpb_test.go 50KB
map.pb.go 49KB
table_unmarshal.go 47KB
typespb_test.go 47KB
typespb_test.go 47KB
command_test.go 45KB
castvalue.pb.go 45KB
wrappers.pb.go 45KB
test_objects.pb.go 44KB
struct.pb.go 43KB
examplepb_test.go 42KB
castvalue.pb.go 42KB
group.pb.go 42KB
command.go 42KB
jsonpb_test.go 42KB
map.pb.go 41KB
testsuites.go 40KB
unmarshal.go 40KB
stdtypes.pb.go 40KB
map.pb.go 39KB
test.pb.go 39KB
cache_test.go 38KB
service_controller.go 38KB
unmarshalmerge.pb.go 38KB
license_gpl_3.go 36KB
marshalto.go 36KB
flag_test.go 36KB
client.go 36KB
glog.go 36KB
flag.go 35KB
app.go 35KB
jsonpb.go 35KB
license_agpl.go 35KB
httpcache_test.go 35KB
jsonpb_test.go 34KB
jsonpb.go 34KB
encode.go 34KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6723
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功