mirror of https://github.com/ethereum/go-ethereum
Merge pull request #2677 from bas-vk/cli
cmd/geth: codegansta/cli package renamed to urfave/clipull/2681/head
commit
63d1d145e2
14
Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/bash_autocomplete
generated
vendored
14
Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/bash_autocomplete
generated
vendored
@ -1,14 +0,0 @@ |
||||
#! /bin/bash |
||||
|
||||
: ${PROG:=$(basename ${BASH_SOURCE})} |
||||
|
||||
_cli_bash_autocomplete() { |
||||
local cur opts base |
||||
COMPREPLY=() |
||||
cur="${COMP_WORDS[COMP_CWORD]}" |
||||
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ) |
||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) |
||||
return 0 |
||||
} |
||||
|
||||
complete -F _cli_bash_autocomplete $PROG |
@ -1,5 +0,0 @@ |
||||
autoload -U compinit && compinit |
||||
autoload -U bashcompinit && bashcompinit |
||||
|
||||
script_dir=$(dirname $0) |
||||
source ${script_dir}/bash_autocomplete |
@ -1,7 +0,0 @@ |
||||
language: go |
||||
go: |
||||
- 1.5 |
||||
- tip |
||||
matrix: |
||||
allow_failures: |
||||
- go: tip |
@ -1,19 +0,0 @@ |
||||
Copyright (c) 2015 Olivier Poitrey <rs@dailymotion.com> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
copies of the Software, and to permit persons to whom the Software is furnished |
||||
to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in all |
||||
copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
@ -1,134 +0,0 @@ |
||||
# XHandler |
||||
|
||||
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/xhandler) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/xhandler/master/LICENSE) [![Build Status](https://travis-ci.org/rs/xhandler.svg?branch=master)](https://travis-ci.org/rs/xhandler) [![Coverage](http://gocover.io/_badge/github.com/rs/xhandler)](http://gocover.io/github.com/rs/xhandler) |
||||
|
||||
XHandler is a bridge between [net/context](https://godoc.org/golang.org/x/net/context) and `http.Handler`. |
||||
|
||||
It lets you enforce `net/context` in your handlers without sacrificing compatibility with existing `http.Handlers` nor imposing a specific router. |
||||
|
||||
Thanks to `net/context` deadline management, `xhandler` is able to enforce a per request deadline and will cancel the context when the client closes the connection unexpectedly. |
||||
|
||||
You may create your own `net/context` aware handler pretty much the same way as you would do with http.Handler. |
||||
|
||||
Read more about xhandler on [Dailymotion engineering blog](http://engineering.dailymotion.com/our-way-to-go/). |
||||
|
||||
## Installing |
||||
|
||||
go get -u github.com/rs/xhandler |
||||
|
||||
## Usage |
||||
|
||||
```go |
||||
package main |
||||
|
||||
import ( |
||||
"log" |
||||
"net/http" |
||||
"time" |
||||
|
||||
"github.com/rs/cors" |
||||
"github.com/rs/xhandler" |
||||
"golang.org/x/net/context" |
||||
) |
||||
|
||||
type myMiddleware struct { |
||||
next xhandler.HandlerC |
||||
} |
||||
|
||||
func (h myMiddleware) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
ctx = context.WithValue(ctx, "test", "World") |
||||
h.next.ServeHTTPC(ctx, w, r) |
||||
} |
||||
|
||||
func main() { |
||||
c := xhandler.Chain{} |
||||
|
||||
// Add close notifier handler so context is cancelled when the client closes |
||||
// the connection |
||||
c.UseC(xhandler.CloseHandler) |
||||
|
||||
// Add timeout handler |
||||
c.UseC(xhandler.TimeoutHandler(2 * time.Second)) |
||||
|
||||
// Middleware putting something in the context |
||||
c.UseC(func(next xhandler.HandlerC) xhandler.HandlerC { |
||||
return myMiddleware{next: next} |
||||
}) |
||||
|
||||
// Mix it with a non-context-aware middleware handler |
||||
c.Use(cors.Default().Handler) |
||||
|
||||
// Final handler (using handlerFuncC), reading from the context |
||||
xh := xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
value := ctx.Value("test").(string) |
||||
w.Write([]byte("Hello " + value)) |
||||
}) |
||||
|
||||
// Bridge context aware handlers with http.Handler using xhandler.Handle() |
||||
http.Handle("/test", c.Handler(xh)) |
||||
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
||||
``` |
||||
|
||||
### Using xmux |
||||
|
||||
Xhandler comes with an optional context aware [muxer](https://github.com/rs/xmux) forked from [httprouter](https://github.com/julienschmidt/httprouter): |
||||
|
||||
```go |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"log" |
||||
"net/http" |
||||
"time" |
||||
|
||||
"github.com/rs/xhandler" |
||||
"github.com/rs/xmux" |
||||
"golang.org/x/net/context" |
||||
) |
||||
|
||||
func main() { |
||||
c := xhandler.Chain{} |
||||
|
||||
// Append a context-aware middleware handler |
||||
c.UseC(xhandler.CloseHandler) |
||||
|
||||
// Another context-aware middleware handler |
||||
c.UseC(xhandler.TimeoutHandler(2 * time.Second)) |
||||
|
||||
mux := xmux.New() |
||||
|
||||
// Use c.Handler to terminate the chain with your final handler |
||||
mux.GET("/welcome/:name", xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { |
||||
fmt.Fprintf(w, "Welcome %s!", xmux.Params(ctx).Get("name")) |
||||
})) |
||||
|
||||
if err := http.ListenAndServe(":8080", c.Handler(mux)); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
||||
``` |
||||
|
||||
See [xmux](https://github.com/rs/xmux) for more examples. |
||||
|
||||
## Context Aware Middleware |
||||
|
||||
Here is a list of `net/context` aware middleware handlers implementing `xhandler.HandlerC` interface. |
||||
|
||||
Feel free to put up a PR linking your middleware if you have built one: |
||||
|
||||
| Middleware | Author | Description | |
||||
| ---------- | ------ | ----------- | |
||||
| [xmux](https://github.com/rs/xmux) | [Olivier Poitrey](https://github.com/rs) | HTTP request muxer | |
||||
| [xlog](https://github.com/rs/xlog) | [Olivier Poitrey](https://github.com/rs) | HTTP handler logger | |
||||
| [xstats](https://github.com/rs/xstats) | [Olivier Poitrey](https://github.com/rs) | A generic client for service instrumentation | |
||||
| [xaccess](https://github.com/rs/xaccess) | [Olivier Poitrey](https://github.com/rs) | HTTP handler access logger with [xlog](https://github.com/rs/xlog) and [xstats](https://github.com/rs/xstats) | |
||||
| [cors](https://github.com/rs/cors) | [Olivier Poitrey](https://github.com/rs) | [Cross Origin Resource Sharing](http://www.w3.org/TR/cors/) (CORS) support | |
||||
|
||||
## Licenses |
||||
|
||||
All source code is licensed under the [MIT License](https://raw.github.com/rs/xhandler/master/LICENSE). |
@ -1,93 +0,0 @@ |
||||
package xhandler |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"golang.org/x/net/context" |
||||
) |
||||
|
||||
// Chain is an helper to chain middleware handlers together for an easier
|
||||
// management.
|
||||
type Chain []func(next HandlerC) HandlerC |
||||
|
||||
// UseC appends a context-aware handler to the middleware chain.
|
||||
func (c *Chain) UseC(f func(next HandlerC) HandlerC) { |
||||
*c = append(*c, f) |
||||
} |
||||
|
||||
// Use appends a standard http.Handler to the middleware chain without
|
||||
// lossing track of the context when inserted between two context aware handlers.
|
||||
//
|
||||
// Caveat: the f function will be called on each request so you are better to put
|
||||
// any initialization sequence outside of this function.
|
||||
func (c *Chain) Use(f func(next http.Handler) http.Handler) { |
||||
xf := func(next HandlerC) HandlerC { |
||||
return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
n := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
next.ServeHTTPC(ctx, w, r) |
||||
}) |
||||
f(n).ServeHTTP(w, r) |
||||
}) |
||||
} |
||||
*c = append(*c, xf) |
||||
} |
||||
|
||||
// Handler wraps the provided final handler with all the middleware appended to
|
||||
// the chain and return a new standard http.Handler instance.
|
||||
// The context.Background() context is injected automatically.
|
||||
func (c Chain) Handler(xh HandlerC) http.Handler { |
||||
ctx := context.Background() |
||||
return c.HandlerCtx(ctx, xh) |
||||
} |
||||
|
||||
// HandlerFC is an helper to provide a function (HandlerFuncC) to Handler().
|
||||
//
|
||||
// HandlerFC is equivalent to:
|
||||
// c.Handler(xhandler.HandlerFuncC(xhc))
|
||||
func (c Chain) HandlerFC(xhf HandlerFuncC) http.Handler { |
||||
ctx := context.Background() |
||||
return c.HandlerCtx(ctx, HandlerFuncC(xhf)) |
||||
} |
||||
|
||||
// HandlerH is an helper to provide a standard http handler (http.HandlerFunc)
|
||||
// to Handler(). Your final handler won't have access the context though.
|
||||
func (c Chain) HandlerH(h http.Handler) http.Handler { |
||||
ctx := context.Background() |
||||
return c.HandlerCtx(ctx, HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
h.ServeHTTP(w, r) |
||||
})) |
||||
} |
||||
|
||||
// HandlerF is an helper to provide a standard http handler function
|
||||
// (http.HandlerFunc) to Handler(). Your final handler won't have access
|
||||
// the context though.
|
||||
func (c Chain) HandlerF(hf http.HandlerFunc) http.Handler { |
||||
ctx := context.Background() |
||||
return c.HandlerCtx(ctx, HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
hf(w, r) |
||||
})) |
||||
} |
||||
|
||||
// HandlerCtx wraps the provided final handler with all the middleware appended to
|
||||
// the chain and return a new standard http.Handler instance.
|
||||
func (c Chain) HandlerCtx(ctx context.Context, xh HandlerC) http.Handler { |
||||
return New(ctx, c.HandlerC(xh)) |
||||
} |
||||
|
||||
// HandlerC wraps the provided final handler with all the middleware appended to
|
||||
// the chain and returns a HandlerC instance.
|
||||
func (c Chain) HandlerC(xh HandlerC) HandlerC { |
||||
for i := len(c) - 1; i >= 0; i-- { |
||||
xh = c[i](xh) |
||||
} |
||||
return xh |
||||
} |
||||
|
||||
// HandlerCF wraps the provided final handler func with all the middleware appended to
|
||||
// the chain and returns a HandlerC instance.
|
||||
//
|
||||
// HandlerCF is equivalent to:
|
||||
// c.HandlerC(xhandler.HandlerFuncC(xhc))
|
||||
func (c Chain) HandlerCF(xhc HandlerFuncC) HandlerC { |
||||
return c.HandlerC(HandlerFuncC(xhc)) |
||||
} |
@ -1,59 +0,0 @@ |
||||
package xhandler |
||||
|
||||
import ( |
||||
"net/http" |
||||
"time" |
||||
|
||||
"golang.org/x/net/context" |
||||
) |
||||
|
||||
// CloseHandler returns a Handler cancelling the context when the client
|
||||
// connection close unexpectedly.
|
||||
func CloseHandler(next HandlerC) HandlerC { |
||||
return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
// Cancel the context if the client closes the connection
|
||||
if wcn, ok := w.(http.CloseNotifier); ok { |
||||
var cancel context.CancelFunc |
||||
ctx, cancel = context.WithCancel(ctx) |
||||
defer cancel() |
||||
|
||||
notify := wcn.CloseNotify() |
||||
go func() { |
||||
select { |
||||
case <-notify: |
||||
cancel() |
||||
case <-ctx.Done(): |
||||
} |
||||
}() |
||||
} |
||||
|
||||
next.ServeHTTPC(ctx, w, r) |
||||
}) |
||||
} |
||||
|
||||
// TimeoutHandler returns a Handler which adds a timeout to the context.
|
||||
//
|
||||
// Child handlers have the responsability to obey the context deadline and to return
|
||||
// an appropriate error (or not) response in case of timeout.
|
||||
func TimeoutHandler(timeout time.Duration) func(next HandlerC) HandlerC { |
||||
return func(next HandlerC) HandlerC { |
||||
return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
ctx, _ = context.WithTimeout(ctx, timeout) |
||||
next.ServeHTTPC(ctx, w, r) |
||||
}) |
||||
} |
||||
} |
||||
|
||||
// If is a special handler that will skip insert the condNext handler only if a condition
|
||||
// applies at runtime.
|
||||
func If(cond func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool, condNext func(next HandlerC) HandlerC) func(next HandlerC) HandlerC { |
||||
return func(next HandlerC) HandlerC { |
||||
return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
if cond(ctx, w, r) { |
||||
condNext(next).ServeHTTPC(ctx, w, r) |
||||
} else { |
||||
next.ServeHTTPC(ctx, w, r) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -1,42 +0,0 @@ |
||||
// Package xhandler provides a bridge between http.Handler and net/context.
|
||||
//
|
||||
// xhandler enforces net/context in your handlers without sacrificing
|
||||
// compatibility with existing http.Handlers nor imposing a specific router.
|
||||
//
|
||||
// Thanks to net/context deadline management, xhandler is able to enforce
|
||||
// a per request deadline and will cancel the context in when the client close
|
||||
// the connection unexpectedly.
|
||||
//
|
||||
// You may create net/context aware middlewares pretty much the same way as
|
||||
// you would do with http.Handler.
|
||||
package xhandler |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"golang.org/x/net/context" |
||||
) |
||||
|
||||
// HandlerC is a net/context aware http.Handler
|
||||
type HandlerC interface { |
||||
ServeHTTPC(context.Context, http.ResponseWriter, *http.Request) |
||||
} |
||||
|
||||
// HandlerFuncC type is an adapter to allow the use of ordinary functions
|
||||
// as a xhandler.Handler. If f is a function with the appropriate signature,
|
||||
// xhandler.HandlerFuncC(f) is a xhandler.Handler object that calls f.
|
||||
type HandlerFuncC func(context.Context, http.ResponseWriter, *http.Request) |
||||
|
||||
// ServeHTTPC calls f(ctx, w, r).
|
||||
func (f HandlerFuncC) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
||||
f(ctx, w, r) |
||||
} |
||||
|
||||
// New creates a conventional http.Handler injecting the provided root
|
||||
// context to sub handlers. This handler is used as a bridge between conventional
|
||||
// http.Handler and context aware handlers.
|
||||
func New(ctx context.Context, h HandlerC) http.Handler { |
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
h.ServeHTTPC(ctx, w, r) |
||||
}) |
||||
} |
@ -0,0 +1,310 @@ |
||||
# Change Log |
||||
|
||||
**ATTN**: This project uses [semantic versioning](http://semver.org/). |
||||
|
||||
## [Unreleased] |
||||
|
||||
## [1.17.0] - 2016-05-09 |
||||
### Added |
||||
- Pluggable flag-level help text rendering via `cli.DefaultFlagStringFunc` |
||||
- `context.GlobalBoolT` was added as an analogue to `context.GlobalBool` |
||||
- Support for hiding commands by setting `Hidden: true` -- this will hide the |
||||
commands in help output |
||||
|
||||
### Changed |
||||
- `Float64Flag`, `IntFlag`, and `DurationFlag` default values are no longer |
||||
quoted in help text output. |
||||
- All flag types now include `(default: {value})` strings following usage when a |
||||
default value can be (reasonably) detected. |
||||
- `IntSliceFlag` and `StringSliceFlag` usage strings are now more consistent |
||||
with non-slice flag types |
||||
- Apps now exit with a code of 3 if an unknown subcommand is specified |
||||
(previously they printed "No help topic for...", but still exited 0. This |
||||
makes it easier to script around apps built using `cli` since they can trust |
||||
that a 0 exit code indicated a successful execution. |
||||
- cleanups based on [Go Report Card |
||||
feedback](https://goreportcard.com/report/github.com/codegangsta/cli) |
||||
|
||||
## [1.16.0] - 2016-05-02 |
||||
### Added |
||||
- `Hidden` field on all flag struct types to omit from generated help text |
||||
|
||||
### Changed |
||||
- `BashCompletionFlag` (`--enable-bash-completion`) is now omitted from |
||||
generated help text via the `Hidden` field |
||||
|
||||
### Fixed |
||||
- handling of error values in `HandleAction` and `HandleExitCoder` |
||||
|
||||
## [1.15.0] - 2016-04-30 |
||||
### Added |
||||
- This file! |
||||
- Support for placeholders in flag usage strings |
||||
- `App.Metadata` map for arbitrary data/state management |
||||
- `Set` and `GlobalSet` methods on `*cli.Context` for altering values after |
||||
parsing. |
||||
- Support for nested lookup of dot-delimited keys in structures loaded from |
||||
YAML. |
||||
|
||||
### Changed |
||||
- The `App.Action` and `Command.Action` now prefer a return signature of |
||||
`func(*cli.Context) error`, as defined by `cli.ActionFunc`. If a non-nil |
||||
`error` is returned, there may be two outcomes: |
||||
- If the error fulfills `cli.ExitCoder`, then `os.Exit` will be called |
||||
automatically |
||||
- Else the error is bubbled up and returned from `App.Run` |
||||
- Specifying an `Action` with the legacy return signature of |
||||
`func(*cli.Context)` will produce a deprecation message to stderr |
||||
- Specifying an `Action` that is not a `func` type will produce a non-zero exit |
||||
from `App.Run` |
||||
- Specifying an `Action` func that has an invalid (input) signature will |
||||
produce a non-zero exit from `App.Run` |
||||
|
||||
### Deprecated |
||||
- <a name="deprecated-cli-app-runandexitonerror"></a> |
||||
`cli.App.RunAndExitOnError`, which should now be done by returning an error |
||||
that fulfills `cli.ExitCoder` to `cli.App.Run`. |
||||
- <a name="deprecated-cli-app-action-signature"></a> the legacy signature for |
||||
`cli.App.Action` of `func(*cli.Context)`, which should now have a return |
||||
signature of `func(*cli.Context) error`, as defined by `cli.ActionFunc`. |
||||
|
||||
### Fixed |
||||
- Added missing `*cli.Context.GlobalFloat64` method |
||||
|
||||
## [1.14.0] - 2016-04-03 (backfilled 2016-04-25) |
||||
### Added |
||||
- Codebeat badge |
||||
- Support for categorization via `CategorizedHelp` and `Categories` on app. |
||||
|
||||
### Changed |
||||
- Use `filepath.Base` instead of `path.Base` in `Name` and `HelpName`. |
||||
|
||||
### Fixed |
||||
- Ensure version is not shown in help text when `HideVersion` set. |
||||
|
||||
## [1.13.0] - 2016-03-06 (backfilled 2016-04-25) |
||||
### Added |
||||
- YAML file input support. |
||||
- `NArg` method on context. |
||||
|
||||
## [1.12.0] - 2016-02-17 (backfilled 2016-04-25) |
||||
### Added |
||||
- Custom usage error handling. |
||||
- Custom text support in `USAGE` section of help output. |
||||
- Improved help messages for empty strings. |
||||
- AppVeyor CI configuration. |
||||
|
||||
### Changed |
||||
- Removed `panic` from default help printer func. |
||||
- De-duping and optimizations. |
||||
|
||||
### Fixed |
||||
- Correctly handle `Before`/`After` at command level when no subcommands. |
||||
- Case of literal `-` argument causing flag reordering. |
||||
- Environment variable hints on Windows. |
||||
- Docs updates. |
||||
|
||||
## [1.11.1] - 2015-12-21 (backfilled 2016-04-25) |
||||
### Changed |
||||
- Use `path.Base` in `Name` and `HelpName` |
||||
- Export `GetName` on flag types. |
||||
|
||||
### Fixed |
||||
- Flag parsing when skipping is enabled. |
||||
- Test output cleanup. |
||||
- Move completion check to account for empty input case. |
||||
|
||||
## [1.11.0] - 2015-11-15 (backfilled 2016-04-25) |
||||
### Added |
||||
- Destination scan support for flags. |
||||
- Testing against `tip` in Travis CI config. |
||||
|
||||
### Changed |
||||
- Go version in Travis CI config. |
||||
|
||||
### Fixed |
||||
- Removed redundant tests. |
||||
- Use correct example naming in tests. |
||||
|
||||
## [1.10.2] - 2015-10-29 (backfilled 2016-04-25) |
||||
### Fixed |
||||
- Remove unused var in bash completion. |
||||
|
||||
## [1.10.1] - 2015-10-21 (backfilled 2016-04-25) |
||||
### Added |
||||
- Coverage and reference logos in README. |
||||
|
||||
### Fixed |
||||
- Use specified values in help and version parsing. |
||||
- Only display app version and help message once. |
||||
|
||||
## [1.10.0] - 2015-10-06 (backfilled 2016-04-25) |
||||
### Added |
||||
- More tests for existing functionality. |
||||
- `ArgsUsage` at app and command level for help text flexibility. |
||||
|
||||
### Fixed |
||||
- Honor `HideHelp` and `HideVersion` in `App.Run`. |
||||
- Remove juvenile word from README. |
||||
|
||||
## [1.9.0] - 2015-09-08 (backfilled 2016-04-25) |
||||
### Added |
||||
- `FullName` on command with accompanying help output update. |
||||
- Set default `$PROG` in bash completion. |
||||
|
||||
### Changed |
||||
- Docs formatting. |
||||
|
||||
### Fixed |
||||
- Removed self-referential imports in tests. |
||||
|
||||
## [1.8.0] - 2015-06-30 (backfilled 2016-04-25) |
||||
### Added |
||||
- Support for `Copyright` at app level. |
||||
- `Parent` func at context level to walk up context lineage. |
||||
|
||||
### Fixed |
||||
- Global flag processing at top level. |
||||
|
||||
## [1.7.1] - 2015-06-11 (backfilled 2016-04-25) |
||||
### Added |
||||
- Aggregate errors from `Before`/`After` funcs. |
||||
- Doc comments on flag structs. |
||||
- Include non-global flags when checking version and help. |
||||
- Travis CI config updates. |
||||
|
||||
### Fixed |
||||
- Ensure slice type flags have non-nil values. |
||||
- Collect global flags from the full command hierarchy. |
||||
- Docs prose. |
||||
|
||||
## [1.7.0] - 2015-05-03 (backfilled 2016-04-25) |
||||
### Changed |
||||
- `HelpPrinter` signature includes output writer. |
||||
|
||||
### Fixed |
||||
- Specify go 1.1+ in docs. |
||||
- Set `Writer` when running command as app. |
||||
|
||||
## [1.6.0] - 2015-03-23 (backfilled 2016-04-25) |
||||
### Added |
||||
- Multiple author support. |
||||
- `NumFlags` at context level. |
||||
- `Aliases` at command level. |
||||
|
||||
### Deprecated |
||||
- `ShortName` at command level. |
||||
|
||||
### Fixed |
||||
- Subcommand help output. |
||||
- Backward compatible support for deprecated `Author` and `Email` fields. |
||||
- Docs regarding `Names`/`Aliases`. |
||||
|
||||
## [1.5.0] - 2015-02-20 (backfilled 2016-04-25) |
||||
### Added |
||||
- `After` hook func support at app and command level. |
||||
|
||||
### Fixed |
||||
- Use parsed context when running command as subcommand. |
||||
- Docs prose. |
||||
|
||||
## [1.4.1] - 2015-01-09 (backfilled 2016-04-25) |
||||
### Added |
||||
- Support for hiding `-h / --help` flags, but not `help` subcommand. |
||||
- Stop flag parsing after `--`. |
||||
|
||||
### Fixed |
||||
- Help text for generic flags to specify single value. |
||||
- Use double quotes in output for defaults. |
||||
- Use `ParseInt` instead of `ParseUint` for int environment var values. |
||||
- Use `0` as base when parsing int environment var values. |
||||
|
||||
## [1.4.0] - 2014-12-12 (backfilled 2016-04-25) |
||||
### Added |
||||
- Support for environment variable lookup "cascade". |
||||
- Support for `Stdout` on app for output redirection. |
||||
|
||||
### Fixed |
||||
- Print command help instead of app help in `ShowCommandHelp`. |
||||
|
||||
## [1.3.1] - 2014-11-13 (backfilled 2016-04-25) |
||||
### Added |
||||
- Docs and example code updates. |
||||
|
||||
### Changed |
||||
- Default `-v / --version` flag made optional. |
||||
|
||||
## [1.3.0] - 2014-08-10 (backfilled 2016-04-25) |
||||
### Added |
||||
- `FlagNames` at context level. |
||||
- Exposed `VersionPrinter` var for more control over version output. |
||||
- Zsh completion hook. |
||||
- `AUTHOR` section in default app help template. |
||||
- Contribution guidelines. |
||||
- `DurationFlag` type. |
||||
|
||||
## [1.2.0] - 2014-08-02 |
||||
### Added |
||||
- Support for environment variable defaults on flags plus tests. |
||||
|
||||
## [1.1.0] - 2014-07-15 |
||||
### Added |
||||
- Bash completion. |
||||
- Optional hiding of built-in help command. |
||||
- Optional skipping of flag parsing at command level. |
||||
- `Author`, `Email`, and `Compiled` metadata on app. |
||||
- `Before` hook func support at app and command level. |
||||
- `CommandNotFound` func support at app level. |
||||
- Command reference available on context. |
||||
- `GenericFlag` type. |
||||
- `Float64Flag` type. |
||||
- `BoolTFlag` type. |
||||
- `IsSet` flag helper on context. |
||||
- More flag lookup funcs at context level. |
||||
- More tests & docs. |
||||
|
||||
### Changed |
||||
- Help template updates to account for presence/absence of flags. |
||||
- Separated subcommand help template. |
||||
- Exposed `HelpPrinter` var for more control over help output. |
||||
|
||||
## [1.0.0] - 2013-11-01 |
||||
### Added |
||||
- `help` flag in default app flag set and each command flag set. |
||||
- Custom handling of argument parsing errors. |
||||
- Command lookup by name at app level. |
||||
- `StringSliceFlag` type and supporting `StringSlice` type. |
||||
- `IntSliceFlag` type and supporting `IntSlice` type. |
||||
- Slice type flag lookups by name at context level. |
||||
- Export of app and command help functions. |
||||
- More tests & docs. |
||||
|
||||
## 0.1.0 - 2013-07-22 |
||||
### Added |
||||
- Initial implementation. |
||||
|
||||
[Unreleased]: https://github.com/codegangsta/cli/compare/v1.17.0...HEAD |
||||
[1.17.0]: https://github.com/codegangsta/cli/compare/v1.16.0...v1.17.0 |
||||
[1.16.0]: https://github.com/codegangsta/cli/compare/v1.15.0...v1.16.0 |
||||
[1.15.0]: https://github.com/codegangsta/cli/compare/v1.14.0...v1.15.0 |
||||
[1.14.0]: https://github.com/codegangsta/cli/compare/v1.13.0...v1.14.0 |
||||
[1.13.0]: https://github.com/codegangsta/cli/compare/v1.12.0...v1.13.0 |
||||
[1.12.0]: https://github.com/codegangsta/cli/compare/v1.11.1...v1.12.0 |
||||
[1.11.1]: https://github.com/codegangsta/cli/compare/v1.11.0...v1.11.1 |
||||
[1.11.0]: https://github.com/codegangsta/cli/compare/v1.10.2...v1.11.0 |
||||
[1.10.2]: https://github.com/codegangsta/cli/compare/v1.10.1...v1.10.2 |
||||
[1.10.1]: https://github.com/codegangsta/cli/compare/v1.10.0...v1.10.1 |
||||
[1.10.0]: https://github.com/codegangsta/cli/compare/v1.9.0...v1.10.0 |
||||
[1.9.0]: https://github.com/codegangsta/cli/compare/v1.8.0...v1.9.0 |
||||
[1.8.0]: https://github.com/codegangsta/cli/compare/v1.7.1...v1.8.0 |
||||
[1.7.1]: https://github.com/codegangsta/cli/compare/v1.7.0...v1.7.1 |
||||
[1.7.0]: https://github.com/codegangsta/cli/compare/v1.6.0...v1.7.0 |
||||
[1.6.0]: https://github.com/codegangsta/cli/compare/v1.5.0...v1.6.0 |
||||
[1.5.0]: https://github.com/codegangsta/cli/compare/v1.4.1...v1.5.0 |
||||
[1.4.1]: https://github.com/codegangsta/cli/compare/v1.4.0...v1.4.1 |
||||
[1.4.0]: https://github.com/codegangsta/cli/compare/v1.3.1...v1.4.0 |
||||
[1.3.1]: https://github.com/codegangsta/cli/compare/v1.3.0...v1.3.1 |
||||
[1.3.0]: https://github.com/codegangsta/cli/compare/v1.2.0...v1.3.0 |
||||
[1.2.0]: https://github.com/codegangsta/cli/compare/v1.1.0...v1.2.0 |
||||
[1.1.0]: https://github.com/codegangsta/cli/compare/v1.0.0...v1.1.0 |
||||
[1.0.0]: https://github.com/codegangsta/cli/compare/v0.1.0...v1.0.0 |
@ -0,0 +1,44 @@ |
||||
package cli |
||||
|
||||
// CommandCategories is a slice of *CommandCategory.
|
||||
type CommandCategories []*CommandCategory |
||||
|
||||
// CommandCategory is a category containing commands.
|
||||
type CommandCategory struct { |
||||
Name string |
||||
Commands Commands |
||||
} |
||||
|
||||
func (c CommandCategories) Less(i, j int) bool { |
||||
return c[i].Name < c[j].Name |
||||
} |
||||
|
||||
func (c CommandCategories) Len() int { |
||||
return len(c) |
||||
} |
||||
|
||||
func (c CommandCategories) Swap(i, j int) { |
||||
c[i], c[j] = c[j], c[i] |
||||
} |
||||
|
||||
// AddCommand adds a command to a category.
|
||||
func (c CommandCategories) AddCommand(category string, command Command) CommandCategories { |
||||
for _, commandCategory := range c { |
||||
if commandCategory.Name == category { |
||||
commandCategory.Commands = append(commandCategory.Commands, command) |
||||
return c |
||||
} |
||||
} |
||||
return append(c, &CommandCategory{Name: category, Commands: []Command{command}}) |
||||
} |
||||
|
||||
// VisibleCommands returns a slice of the Commands with Hidden=false
|
||||
func (c *CommandCategory) VisibleCommands() []Command { |
||||
ret := []Command{} |
||||
for _, command := range c.Commands { |
||||
if !command.Hidden { |
||||
ret = append(ret, command) |
||||
} |
||||
} |
||||
return ret |
||||
} |
@ -0,0 +1,92 @@ |
||||
package cli |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
"os" |
||||
"strings" |
||||
) |
||||
|
||||
// OsExiter is the function used when the app exits. If not set defaults to os.Exit.
|
||||
var OsExiter = os.Exit |
||||
|
||||
// ErrWriter is used to write errors to the user. This can be anything
|
||||
// implementing the io.Writer interface and defaults to os.Stderr.
|
||||
var ErrWriter io.Writer = os.Stderr |
||||
|
||||
// MultiError is an error that wraps multiple errors.
|
||||
type MultiError struct { |
||||
Errors []error |
||||
} |
||||
|
||||
// NewMultiError creates a new MultiError. Pass in one or more errors.
|
||||
func NewMultiError(err ...error) MultiError { |
||||
return MultiError{Errors: err} |
||||
} |
||||
|
||||
// Error implents the error interface.
|
||||
func (m MultiError) Error() string { |
||||
errs := make([]string, len(m.Errors)) |
||||
for i, err := range m.Errors { |
||||
errs[i] = err.Error() |
||||
} |
||||
|
||||
return strings.Join(errs, "\n") |
||||
} |
||||
|
||||
// ExitCoder is the interface checked by `App` and `Command` for a custom exit
|
||||
// code
|
||||
type ExitCoder interface { |
||||
error |
||||
ExitCode() int |
||||
} |
||||
|
||||
// ExitError fulfills both the builtin `error` interface and `ExitCoder`
|
||||
type ExitError struct { |
||||
exitCode int |
||||
message string |
||||
} |
||||
|
||||
// NewExitError makes a new *ExitError
|
||||
func NewExitError(message string, exitCode int) *ExitError { |
||||
return &ExitError{ |
||||
exitCode: exitCode, |
||||
message: message, |
||||
} |
||||
} |
||||
|
||||
// Error returns the string message, fulfilling the interface required by
|
||||
// `error`
|
||||
func (ee *ExitError) Error() string { |
||||
return ee.message |
||||
} |
||||
|
||||
// ExitCode returns the exit code, fulfilling the interface required by
|
||||
// `ExitCoder`
|
||||
func (ee *ExitError) ExitCode() int { |
||||
return ee.exitCode |
||||
} |
||||
|
||||
// HandleExitCoder checks if the error fulfills the ExitCoder interface, and if
|
||||
// so prints the error to stderr (if it is non-empty) and calls OsExiter with the
|
||||
// given exit code. If the given error is a MultiError, then this func is
|
||||
// called on all members of the Errors slice.
|
||||
func HandleExitCoder(err error) { |
||||
if err == nil { |
||||
return |
||||
} |
||||
|
||||
if exitErr, ok := err.(ExitCoder); ok { |
||||
if err.Error() != "" { |
||||
fmt.Fprintln(ErrWriter, err) |
||||
} |
||||
OsExiter(exitErr.ExitCode()) |
||||
return |
||||
} |
||||
|
||||
if multiErr, ok := err.(MultiError); ok { |
||||
for _, merr := range multiErr.Errors { |
||||
HandleExitCoder(merr) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
package cli |
||||
|
||||
// BashCompleteFunc is an action to execute when the bash-completion flag is set
|
||||
type BashCompleteFunc func(*Context) |
||||
|
||||
// BeforeFunc is an action to execute before any subcommands are run, but after
|
||||
// the context is ready if a non-nil error is returned, no subcommands are run
|
||||
type BeforeFunc func(*Context) error |
||||
|
||||
// AfterFunc is an action to execute after any subcommands are run, but after the
|
||||
// subcommand has finished it is run even if Action() panics
|
||||
type AfterFunc func(*Context) error |
||||
|
||||
// ActionFunc is the action to execute when no subcommands are specified
|
||||
type ActionFunc func(*Context) error |
||||
|
||||
// CommandNotFoundFunc is executed if the proper command cannot be found
|
||||
type CommandNotFoundFunc func(*Context, string) |
||||
|
||||
// OnUsageErrorFunc is executed if an usage error occurs. This is useful for displaying
|
||||
// customized usage error messages. This function is able to replace the
|
||||
// original error messages. If this function is not set, the "Incorrect usage"
|
||||
// is displayed and the execution is interrupted.
|
||||
type OnUsageErrorFunc func(context *Context, err error, isSubcommand bool) error |
||||
|
||||
// FlagStringFunc is used by the help generation to display a flag, which is
|
||||
// expected to be a single line.
|
||||
type FlagStringFunc func(Flag) string |
Loading…
Reference in new issue