cli
===
[![Build Status](https://travis-ci.org/urfave/cli.svg?branch=master)](https://travis-ci.org/urfave/cli)
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/rtgk5xufi932pb2v?svg=true)](https://ci.appveyor.com/project/urfave/cli)
[![GoDoc](https://godoc.org/github.com/urfave/cli?status.svg)](https://godoc.org/github.com/urfave/cli)
[![codebeat](https://codebeat.co/badges/0a8f30aa-f975-404b-b878-5fab3ae1cc5f)](https://codebeat.co/projects/github-com-urfave-cli)
[![Go Report Card](https://goreportcard.com/badge/urfave/cli)](https://goreportcard.com/report/urfave/cli)
[![top level coverage](https://gocover.io/_badge/github.com/urfave/cli?0 "top level coverage")](http://gocover.io/github.com/urfave/cli) /
[![altsrc coverage](https://gocover.io/_badge/github.com/urfave/cli/altsrc?0 "altsrc coverage")](http://gocover.io/github.com/urfave/cli/altsrc)
**Notice:** This is the library formerly known as
`github.com/codegangsta/cli` -- Github will automatically redirect requests
to this repository, but we recommend updating your references for clarity.
cli is a simple, fast, and fun package for building command line apps in Go. The
goal is to enable developers to write fast and distributable command line
applications in an expressive way.
<!-- toc -->
- [Overview](#overview)
- [Installation](#installation)
* [Supported platforms](#supported-platforms)
* [Using the `v2` branch](#using-the-v2-branch)
* [Pinning to the `v1` releases](#pinning-to-the-v1-releases)
- [Getting Started](#getting-started)
- [Examples](#examples)
* [Arguments](#arguments)
* [Flags](#flags)
+ [Placeholder Values](#placeholder-values)
+ [Alternate Names](#alternate-names)
+ [Ordering](#ordering)
+ [Values from the Environment](#values-from-the-environment)
+ [Values from alternate input sources (YAML, TOML, and others)](#values-from-alternate-input-sources-yaml-toml-and-others)
* [Subcommands](#subcommands)
* [Subcommands categories](#subcommands-categories)
* [Exit code](#exit-code)
* [Bash Completion](#bash-completion)
+ [Enabling](#enabling)
+ [Distribution](#distribution)
+ [Customization](#customization)
* [Generated Help Text](#generated-help-text)
+ [Customization](#customization-1)
* [Version Flag](#version-flag)
+ [Customization](#customization-2)
+ [Full API Example](#full-api-example)
- [Contribution Guidelines](#contribution-guidelines)
<!-- tocstop -->
## Overview
Command line apps are usually so tiny that there is absolutely no reason why
your code should *not* be self-documenting. Things like generating help text and
parsing command flags/options should not hinder productivity when writing a
command line app.
**This is where cli comes into play.** cli makes command line programming fun,
organized, and expressive!
## Installation
Make sure you have a working Go environment. Go version 1.2+ is supported. [See
the install instructions for Go](http://golang.org/doc/install.html).
To install cli, simply run:
```
$ go get github.com/urfave/cli
```
Make sure your `PATH` includes the `$GOPATH/bin` directory so your commands can
be easily used:
```
export PATH=$PATH:$GOPATH/bin
```
### Supported platforms
cli is tested against multiple versions of Go on Linux, and against the latest
released version of Go on OS X and Windows. For full details, see
[`./.travis.yml`](./.travis.yml) and [`./appveyor.yml`](./appveyor.yml).
### Using the `v2` branch
**Warning**: The `v2` branch is currently unreleased and considered unstable.
There is currently a long-lived branch named `v2` that is intended to land as
the new `master` branch once development there has settled down. The current
`master` branch (mirrored as `v1`) is being manually merged into `v2` on
an irregular human-based schedule, but generally if one wants to "upgrade" to
`v2` *now* and accept the volatility (read: "awesomeness") that comes along with
that, please use whatever version pinning of your preference, such as via
`gopkg.in`:
```
$ go get gopkg.in/urfave/cli.v2
```
``` go
...
import (
"gopkg.in/urfave/cli.v2" // imports as package "cli"
)
...
```
### Pinning to the `v1` releases
Similarly to the section above describing use of the `v2` branch, if one wants
to avoid any unexpected compatibility pains once `v2` becomes `master`, then
pinning to `v1` is an acceptable option, e.g.:
```
$ go get gopkg.in/urfave/cli.v1
```
``` go
...
import (
"gopkg.in/urfave/cli.v1" // imports as package "cli"
)
...
```
This will pull the latest tagged `v1` release (e.g. `v1.18.1` at the time of writing).
## Getting Started
One of the philosophies behind cli is that an API should be playful and full of
discovery. So a cli app can be as little as one line of code in `main()`.
<!-- {
"args": ["--help"],
"output": "A new cli application"
} -->
``` go
package main
import (
"os"
"github.com/urfave/cli"
)
func main() {
cli.NewApp().Run(os.Args)
}
```
This app will run and show help text, but is not very useful. Let's give an
action to execute and some help documentation:
<!-- {
"output": "boom! I say!"
} -->
``` go
package main
import (
"fmt"
"os"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "boom"
app.Usage = "make an explosive entrance"
app.Action = func(c *cli.Context) error {
fmt.Println("boom! I say!")
return nil
}
app.Run(os.Args)
}
```
Running this already gives you a ton of functionality, plus support for things
like subcommands and flags, which are covered below.
## Examples
Being a programmer can be a lonely job. Thankfully by the power of automation
that is not the case! Let's create a greeter app to fend off our demons of
loneliness!
Start by creating a directory named `greet`, and within it, add a file,
`greet.go` with the following code in it:
<!-- {
"output": "Hello friend!"
} -->
``` go
package main
import (
"fmt"
"os"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "greet"
app.Usage = "fight the loneliness!"
app.Action = func(c *cli.Context) error {
fmt.Println("Hello friend!")
return nil
}
app.Run(os.Args)
}
```
Install our command to the `$GOPATH/bin` directory:
```
$ go install
```
Finally run our new command:
```
$ greet
Hello friend!
```
cli also generates neat help text:
```
$ greet help
NAME:
greet - fight the loneliness!
USAGE:
greet [global options] command [command options] [arguments...]
VERSION:
0.0.0
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS
--version Shows version information
```
### Arguments
You can lookup arguments by calling the `Args` function on `cli.Context`, e.g.:
<!-- {
"output": "Hello \""
} -->
``` go
package main
import (
"fmt"
"os"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Action = func(c *cli.Context) error {
fmt.Printf("Hello %q", c.Args().Get(0))
return nil
}
app.Run(os.Args)
}
```
### Flags
Setting and querying flags is simple.
<!-- {
"output": "Hello Nefertiti"
} -->
``` go
package main
import (
"fmt"
"os"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag {
cli.StringFlag{
Name: "lang",
Value: "english",
Usage: "language for the greeting",
},
}
app.Action = func(c *cli.Context) error {
name := "Nefertiti"
if c.NArg() > 0 {
name = c.Args().Get(0)
}
if c.String("lang") == "spanish" {
fmt.Println("Hola", name)
} else {
fmt.Println("Hello", name)
}
return nil
}
app.Run(os.Args)
}
```
You can also set a destination variable for a flag, to which the content will be
scanned.
<!-- {
"output": "Hello someone"
} -->
``` go
package main
import (
"os"
"fmt"
"github.com/urfave/cli"
)
func main() {
var language string
app := cli.NewApp()
app.Flags = []cli.Flag {
cli.StringFlag{
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
A-Tune是一款基于AI的操作系统性能调优引擎 (1466个子文件)
arm64_config_for_basic_test_suite 141KB
atune-adm 778B
AUTHOR 20B
AUTHORS 753B
AUTHORS 173B
AUTHORS 173B
AUTHORS 173B
AUTHORS 173B
AUTHORS 12B
sqlite3-binding.c 7.54MB
sqlite3_opt_unlock_notify.c 2KB
gccgo_c.c 1KB
sysbench_config.cfg 138B
CHANGELOG 0B
engine.cnf 3KB
atuned.cnf 3KB
ui.cnf 2KB
my.cnf 738B
my.cnf 417B
modify.conf 5KB
basic-test-suite.conf 3KB
mysql-2p-sysbench.conf 3KB
http-long-connection.conf 2KB
https-short-connection.conf 2KB
human-genome.conf 2KB
ceph-vdbench.conf 2KB
hdfs-dfsio.conf 2KB
postgresql-2p-sysbench.conf 2KB
spark-common.conf 2KB
4p-tpcc-c3.conf 2KB
4p-tpcc-c3.conf 2KB
redis-benchmark.conf 2KB
4p-tpcc-c3.conf 2KB
2p-tpcc-c3.conf 2KB
2p-tpcc-c3.conf 1KB
http-short-connection.conf 1KB
https-long-connection.conf 1KB
tomcat-common.conf 1KB
RSAPublic-chauffeur.conf 1KB
MD5-chauffeur.conf 1KB
AES-chauffeur.conf 1KB
olc.conf 1KB
unixbench.conf 1KB
dubbo-benchmark.conf 1KB
2p-tpcc-c3.conf 1KB
2p-sysbench.conf 1KB
wordcount.conf 1KB
tersort.conf 1KB
fio.conf 1KB
host.conf 1KB
example.conf 1KB
spirent-pingpo.conf 1KB
network-intensive.conf 1KB
compute-intensive.conf 1KB
kmeans.conf 1KB
speccpu2006.conf 1KB
sql1.conf 1KB
io-intensive.conf 1KB
throughput-performance.conf 1KB
vdbench-hdd.conf 1KB
dfsio-ssd.conf 1KB
lmbench.conf 1024B
sql3.conf 1022B
sql4.conf 1022B
robox.conf 1005B
bayesian.conf 1000B
sql10.conf 997B
sql5.conf 996B
sql8.conf 996B
sql6.conf 996B
sql9.conf 996B
sql7.conf 995B
sql2.conf 995B
specjbb2015.conf 992B
dfsio-hdd.conf 991B
memory-intensive.conf 989B
http-connection.conf 981B
vdbench-ssd.conf 975B
stream.conf 966B
default.conf 963B
2p-sysbench-hdd.conf 948B
2p-sysbench-ssd.conf 941B
2p-sysbench-hdd.conf 938B
2p-sysbench-ssd.conf 931B
netperf.conf 907B
example.conf 659B
config 132B
CONTRIBUTORS 170B
CONTRIBUTORS 170B
CONTRIBUTORS 170B
CONTRIBUTORS 170B
CONTRIBUTORS 90B
storage-ceph-vdbench-hdd-20190105-104644.csv 7.16MB
web-nginx-https-short-connection-20190107-101949.csv 5.22MB
database-mysql-2p-sysbench-hdd-20190103-122812.csv 4.15MB
web-nginx-http-long-connection-20200701-143112.csv 3.99MB
storage-ceph-vdbench-hdd-20190109-073016.csv 3.92MB
database-mysql-2p-sysbench-hdd-20190727-084916.csv 3.79MB
database-mariadb-2p-tpcc-c3-20200526-140354.csv 3.5MB
default-default-20200722-231840.csv 3.26MB
共 1466 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6713
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功