![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)
[![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() {
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.
[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
},
}
```
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("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache")
没有合适的资源?快使用搜索试试~ 我知道了~
facility-location:Go中解决了设施位置问题
共961个文件
go:741个
txt:62个
s:47个
需积分: 3 0 下载量 158 浏览量
2021-05-09
13:48:27
上传
评论
收藏 5.92MB ZIP 举报
温馨提示
设施位置问题 在Go(Golang)中解决了单一分配的设施位置问题。 要求 转到-https: 用法 go build ./facility-location [input-file] [flags] Flags: -a, --async int number of async tasks (default 4) -b, --best float value of the best solution -h, --help help for hub-spoke-go -v, --verbose active verbose mode 例子: ./facility-location ./facility-location data/ap10_2.txt -b 90963539.4763 -v -a 4 启发式应用 GRASP(贪婪随机自
资源详情
资源评论
资源推荐
收起资源包目录
facility-location:Go中解决了设施位置问题 (961个子文件)
AUTHORS 174B
AUTHORS 173B
libotr_test_helper.c 6KB
sshd_test_pw.c 4KB
gccgo_c.c 1KB
go12.c 497B
codereview.cfg 21B
codereview.cfg 21B
CONTRIBUTORS 171B
CONTRIBUTORS 170B
keccakKats.json.deflate 509KB
Dockerfile 2KB
.gitattributes 345B
.gitattributes 345B
.gitignore 467B
.gitignore 298B
.gitignore 263B
.gitignore 252B
.gitignore 84B
.gitignore 84B
.gitignore 41B
.gitignore 16B
.gitignore 9B
.gitignore 7B
chacha20poly1305_vectors_test.go 295KB
blake2b_test.go 105KB
zerrors_linux_ppc64le.go 101KB
zerrors_linux_ppc64.go 101KB
zerrors_linux_s390x.go 101KB
zerrors_linux_mips64le.go 98KB
zerrors_linux_mipsle.go 98KB
zerrors_linux_mips64.go 98KB
zerrors_linux_mips.go 98KB
zerrors_linux_arm.go 98KB
zerrors_linux_amd64.go 98KB
zerrors_linux_386.go 98KB
zerrors_linux_arm64.go 97KB
blake2s_test.go 97KB
const.go 94KB
zsyscall_windows.go 88KB
zerrors_linux_sparc64.go 84KB
type_specific_codegen_test.go 72KB
zerrors_darwin_arm64.go 71KB
zerrors_darwin_amd64.go 71KB
zerrors_darwin_386.go 71KB
zerrors_darwin_arm.go 71KB
type_specific_codegen.go 69KB
zerrors_netbsd_386.go 69KB
zerrors_netbsd_amd64.go 68KB
zerrors_netbsd_arm.go 68KB
zerrors_freebsd_arm.go 67KB
zerrors_freebsd_amd64.go 67KB
zerrors_freebsd_386.go 67KB
zerrors_openbsd_arm.go 64KB
zerrors_openbsd_386.go 64KB
zerrors_openbsd_amd64.go 64KB
zerrors_dragonfly_amd64.go 62KB
format_test.go 56KB
zerrors_solaris_amd64.go 55KB
zsyscall_linux_ppc64le.go 53KB
zsyscall_linux_ppc64.go 53KB
zsyscall_linux_amd64.go 52KB
zsyscall_linux_mipsle.go 52KB
zsyscall_linux_mips.go 52KB
zsyscall_linux_mips64le.go 51KB
zsyscall_linux_mips64.go 51KB
zsyscall_linux_arm.go 50KB
zsyscall_linux_arm64.go 49KB
ocsp_test.go 48KB
zsyscall_linux_386.go 47KB
zsyscall_linux_s390x.go 47KB
zsyscall_freebsd_arm.go 46KB
zsyscall_freebsd_386.go 46KB
zsyscall_freebsd_amd64.go 45KB
zsyscall_solaris_amd64.go 44KB
syscall_windows.go 44KB
zsyscall_linux_sparc64.go 44KB
syscall_linux.go 42KB
read_test.go 42KB
edwards25519.go 41KB
assertions_test.go 40KB
command.go 39KB
zsyscall_darwin_386.go 38KB
zsyscall_darwin_arm.go 38KB
zsyscall_darwin_arm64.go 37KB
zsyscall_darwin_amd64.go 37KB
acme_test.go 37KB
mock_test.go 36KB
assertions.go 36KB
license_gpl_3.go 36KB
otr.go 35KB
cobra_test.go 35KB
license_agpl.go 35KB
zsyscall_dragonfly_amd64.go 34KB
zsyscall_openbsd_arm.go 33KB
zsyscall_openbsd_386.go 33KB
zsyscall_openbsd_amd64.go 33KB
dump_test.go 33KB
flag.go 33KB
types_windows.go 32KB
共 961 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
杜佳加
- 粉丝: 42
- 资源: 4625
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- TI杯大奖赛本科组+研究生组论文+源代码+PCB基于蓝牙及GSM的智能防盗系统
- (精品)工业互联网(物联网)及其应用.pdf
- 上市公司治理质量综合指数/水平2003-2023年【结果+代码】
- Python基于深度强化学习的MEC计算卸载与资源分配源码(高分项目).zip
- PTAC-C++求n以内最大的k个素数以及它们的和.rar
- 古诗15-唐朝白居易的游悟真寺
- (自适应手机端)响应式陶瓷研磨盘设备pbootcms网站模板 抛光设备网站源码.zip
- 3G直放站监控系统南向接口协议技术规范
- (精品)泵站自动化监控系统培训课件.ppt
- 基于python弹幕情感分析的直播高光时刻判断模型设计与系统实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0