![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 (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 "{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 simple configuration
file which will help you eliminate providing a
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
人工智能-项目实践-区块链-基于Fabric超级账本为底层的企业资产管理、交易、防伪、溯源一体化的开源区块链解决方案 安装说明 基础环境&链码安装 采用docker部署 需要fabric 1.0版本的网络环境支持 安装链码 WEB API & WEB PAGE GO语言环境,打包即可运行 独立的vue前端项目,采用NGINX运行
资源推荐
资源详情
资源评论
收起资源包目录
人工智能-项目实践-区块链-基于Fabric超级账本为底层的企业资产管理、交易、防伪、溯源一体化的开源区块链解决方案.zip (2000个子文件)
sqlite3-binding.c 5MB
custom.css 4KB
report.css 937B
table_test.go 212KB
table.go 123KB
entity.go 98KB
generator.go 89KB
descriptor.pb.go 89KB
server.go 85KB
parser.go 72KB
type_specific_codegen.go 71KB
exectransaction_test.go 62KB
mountinfo_linux_test.go 61KB
channel_test.go 58KB
transport.go 58KB
chain_test.go 57KB
couchdb_test.go 56KB
container.go 54KB
handler.go 54KB
impl_test.go 50KB
impl_test.go 49KB
pkcs11.go 48KB
parse.go 48KB
ote.go 48KB
marshal_test.go 48KB
ROM64.go 48KB
dialects_builtin.go 47KB
lscc_test.go 45KB
xml.go 45KB
frame.go 44KB
server_test.go 44KB
validator_onevalidsignature_test.go 41KB
couchdb.go 41KB
flag.go 39KB
gossip_impl.go 39KB
discovery_test.go 38KB
mspimpl.go 37KB
marshal.go 35KB
shim_test.go 35KB
gossip_test.go 35KB
ciphers.go 34KB
encode.go 34KB
command.go 34KB
handler.go 33KB
archive.go 33KB
const.go 33KB
state_test.go 31KB
archive_test.go 30KB
token.go 30KB
chaincode_support.go 29KB
chaincode.go 29KB
endorser_test.go 29KB
chaincode_support_test.go 29KB
discovery_impl.go 29KB
viper.go 28KB
names-generator.go 28KB
flag.go 28KB
copy_unix_test.go 28KB
jsonpb.go 28KB
assertions.go 27KB
client.go 27KB
reader.go 27KB
file_test.go 27KB
block.go 27KB
lscc.go 27KB
trace.go 26KB
exec.go 26KB
db.go 25KB
properties.go 25KB
xml_test.go 25KB
async_producer.go 25KB
msp_test.go 25KB
AES.go 24KB
marbles_chaincode.go 24KB
validator.go 24KB
markdown.go 24KB
decode.go 24KB
gossip_service_test.go 24KB
lib.go 24KB
client.go 24KB
html.go 23KB
consensus.go 23KB
channel.go 23KB
kv_ledger_test.go 23KB
table.go 23KB
difflib.go 22KB
devmapper.go 22KB
text_parser.go 22KB
manager_test.go 22KB
comm_test.go 22KB
inline.go 21KB
state.go 21KB
consumer.go 21KB
mapstructure.go 21KB
text.go 21KB
terminal.go 21KB
gen.go 20KB
read_test.go 20KB
client_test.go 20KB
image.go 20KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
博士僧小星
- 粉丝: 1745
- 资源: 5850
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2022NOC软件创意编程赛项真题python小学高年级-决赛(有解析)
- mathml转换latex需要的xsl文件
- 2022NOC软件创意编程赛项真题图形化小学高年级-决赛赛(有解析)
- gbase驱动下载gbase-connector-java-8.3.81.53驱动下载
- 2022NOC软件创意编程赛项真题图形化小学低年级-决赛赛(有解析)
- InsightFace从青铜到王者,超大规模人脸识别的优雅解法
- python后端开发spider框架详解
- 基于 STM32 与 ESP8266 的智能家居系统源码.zip
- 毕业设计:基于SSM的mysql-个性化点餐配送系统(源码 + 数据库 + 说明文档)
- 基于matlab的鱼苗计数识别(GUI界面).zip代码57
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功