# Kingpin - A Go (golang) command line and flag parser
[![](https://godoc.org/github.com/alecthomas/kingpin?status.svg)](http://godoc.org/github.com/alecthomas/kingpin) [![Build Status](https://travis-ci.org/alecthomas/kingpin.svg?branch=master)](https://travis-ci.org/alecthomas/kingpin) [![Gitter chat](https://badges.gitter.im/alecthomas.png)](https://gitter.im/alecthomas/Lobby)
<!-- MarkdownTOC -->
- [Overview](#overview)
- [Features](#features)
- [User-visible changes between v1 and v2](#user-visible-changes-between-v1-and-v2)
- [Flags can be used at any point after their definition.](#flags-can-be-used-at-any-point-after-their-definition)
- [Short flags can be combined with their parameters](#short-flags-can-be-combined-with-their-parameters)
- [API changes between v1 and v2](#api-changes-between-v1-and-v2)
- [Versions](#versions)
- [V2 is the current stable version](#v2-is-the-current-stable-version)
- [V1 is the OLD stable version](#v1-is-the-old-stable-version)
- [Change History](#change-history)
- [Examples](#examples)
- [Simple Example](#simple-example)
- [Complex Example](#complex-example)
- [Reference Documentation](#reference-documentation)
- [Displaying errors and usage information](#displaying-errors-and-usage-information)
- [Sub-commands](#sub-commands)
- [Custom Parsers](#custom-parsers)
- [Repeatable flags](#repeatable-flags)
- [Boolean Values](#boolean-values)
- [Default Values](#default-values)
- [Place-holders in Help](#place-holders-in-help)
- [Consuming all remaining arguments](#consuming-all-remaining-arguments)
- [Bash/ZSH Shell Completion](#bashzsh-shell-completion)
- [Supporting -h for help](#supporting--h-for-help)
- [Custom help](#custom-help)
<!-- /MarkdownTOC -->
## Overview
Kingpin is a [fluent-style](http://en.wikipedia.org/wiki/Fluent_interface),
type-safe command-line parser. It supports flags, nested commands, and
positional arguments.
Install it with:
$ go get gopkg.in/alecthomas/kingpin.v2
It looks like this:
```go
var (
verbose = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()
name = kingpin.Arg("name", "Name of user.").Required().String()
)
func main() {
kingpin.Parse()
fmt.Printf("%v, %s\n", *verbose, *name)
}
```
More [examples](https://github.com/alecthomas/kingpin/tree/master/_examples) are available.
Second to parsing, providing the user with useful help is probably the most
important thing a command-line parser does. Kingpin tries to provide detailed
contextual help if `--help` is encountered at any point in the command line
(excluding after `--`).
## Features
- Help output that isn't as ugly as sin.
- Fully [customisable help](#custom-help), via Go templates.
- Parsed, type-safe flags (`kingpin.Flag("f", "help").Int()`)
- Parsed, type-safe positional arguments (`kingpin.Arg("a", "help").Int()`).
- Parsed, type-safe, arbitrarily deep commands (`kingpin.Command("c", "help")`).
- Support for required flags and required positional arguments (`kingpin.Flag("f", "").Required().Int()`).
- Support for arbitrarily nested default commands (`command.Default()`).
- Callbacks per command, flag and argument (`kingpin.Command("c", "").Action(myAction)`).
- POSIX-style short flag combining (`-a -b` -> `-ab`).
- Short-flag+parameter combining (`-a parm` -> `-aparm`).
- Read command-line from files (`@<file>`).
- Automatically generate man pages (`--help-man`).
## User-visible changes between v1 and v2
### Flags can be used at any point after their definition.
Flags can be specified at any point after their definition, not just
*immediately after their associated command*. From the chat example below, the
following used to be required:
```
$ chat --server=chat.server.com:8080 post --image=~/Downloads/owls.jpg pics
```
But the following will now work:
```
$ chat post --server=chat.server.com:8080 --image=~/Downloads/owls.jpg pics
```
### Short flags can be combined with their parameters
Previously, if a short flag was used, any argument to that flag would have to
be separated by a space. That is no longer the case.
## API changes between v1 and v2
- `ParseWithFileExpansion()` is gone. The new parser directly supports expanding `@<file>`.
- Added `FatalUsage()` and `FatalUsageContext()` for displaying an error + usage and terminating.
- `Dispatch()` renamed to `Action()`.
- Added `ParseContext()` for parsing a command line into its intermediate context form without executing.
- Added `Terminate()` function to override the termination function.
- Added `UsageForContextWithTemplate()` for printing usage via a custom template.
- Added `UsageTemplate()` for overriding the default template to use. Two templates are included:
1. `DefaultUsageTemplate` - default template.
2. `CompactUsageTemplate` - compact command template for larger applications.
## Versions
Kingpin uses [gopkg.in](https://gopkg.in/alecthomas/kingpin) for versioning.
The current stable version is [gopkg.in/alecthomas/kingpin.v2](https://gopkg.in/alecthomas/kingpin.v2). The previous version, [gopkg.in/alecthomas/kingpin.v1](https://gopkg.in/alecthomas/kingpin.v1), is deprecated and in maintenance mode.
### [V2](https://gopkg.in/alecthomas/kingpin.v2) is the current stable version
Installation:
```sh
$ go get gopkg.in/alecthomas/kingpin.v2
```
### [V1](https://gopkg.in/alecthomas/kingpin.v1) is the OLD stable version
Installation:
```sh
$ go get gopkg.in/alecthomas/kingpin.v1
```
## Change History
- *2015-09-19* -- Stable v2.1.0 release.
- Added `command.Default()` to specify a default command to use if no other
command matches. This allows for convenient user shortcuts.
- Exposed `HelpFlag` and `VersionFlag` for further customisation.
- `Action()` and `PreAction()` added and both now support an arbitrary
number of callbacks.
- `kingpin.SeparateOptionalFlagsUsageTemplate`.
- `--help-long` and `--help-man` (hidden by default) flags.
- Flags are "interspersed" by default, but can be disabled with `app.Interspersed(false)`.
- Added flags for all simple builtin types (int8, uint16, etc.) and slice variants.
- Use `app.Writer(os.Writer)` to specify the default writer for all output functions.
- Dropped `os.Writer` prefix from all printf-like functions.
- *2015-05-22* -- Stable v2.0.0 release.
- Initial stable release of v2.0.0.
- Fully supports interspersed flags, commands and arguments.
- Flags can be present at any point after their logical definition.
- Application.Parse() terminates if commands are present and a command is not parsed.
- Dispatch() -> Action().
- Actions are dispatched after all values are populated.
- Override termination function (defaults to os.Exit).
- Override output stream (defaults to os.Stderr).
- Templatised usage help, with default and compact templates.
- Make error/usage functions more consistent.
- Support argument expansion from files by default (with @<file>).
- Fully public data model is available via .Model().
- Parser has been completely refactored.
- Parsing and execution has been split into distinct stages.
- Use `go generate` to generate repeated flags.
- Support combined short-flag+argument: -fARG.
- *2015-01-23* -- Stable v1.3.4 release.
- Support "--" for separating flags from positional arguments.
- Support loading flags from files (ParseWithFileExpansion()). Use @FILE as an argument.
- Add post-app and post-cmd validation hooks. This allows arbitrary validation to be added.
- A bunch of improvements to help usage and formatting.
- Support arbitrarily nested sub-commands.
- *2014-07-08* -- Stable v1.2.0 release.
- Pass any value through to `Strings()` when final argument.
Allows for values that look like flags to be processed.
- Allow `--help` to be used with commands.
- Support `Hidden()` flags.
- Parser for [units.Base2Bytes](https://github.com/alecthomas/units)
没有合适的资源?快使用搜索试试~ 我知道了~
Prometheus 导出器,用于 *NIX 内核公开的硬件和操作系统指标,用 Go 编写,带有可插拔指标收集器
共1397个文件
go:1051个
build:49个
md:41个
需积分: 5 0 下载量 92 浏览量
2024-03-04
18:33:16
上传
评论
收藏 3.56MB ZIP 举报
温馨提示
Prometheus 导出器,用于 *NIX 内核公开的硬件和操作系统指标,用 Go 编写,带有可插拔指标收集器。建议 Windows 用户使用 WMI 导出器。
资源推荐
资源详情
资源评论
收起资源包目录
Prometheus 导出器,用于 *NIX 内核公开的硬件和操作系统指标,用 Go 编写,带有可插拔指标收集器 (1397个子文件)
abdstats 922B
arcstats 4KB
arp 529B
buddyinfo 300B
BUILD 3KB
BUILD 3KB
BUILD 3KB
BUILD 2KB
BUILD 2KB
BUILD 2KB
BUILD 2KB
BUILD 2KB
BUILD 2KB
BUILD 2KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 1KB
BUILD 939B
BUILD 893B
BUILD 893B
BUILD 876B
BUILD 857B
BUILD 805B
BUILD 795B
BUILD 783B
BUILD 761B
BUILD 756B
BUILD 750B
BUILD 746B
BUILD 676B
BUILD 656B
BUILD 639B
BUILD 632B
BUILD 610B
BUILD 606B
BUILD 600B
BUILD 581B
BUILD 577B
BUILD 570B
BUILD 559B
BUILD 519B
BUILD 513B
BUILD 499B
BUILD 494B
BUILD 481B
iostat_darwin.c 4KB
devstat_freebsd.c 3KB
kvm_bsd.c 1KB
gccgo_c.c 1KB
Makefile.common 4KB
CONTRIBUTORS 169B
COPYING 1KB
COPYING 1KB
dbuf_stats 3KB
dev 1KB
diskstats 3KB
dmu_tx 520B
dnodestats 1KB
Dockerfile 1KB
Dockerfile 241B
.dockerignore 40B
drbd 264B
entropy_avail 5B
file-nr 15B
fm 241B
.gitignore 406B
generated.pb.go 1.23MB
tables.go 338KB
tables.go 263KB
types.go 262KB
generated.pb.go 193KB
types.go 175KB
types_swagger_doc_generated.go 175KB
zz_generated.deepcopy.go 157KB
zz_generated.deepcopy.go 156KB
zerrors_linux_ppc64le.go 120KB
zerrors_linux_ppc64.go 120KB
zerrors_linux_s390x.go 120KB
zerrors_linux_mips64le.go 118KB
zerrors_linux_mipsle.go 118KB
zerrors_linux_mips64.go 118KB
zerrors_linux_mips.go 118KB
zerrors_linux_arm.go 118KB
zerrors_linux_amd64.go 118KB
zerrors_linux_386.go 118KB
zerrors_linux_arm64.go 117KB
zerrors_linux_riscv64.go 117KB
tables.go 109KB
zsyscall_windows.go 89KB
server.go 86KB
zerrors_linux_sparc64.go 86KB
scannerc.go 75KB
共 1397 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6656
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功