![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)
* [Docker](https://github.com/docker/docker)
* [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/)
* [Parse (CLI)](https://parse.com/)
* [GiantSwarm's swarm](https://github.com/giantswarm/cli)
* [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack)
[![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)
![cobra](https://cloud.githubusercontent.com/assets/173412/10911369/84832a8e-8212-11e5-9f82-cc96660a4794.gif)
# 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 create appname` & `cobra add cmdname`
* Intelligent suggestions (`app srver`... did you mean `app server`?)
* Automatic help generation for commands and flags
* Automatic detailed help for `app help [command]`
* 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 flexibilty to define your own help, usage, etc.
* Optional tight integration with [viper](http://github.com/spf13/viper) for 12-factor apps
Cobra has an exceptionally clean interface and simple design without needless
constructors or initialization methods.
Applications built with Cobra commands are designed to be as user-friendly as
possible. Flags can be placed before or after the command (as long as a
confusing space isn’t provided). Both short and long flags can be used. A
command need not even be fully typed. Help is automatically generated and
available for the application or for a specific command using either the help
command or the `--help` flag.
# 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.
A Command has the following structure:
```go
type Command struct {
Use string // The one-line usage message.
Short string // The short description shown in the 'help' output.
Long string // The long message shown in the 'help <this-command>' output.
Run func(cmd *Command, args []string) // Run runs the 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/ogier/pflag), a fork of the flag standard library
which maintains the same interface while adding POSIX compliance.
## Usage
Cobra works by creating a set of commands and then organizing them into a tree.
The tree defines the structure of the application.
Once each command is defined with its corresponding flags, then the
tree is assigned to the commander which is finally executed.
# Installing
Using Cobra is easy. First, use `go get` to install the latest version
of the library. This command will install the `cobra` generator executible
along with the library:
> go get -v 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, to initialize Cobra.
```go
package main
import (
"fmt"
"os"
"{pathToYourApp}/cmd"
)
func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
```
## 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.
In order to use the cobra command, compile it using the following command:
> go install github.com/spf13/cobra/cobra
This will create the cobra executable under your go path bin directory!
### cobra init
The `cobra init [yourApp]` command will create your initial application code
for you. It is a very powerful application that will populate your program with
the right structure so you can immediately enjoy all the benefits of Cobra. It
will also automatically apply the license you specify to your application.
Cobra init is pretty smart. You can provide it a full path, or simply a path
similar to what is expected in the import.
```
cobra init github.com/spf13/newAppName
```
### cobra add
Once an application is initialized Cobra can create additional commands for you.
Let's say you created an app and you wanted the following commands for it:
* app serve
* app config
* app config create
In your project directory (where your main.go file is) you would run the following:
```
cobra add serve
cobra add config
cobra add create -p 'configCmd'
```
Once you have run these three commands you would have an app structure that would look like:
```
▾ app/
▾ cmd/
serve.go
config.go
create.go
main.go
```
at this point you can run `go run main.go` and it would run your app. `go run
main.go serve`, `go run main.go config`, `go run main.go config create` along
with `go run main.go help serve`, etc would all work.
Obviously you haven't added your own code to these yet, the commands are ready
for you to give them their tasks. Have fun.
### Configuring the cobra generator
The cobra generator will be easier to use if you provide a simp
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Open Falcon企业级监控系统 v0.3.0.zip (1483个子文件)
AUTHORS 2KB
AUTHORS 2KB
rrd_update.c 51KB
rrd_rpncalc.c 31KB
rrd_parsetime.c 29KB
rrd_create.c 22KB
rrd_open.c 20KB
rrd_hw_update.c 19KB
rrd_hw.c 15KB
rrd_fetch.c 13KB
rrd_info.c 10KB
rrd_error.c 9KB
hash_32.c 4KB
rrd_hw_math.c 4KB
rrd_utils.c 4KB
rrd_format.c 4KB
rrd_diff.c 3KB
gccgo_c.c 1014B
rrdfunc.c 927B
rrd_nan_inf.c 638B
rrd_version.c 604B
COMPATIBLE 108B
supervisord.conf 4KB
control 2KB
control 2KB
control 2KB
control 2KB
control 2KB
control 2KB
control 2KB
control 2KB
control 2KB
COPYING 484B
bootstrap.min.css 88KB
font-awesome-ie7.css 40KB
font-awesome-ie7.min.css 37KB
font-awesome.css 27KB
style.css 23KB
font-awesome.min.css 22KB
assets.css 11KB
bootstrap-responsive.min.css 9KB
dashboard.css 5KB
odometer.css 4KB
g.css 3KB
debug 4KB
debug 4KB
debug 1KB
debug 1KB
Dockerfile 895B
.dockerignore 45B
OpenSans-Regular-webfont.eot 71KB
fontawesome-webfont.eot 37KB
http.recv.filter 199B
.gitignore 401B
.gitignore 369B
.gitignore 365B
.gitignore 362B
.gitignore 356B
.gitignore 352B
.gitignore 351B
.gitignore 318B
.gitignore 99B
.gitignore 84B
.gitignore 60B
.gitignore 41B
.gitignore 13B
.gitignore 12B
gitversion 8B
table.go 459KB
tables.go 338KB
zerrors_linux_ppc64le.go 84KB
zerrors_linux_ppc64.go 84KB
zerrors_linux_s390x.go 84KB
zerrors_linux_sparc64.go 84KB
zerrors_linux_mipsle.go 82KB
zerrors_linux_mips.go 82KB
zerrors_linux_mips64le.go 82KB
zerrors_linux_mips64.go 82KB
zerrors_linux_arm.go 82KB
zerrors_linux_amd64.go 82KB
zerrors_linux_386.go 82KB
zerrors_linux_arm64.go 81KB
scannerc.go 75KB
zerrors_freebsd_amd64.go 70KB
zerrors_freebsd_386.go 69KB
zerrors_freebsd_arm.go 69KB
zerrors_netbsd_386.go 68KB
zerrors_netbsd_amd64.go 68KB
zerrors_netbsd_arm.go 67KB
zerrors_openbsd_386.go 63KB
zerrors_openbsd_amd64.go 63KB
zerrors_darwin_arm64.go 63KB
zerrors_darwin_amd64.go 63KB
zerrors_darwin_386.go 63KB
zerrors_dragonfly_amd64.go 62KB
baked_in.go 55KB
zerrors_darwin_arm.go 53KB
zerrors_solaris_amd64.go 53KB
emitterc.go 44KB
zsyscall_linux_ppc64le.go 44KB
共 1483 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15
资源评论
芝麻粒儿
- 粉丝: 6w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功