Merge pull request #1949 from karalabe/update-command-usage
cmd/geth, cmd/utils, eth: group CLI flags by purpose
This commit is contained in:
commit
98b036ddb6
|
@ -7,8 +7,8 @@
|
||||||
"Deps": [
|
"Deps": [
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/codegangsta/cli",
|
"ImportPath": "github.com/codegangsta/cli",
|
||||||
"Comment": "1.2.0-95-g9b2bd2b",
|
"Comment": "1.2.0-161-gf445c89",
|
||||||
"Rev": "9b2bd2b3489748d4d0a204fa4eb2ee9e89e0ebc6"
|
"Rev": "f445c894402839580d30de47551cedc152dad814"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/davecgh/go-spew/spew",
|
"ImportPath": "github.com/davecgh/go-spew/spew",
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
language: go
|
language: go
|
||||||
go: 1.1
|
sudo: false
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.0.3
|
||||||
|
- 1.1.2
|
||||||
|
- 1.2.2
|
||||||
|
- 1.3.3
|
||||||
|
- 1.4.2
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- go vet ./...
|
- go vet ./...
|
||||||
|
|
|
@ -1,18 +1,17 @@
|
||||||
|
[![Coverage](http://gocover.io/_badge/github.com/codegangsta/cli?0)](http://gocover.io/github.com/codegangsta/cli)
|
||||||
[![Build Status](https://travis-ci.org/codegangsta/cli.png?branch=master)](https://travis-ci.org/codegangsta/cli)
|
[![Build Status](https://travis-ci.org/codegangsta/cli.png?branch=master)](https://travis-ci.org/codegangsta/cli)
|
||||||
|
[![GoDoc](https://godoc.org/github.com/codegangsta/cli?status.svg)](https://godoc.org/github.com/codegangsta/cli)
|
||||||
|
|
||||||
# cli.go
|
# cli.go
|
||||||
cli.go is 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.
|
`cli.go` is 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.
|
||||||
|
|
||||||
You can view the API docs here:
|
|
||||||
http://godoc.org/github.com/codegangsta/cli
|
|
||||||
|
|
||||||
## Overview
|
## 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.
|
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.go comes into play.** cli.go makes command line programming fun, organized, and expressive!
|
**This is where `cli.go` comes into play.** `cli.go` makes command line programming fun, organized, and expressive!
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
Make sure you have a working Go environment (go 1.1 is *required*). [See the install instructions](http://golang.org/doc/install.html).
|
Make sure you have a working Go environment (go 1.1+ is *required*). [See the install instructions](http://golang.org/doc/install.html).
|
||||||
|
|
||||||
To install `cli.go`, simply run:
|
To install `cli.go`, simply run:
|
||||||
```
|
```
|
||||||
|
@ -25,7 +24,7 @@ export PATH=$PATH:$GOPATH/bin
|
||||||
```
|
```
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
One of the philosophies behind cli.go is that an API should be playful and full of discovery. So a cli.go app can be as little as one line of code in `main()`.
|
One of the philosophies behind `cli.go` is that an API should be playful and full of discovery. So a `cli.go` app can be as little as one line of code in `main()`.
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
package main
|
package main
|
||||||
|
@ -103,7 +102,8 @@ $ greet
|
||||||
Hello friend!
|
Hello friend!
|
||||||
```
|
```
|
||||||
|
|
||||||
cli.go also generates some bitchass help text:
|
`cli.go` also generates neat help text:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ greet help
|
$ greet help
|
||||||
NAME:
|
NAME:
|
||||||
|
@ -158,6 +158,8 @@ app.Action = func(c *cli.Context) {
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
See full list of flags at http://godoc.org/github.com/codegangsta/cli
|
||||||
|
|
||||||
#### Alternate Names
|
#### Alternate Names
|
||||||
|
|
||||||
You can set alternate (or short) names for flags by providing a comma-delimited list for the `Name`. e.g.
|
You can set alternate (or short) names for flags by providing a comma-delimited list for the `Name`. e.g.
|
||||||
|
@ -289,6 +291,21 @@ setting the `PROG` variable to the name of your program:
|
||||||
|
|
||||||
`PROG=myprogram source /.../cli/autocomplete/bash_autocomplete`
|
`PROG=myprogram source /.../cli/autocomplete/bash_autocomplete`
|
||||||
|
|
||||||
|
#### To Distribute
|
||||||
|
|
||||||
|
Copy `autocomplete/bash_autocomplete` into `/etc/bash_completion.d/` and rename
|
||||||
|
it to the name of the program you wish to add autocomplete support for (or
|
||||||
|
automatically install it there if you are distributing a package). Don't forget
|
||||||
|
to source the file to make it active in the current shell.
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo cp src/bash_autocomplete /etc/bash_completion.d/<myprogram>
|
||||||
|
source /etc/bash_completion.d/<myprogram>
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, you can just document that users should source the generic
|
||||||
|
`autocomplete/bash_autocomplete` in their bash configuration with `$PROG` set
|
||||||
|
to the name of their program (as above).
|
||||||
|
|
||||||
## Contribution Guidelines
|
## Contribution Guidelines
|
||||||
Feel free to put up a pull request to fix a bug or maybe add a feature. I will give it a code review and make sure that it does not break backwards compatibility. If I or any other collaborators agree that it is in line with the vision of the project, we will work with you to get the code into a mergeable state and merge it into the master branch.
|
Feel free to put up a pull request to fix a bug or maybe add a feature. I will give it a code review and make sure that it does not break backwards compatibility. If I or any other collaborators agree that it is in line with the vision of the project, we will work with you to get the code into a mergeable state and merge it into the master branch.
|
||||||
|
|
|
@ -5,19 +5,20 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"text/tabwriter"
|
|
||||||
"text/template"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App is the main structure of a cli application. It is recomended that
|
// App is the main structure of a cli application. It is recomended that
|
||||||
// and app be created with the cli.NewApp() function
|
// an app be created with the cli.NewApp() function
|
||||||
type App struct {
|
type App struct {
|
||||||
// The name of the program. Defaults to os.Args[0]
|
// The name of the program. Defaults to os.Args[0]
|
||||||
Name string
|
Name string
|
||||||
|
// Full name of command for help, defaults to Name
|
||||||
|
HelpName string
|
||||||
// Description of the program.
|
// Description of the program.
|
||||||
Usage string
|
Usage string
|
||||||
|
// Description of the program argument format.
|
||||||
|
ArgsUsage string
|
||||||
// Version of the program
|
// Version of the program
|
||||||
Version string
|
Version string
|
||||||
// List of commands to execute
|
// List of commands to execute
|
||||||
|
@ -46,6 +47,8 @@ type App struct {
|
||||||
Compiled time.Time
|
Compiled time.Time
|
||||||
// List of all authors who contributed
|
// List of all authors who contributed
|
||||||
Authors []Author
|
Authors []Author
|
||||||
|
// Copyright of the binary if any
|
||||||
|
Copyright string
|
||||||
// Name of Author (Note: Use App.Authors, this is deprecated)
|
// Name of Author (Note: Use App.Authors, this is deprecated)
|
||||||
Author string
|
Author string
|
||||||
// Email of Author (Note: Use App.Authors, this is deprecated)
|
// Email of Author (Note: Use App.Authors, this is deprecated)
|
||||||
|
@ -68,6 +71,7 @@ func compileTime() time.Time {
|
||||||
func NewApp() *App {
|
func NewApp() *App {
|
||||||
return &App{
|
return &App{
|
||||||
Name: os.Args[0],
|
Name: os.Args[0],
|
||||||
|
HelpName: os.Args[0],
|
||||||
Usage: "A new cli application",
|
Usage: "A new cli application",
|
||||||
Version: "0.0.0",
|
Version: "0.0.0",
|
||||||
BashComplete: DefaultAppComplete,
|
BashComplete: DefaultAppComplete,
|
||||||
|
@ -83,25 +87,14 @@ func (a *App) Run(arguments []string) (err error) {
|
||||||
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
||||||
}
|
}
|
||||||
|
|
||||||
if HelpPrinter == nil {
|
newCmds := []Command{}
|
||||||
defer func() {
|
for _, c := range a.Commands {
|
||||||
HelpPrinter = nil
|
if c.HelpName == "" {
|
||||||
}()
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
||||||
|
|
||||||
HelpPrinter = func(templ string, data interface{}) {
|
|
||||||
funcMap := template.FuncMap{
|
|
||||||
"join": strings.Join,
|
|
||||||
}
|
|
||||||
|
|
||||||
w := tabwriter.NewWriter(a.Writer, 0, 8, 1, '\t', 0)
|
|
||||||
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
|
||||||
err := t.Execute(w, data)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
w.Flush()
|
|
||||||
}
|
}
|
||||||
|
newCmds = append(newCmds, c)
|
||||||
}
|
}
|
||||||
|
a.Commands = newCmds
|
||||||
|
|
||||||
// append help to commands
|
// append help to commands
|
||||||
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
||||||
|
@ -127,17 +120,16 @@ func (a *App) Run(arguments []string) (err error) {
|
||||||
nerr := normalizeFlags(a.Flags, set)
|
nerr := normalizeFlags(a.Flags, set)
|
||||||
if nerr != nil {
|
if nerr != nil {
|
||||||
fmt.Fprintln(a.Writer, nerr)
|
fmt.Fprintln(a.Writer, nerr)
|
||||||
context := NewContext(a, set, set)
|
context := NewContext(a, set, nil)
|
||||||
ShowAppHelp(context)
|
ShowAppHelp(context)
|
||||||
fmt.Fprintln(a.Writer)
|
|
||||||
return nerr
|
return nerr
|
||||||
}
|
}
|
||||||
context := NewContext(a, set, set)
|
context := NewContext(a, set, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(a.Writer, "Incorrect Usage.\n\n")
|
fmt.Fprintln(a.Writer, "Incorrect Usage.")
|
||||||
ShowAppHelp(context)
|
|
||||||
fmt.Fprintln(a.Writer)
|
fmt.Fprintln(a.Writer)
|
||||||
|
ShowAppHelp(context)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,20 +137,26 @@ func (a *App) Run(arguments []string) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if checkHelp(context) {
|
if !a.HideHelp && checkHelp(context) {
|
||||||
|
ShowAppHelp(context)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if checkVersion(context) {
|
if !a.HideVersion && checkVersion(context) {
|
||||||
|
ShowVersion(context)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.After != nil {
|
if a.After != nil {
|
||||||
defer func() {
|
defer func() {
|
||||||
// err is always nil here.
|
afterErr := a.After(context)
|
||||||
// There is a check to see if it is non-nil
|
if afterErr != nil {
|
||||||
// just few lines before.
|
if err != nil {
|
||||||
err = a.After(context)
|
err = NewMultiError(err, afterErr)
|
||||||
|
} else {
|
||||||
|
err = afterErr
|
||||||
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,6 +201,15 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newCmds := []Command{}
|
||||||
|
for _, c := range a.Commands {
|
||||||
|
if c.HelpName == "" {
|
||||||
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
||||||
|
}
|
||||||
|
newCmds = append(newCmds, c)
|
||||||
|
}
|
||||||
|
a.Commands = newCmds
|
||||||
|
|
||||||
// append flags
|
// append flags
|
||||||
if a.EnableBashCompletion {
|
if a.EnableBashCompletion {
|
||||||
a.appendFlag(BashCompletionFlag)
|
a.appendFlag(BashCompletionFlag)
|
||||||
|
@ -213,21 +220,22 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||||
set.SetOutput(ioutil.Discard)
|
set.SetOutput(ioutil.Discard)
|
||||||
err = set.Parse(ctx.Args().Tail())
|
err = set.Parse(ctx.Args().Tail())
|
||||||
nerr := normalizeFlags(a.Flags, set)
|
nerr := normalizeFlags(a.Flags, set)
|
||||||
context := NewContext(a, set, ctx.globalSet)
|
context := NewContext(a, set, ctx)
|
||||||
|
|
||||||
if nerr != nil {
|
if nerr != nil {
|
||||||
fmt.Fprintln(a.Writer, nerr)
|
fmt.Fprintln(a.Writer, nerr)
|
||||||
|
fmt.Fprintln(a.Writer)
|
||||||
if len(a.Commands) > 0 {
|
if len(a.Commands) > 0 {
|
||||||
ShowSubcommandHelp(context)
|
ShowSubcommandHelp(context)
|
||||||
} else {
|
} else {
|
||||||
ShowCommandHelp(ctx, context.Args().First())
|
ShowCommandHelp(ctx, context.Args().First())
|
||||||
}
|
}
|
||||||
fmt.Fprintln(a.Writer)
|
|
||||||
return nerr
|
return nerr
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(a.Writer, "Incorrect Usage.\n\n")
|
fmt.Fprintln(a.Writer, "Incorrect Usage.")
|
||||||
|
fmt.Fprintln(a.Writer)
|
||||||
ShowSubcommandHelp(context)
|
ShowSubcommandHelp(context)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -248,10 +256,14 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||||
|
|
||||||
if a.After != nil {
|
if a.After != nil {
|
||||||
defer func() {
|
defer func() {
|
||||||
// err is always nil here.
|
afterErr := a.After(context)
|
||||||
// There is a check to see if it is non-nil
|
if afterErr != nil {
|
||||||
// just few lines before.
|
if err != nil {
|
||||||
err = a.After(context)
|
err = NewMultiError(err, afterErr)
|
||||||
|
} else {
|
||||||
|
err = afterErr
|
||||||
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,622 +0,0 @@
|
||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ExampleApp() {
|
|
||||||
// set args for examples sake
|
|
||||||
os.Args = []string{"greet", "--name", "Jeremy"}
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Name = "greet"
|
|
||||||
app.Flags = []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
|
||||||
}
|
|
||||||
app.Action = func(c *cli.Context) {
|
|
||||||
fmt.Printf("Hello %v\n", c.String("name"))
|
|
||||||
}
|
|
||||||
app.Author = "Harrison"
|
|
||||||
app.Email = "harrison@lolwut.com"
|
|
||||||
app.Authors = []cli.Author{cli.Author{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
|
|
||||||
app.Run(os.Args)
|
|
||||||
// Output:
|
|
||||||
// Hello Jeremy
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleAppSubcommand() {
|
|
||||||
// set args for examples sake
|
|
||||||
os.Args = []string{"say", "hi", "english", "--name", "Jeremy"}
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Name = "say"
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "hello",
|
|
||||||
Aliases: []string{"hi"},
|
|
||||||
Usage: "use it to see a description",
|
|
||||||
Description: "This is how we describe hello the function",
|
|
||||||
Subcommands: []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "english",
|
|
||||||
Aliases: []string{"en"},
|
|
||||||
Usage: "sends a greeting in english",
|
|
||||||
Description: "greets someone in english",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "name",
|
|
||||||
Value: "Bob",
|
|
||||||
Usage: "Name of the person to greet",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
fmt.Println("Hello,", c.String("name"))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Run(os.Args)
|
|
||||||
// Output:
|
|
||||||
// Hello, Jeremy
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleAppHelp() {
|
|
||||||
// set args for examples sake
|
|
||||||
os.Args = []string{"greet", "h", "describeit"}
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Name = "greet"
|
|
||||||
app.Flags = []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
|
||||||
}
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "describeit",
|
|
||||||
Aliases: []string{"d"},
|
|
||||||
Usage: "use it to see a description",
|
|
||||||
Description: "This is how we describe describeit the function",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
fmt.Printf("i like to describe things")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
app.Run(os.Args)
|
|
||||||
// Output:
|
|
||||||
// NAME:
|
|
||||||
// describeit - use it to see a description
|
|
||||||
//
|
|
||||||
// USAGE:
|
|
||||||
// command describeit [arguments...]
|
|
||||||
//
|
|
||||||
// DESCRIPTION:
|
|
||||||
// This is how we describe describeit the function
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleAppBashComplete() {
|
|
||||||
// set args for examples sake
|
|
||||||
os.Args = []string{"greet", "--generate-bash-completion"}
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Name = "greet"
|
|
||||||
app.EnableBashCompletion = true
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "describeit",
|
|
||||||
Aliases: []string{"d"},
|
|
||||||
Usage: "use it to see a description",
|
|
||||||
Description: "This is how we describe describeit the function",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
fmt.Printf("i like to describe things")
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
Name: "next",
|
|
||||||
Usage: "next example",
|
|
||||||
Description: "more stuff to see when generating bash completion",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
fmt.Printf("the next example")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Run(os.Args)
|
|
||||||
// Output:
|
|
||||||
// describeit
|
|
||||||
// d
|
|
||||||
// next
|
|
||||||
// help
|
|
||||||
// h
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_Run(t *testing.T) {
|
|
||||||
s := ""
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Action = func(c *cli.Context) {
|
|
||||||
s = s + c.Args().First()
|
|
||||||
}
|
|
||||||
|
|
||||||
err := app.Run([]string{"command", "foo"})
|
|
||||||
expect(t, err, nil)
|
|
||||||
err = app.Run([]string{"command", "bar"})
|
|
||||||
expect(t, err, nil)
|
|
||||||
expect(t, s, "foobar")
|
|
||||||
}
|
|
||||||
|
|
||||||
var commandAppTests = []struct {
|
|
||||||
name string
|
|
||||||
expected bool
|
|
||||||
}{
|
|
||||||
{"foobar", true},
|
|
||||||
{"batbaz", true},
|
|
||||||
{"b", true},
|
|
||||||
{"f", true},
|
|
||||||
{"bat", false},
|
|
||||||
{"nothing", false},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_Command(t *testing.T) {
|
|
||||||
app := cli.NewApp()
|
|
||||||
fooCommand := cli.Command{Name: "foobar", Aliases: []string{"f"}}
|
|
||||||
batCommand := cli.Command{Name: "batbaz", Aliases: []string{"b"}}
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
fooCommand,
|
|
||||||
batCommand,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range commandAppTests {
|
|
||||||
expect(t, app.Command(test.name) != nil, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
|
|
||||||
var parsedOption, firstArg string
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
command := cli.Command{
|
|
||||||
Name: "cmd",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "option", Value: "", Usage: "some option"},
|
|
||||||
},
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
parsedOption = c.String("option")
|
|
||||||
firstArg = c.Args().First()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
app.Commands = []cli.Command{command}
|
|
||||||
|
|
||||||
app.Run([]string{"", "cmd", "my-arg", "--option", "my-option"})
|
|
||||||
|
|
||||||
expect(t, parsedOption, "my-option")
|
|
||||||
expect(t, firstArg, "my-arg")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
|
|
||||||
var context *cli.Context
|
|
||||||
|
|
||||||
a := cli.NewApp()
|
|
||||||
a.Commands = []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "foo",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
context = c
|
|
||||||
},
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "lang",
|
|
||||||
Value: "english",
|
|
||||||
Usage: "language for the greeting",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Before: func(_ *cli.Context) error { return nil },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
|
|
||||||
|
|
||||||
expect(t, context.Args().Get(0), "abcd")
|
|
||||||
expect(t, context.String("lang"), "spanish")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
|
|
||||||
var parsedOption string
|
|
||||||
var args []string
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
command := cli.Command{
|
|
||||||
Name: "cmd",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "option", Value: "", Usage: "some option"},
|
|
||||||
},
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
parsedOption = c.String("option")
|
|
||||||
args = c.Args()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
app.Commands = []cli.Command{command}
|
|
||||||
|
|
||||||
app.Run([]string{"", "cmd", "my-arg", "--option", "my-option", "--", "--notARealFlag"})
|
|
||||||
|
|
||||||
expect(t, parsedOption, "my-option")
|
|
||||||
expect(t, args[0], "my-arg")
|
|
||||||
expect(t, args[1], "--")
|
|
||||||
expect(t, args[2], "--notARealFlag")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
|
|
||||||
var args []string
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
command := cli.Command{
|
|
||||||
Name: "cmd",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
args = c.Args()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
app.Commands = []cli.Command{command}
|
|
||||||
|
|
||||||
app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"})
|
|
||||||
|
|
||||||
expect(t, args[0], "my-arg")
|
|
||||||
expect(t, args[1], "--")
|
|
||||||
expect(t, args[2], "notAFlagAtAll")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_Float64Flag(t *testing.T) {
|
|
||||||
var meters float64
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Flags = []cli.Flag{
|
|
||||||
cli.Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
|
|
||||||
}
|
|
||||||
app.Action = func(c *cli.Context) {
|
|
||||||
meters = c.Float64("height")
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Run([]string{"", "--height", "1.93"})
|
|
||||||
expect(t, meters, 1.93)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_ParseSliceFlags(t *testing.T) {
|
|
||||||
var parsedOption, firstArg string
|
|
||||||
var parsedIntSlice []int
|
|
||||||
var parsedStringSlice []string
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
command := cli.Command{
|
|
||||||
Name: "cmd",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntSliceFlag{Name: "p", Value: &cli.IntSlice{}, Usage: "set one or more ip addr"},
|
|
||||||
cli.StringSliceFlag{Name: "ip", Value: &cli.StringSlice{}, Usage: "set one or more ports to open"},
|
|
||||||
},
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
parsedIntSlice = c.IntSlice("p")
|
|
||||||
parsedStringSlice = c.StringSlice("ip")
|
|
||||||
parsedOption = c.String("option")
|
|
||||||
firstArg = c.Args().First()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
app.Commands = []cli.Command{command}
|
|
||||||
|
|
||||||
app.Run([]string{"", "cmd", "my-arg", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"})
|
|
||||||
|
|
||||||
IntsEquals := func(a, b []int) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i, v := range a {
|
|
||||||
if v != b[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
StrsEquals := func(a, b []string) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i, v := range a {
|
|
||||||
if v != b[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
var expectedIntSlice = []int{22, 80}
|
|
||||||
var expectedStringSlice = []string{"8.8.8.8", "8.8.4.4"}
|
|
||||||
|
|
||||||
if !IntsEquals(parsedIntSlice, expectedIntSlice) {
|
|
||||||
t.Errorf("%v does not match %v", parsedIntSlice, expectedIntSlice)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !StrsEquals(parsedStringSlice, expectedStringSlice) {
|
|
||||||
t.Errorf("%v does not match %v", parsedStringSlice, expectedStringSlice)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_DefaultStdout(t *testing.T) {
|
|
||||||
app := cli.NewApp()
|
|
||||||
|
|
||||||
if app.Writer != os.Stdout {
|
|
||||||
t.Error("Default output writer not set.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockWriter struct {
|
|
||||||
written []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *mockWriter) Write(p []byte) (n int, err error) {
|
|
||||||
if fw.written == nil {
|
|
||||||
fw.written = p
|
|
||||||
} else {
|
|
||||||
fw.written = append(fw.written, p...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *mockWriter) GetWritten() (b []byte) {
|
|
||||||
return fw.written
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_SetStdout(t *testing.T) {
|
|
||||||
w := &mockWriter{}
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Name = "test"
|
|
||||||
app.Writer = w
|
|
||||||
|
|
||||||
err := app.Run([]string{"help"})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Run error: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(w.written) == 0 {
|
|
||||||
t.Error("App did not write output to desired writer.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_BeforeFunc(t *testing.T) {
|
|
||||||
beforeRun, subcommandRun := false, false
|
|
||||||
beforeError := fmt.Errorf("fail")
|
|
||||||
var err error
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
|
|
||||||
app.Before = func(c *cli.Context) error {
|
|
||||||
beforeRun = true
|
|
||||||
s := c.String("opt")
|
|
||||||
if s == "fail" {
|
|
||||||
return beforeError
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
cli.Command{
|
|
||||||
Name: "sub",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
subcommandRun = true
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Flags = []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "opt"},
|
|
||||||
}
|
|
||||||
|
|
||||||
// run with the Before() func succeeding
|
|
||||||
err = app.Run([]string{"command", "--opt", "succeed", "sub"})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Run error: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if beforeRun == false {
|
|
||||||
t.Errorf("Before() not executed when expected")
|
|
||||||
}
|
|
||||||
|
|
||||||
if subcommandRun == false {
|
|
||||||
t.Errorf("Subcommand not executed when expected")
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset
|
|
||||||
beforeRun, subcommandRun = false, false
|
|
||||||
|
|
||||||
// run with the Before() func failing
|
|
||||||
err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
|
||||||
|
|
||||||
// should be the same error produced by the Before func
|
|
||||||
if err != beforeError {
|
|
||||||
t.Errorf("Run error expected, but not received")
|
|
||||||
}
|
|
||||||
|
|
||||||
if beforeRun == false {
|
|
||||||
t.Errorf("Before() not executed when expected")
|
|
||||||
}
|
|
||||||
|
|
||||||
if subcommandRun == true {
|
|
||||||
t.Errorf("Subcommand executed when NOT expected")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApp_AfterFunc(t *testing.T) {
|
|
||||||
afterRun, subcommandRun := false, false
|
|
||||||
afterError := fmt.Errorf("fail")
|
|
||||||
var err error
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
|
|
||||||
app.After = func(c *cli.Context) error {
|
|
||||||
afterRun = true
|
|
||||||
s := c.String("opt")
|
|
||||||
if s == "fail" {
|
|
||||||
return afterError
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
cli.Command{
|
|
||||||
Name: "sub",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
subcommandRun = true
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Flags = []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "opt"},
|
|
||||||
}
|
|
||||||
|
|
||||||
// run with the After() func succeeding
|
|
||||||
err = app.Run([]string{"command", "--opt", "succeed", "sub"})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Run error: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if afterRun == false {
|
|
||||||
t.Errorf("After() not executed when expected")
|
|
||||||
}
|
|
||||||
|
|
||||||
if subcommandRun == false {
|
|
||||||
t.Errorf("Subcommand not executed when expected")
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset
|
|
||||||
afterRun, subcommandRun = false, false
|
|
||||||
|
|
||||||
// run with the Before() func failing
|
|
||||||
err = app.Run([]string{"command", "--opt", "fail", "sub"})
|
|
||||||
|
|
||||||
// should be the same error produced by the Before func
|
|
||||||
if err != afterError {
|
|
||||||
t.Errorf("Run error expected, but not received")
|
|
||||||
}
|
|
||||||
|
|
||||||
if afterRun == false {
|
|
||||||
t.Errorf("After() not executed when expected")
|
|
||||||
}
|
|
||||||
|
|
||||||
if subcommandRun == false {
|
|
||||||
t.Errorf("Subcommand not executed when expected")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAppNoHelpFlag(t *testing.T) {
|
|
||||||
oldFlag := cli.HelpFlag
|
|
||||||
defer func() {
|
|
||||||
cli.HelpFlag = oldFlag
|
|
||||||
}()
|
|
||||||
|
|
||||||
cli.HelpFlag = cli.BoolFlag{}
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
err := app.Run([]string{"test", "-h"})
|
|
||||||
|
|
||||||
if err != flag.ErrHelp {
|
|
||||||
t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAppHelpPrinter(t *testing.T) {
|
|
||||||
oldPrinter := cli.HelpPrinter
|
|
||||||
defer func() {
|
|
||||||
cli.HelpPrinter = oldPrinter
|
|
||||||
}()
|
|
||||||
|
|
||||||
var wasCalled = false
|
|
||||||
cli.HelpPrinter = func(template string, data interface{}) {
|
|
||||||
wasCalled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Run([]string{"-h"})
|
|
||||||
|
|
||||||
if wasCalled == false {
|
|
||||||
t.Errorf("Help printer expected to be called, but was not")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAppVersionPrinter(t *testing.T) {
|
|
||||||
oldPrinter := cli.VersionPrinter
|
|
||||||
defer func() {
|
|
||||||
cli.VersionPrinter = oldPrinter
|
|
||||||
}()
|
|
||||||
|
|
||||||
var wasCalled = false
|
|
||||||
cli.VersionPrinter = func(c *cli.Context) {
|
|
||||||
wasCalled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
app := cli.NewApp()
|
|
||||||
ctx := cli.NewContext(app, nil, nil)
|
|
||||||
cli.ShowVersion(ctx)
|
|
||||||
|
|
||||||
if wasCalled == false {
|
|
||||||
t.Errorf("Version printer expected to be called, but was not")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAppCommandNotFound(t *testing.T) {
|
|
||||||
beforeRun, subcommandRun := false, false
|
|
||||||
app := cli.NewApp()
|
|
||||||
|
|
||||||
app.CommandNotFound = func(c *cli.Context, command string) {
|
|
||||||
beforeRun = true
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
cli.Command{
|
|
||||||
Name: "bar",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
subcommandRun = true
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Run([]string{"command", "foo"})
|
|
||||||
|
|
||||||
expect(t, beforeRun, true)
|
|
||||||
expect(t, subcommandRun, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGlobalFlagsInSubcommands(t *testing.T) {
|
|
||||||
subcommandRun := false
|
|
||||||
app := cli.NewApp()
|
|
||||||
|
|
||||||
app.Flags = []cli.Flag{
|
|
||||||
cli.BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
cli.Command{
|
|
||||||
Name: "foo",
|
|
||||||
Subcommands: []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "bar",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
if c.GlobalBool("debug") {
|
|
||||||
subcommandRun = true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Run([]string{"command", "-d", "foo", "bar"})
|
|
||||||
|
|
||||||
expect(t, subcommandRun, true)
|
|
||||||
}
|
|
2
Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/bash_autocomplete
generated
vendored
2
Godeps/_workspace/src/github.com/codegangsta/cli/autocomplete/bash_autocomplete
generated
vendored
|
@ -1,5 +1,7 @@
|
||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
|
|
||||||
|
: ${PROG:=$(basename ${BASH_SOURCE})}
|
||||||
|
|
||||||
_cli_bash_autocomplete() {
|
_cli_bash_autocomplete() {
|
||||||
local cur prev opts base
|
local cur prev opts base
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
|
|
|
@ -17,3 +17,24 @@
|
||||||
// app.Run(os.Args)
|
// app.Run(os.Args)
|
||||||
// }
|
// }
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MultiError struct {
|
||||||
|
Errors []error
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMultiError(err ...error) MultiError {
|
||||||
|
return MultiError{Errors: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
|
@ -1,100 +0,0 @@
|
||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Example() {
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Name = "todo"
|
|
||||||
app.Usage = "task list on the command line"
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "add",
|
|
||||||
Aliases: []string{"a"},
|
|
||||||
Usage: "add a task to the list",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
println("added task: ", c.Args().First())
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "complete",
|
|
||||||
Aliases: []string{"c"},
|
|
||||||
Usage: "complete a task on the list",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
println("completed task: ", c.Args().First())
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Run(os.Args)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleSubcommand() {
|
|
||||||
app := cli.NewApp()
|
|
||||||
app.Name = "say"
|
|
||||||
app.Commands = []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "hello",
|
|
||||||
Aliases: []string{"hi"},
|
|
||||||
Usage: "use it to see a description",
|
|
||||||
Description: "This is how we describe hello the function",
|
|
||||||
Subcommands: []cli.Command{
|
|
||||||
{
|
|
||||||
Name: "english",
|
|
||||||
Aliases: []string{"en"},
|
|
||||||
Usage: "sends a greeting in english",
|
|
||||||
Description: "greets someone in english",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "name",
|
|
||||||
Value: "Bob",
|
|
||||||
Usage: "Name of the person to greet",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
println("Hello, ", c.String("name"))
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
Name: "spanish",
|
|
||||||
Aliases: []string{"sp"},
|
|
||||||
Usage: "sends a greeting in spanish",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "surname",
|
|
||||||
Value: "Jones",
|
|
||||||
Usage: "Surname of the person to greet",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
println("Hola, ", c.String("surname"))
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
Name: "french",
|
|
||||||
Aliases: []string{"fr"},
|
|
||||||
Usage: "sends a greeting in french",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "nickname",
|
|
||||||
Value: "Stevie",
|
|
||||||
Usage: "Nickname of the person to greet",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
println("Bonjour, ", c.String("nickname"))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
Name: "bye",
|
|
||||||
Usage: "says goodbye",
|
|
||||||
Action: func(c *cli.Context) {
|
|
||||||
println("bye")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Run(os.Args)
|
|
||||||
}
|
|
|
@ -18,6 +18,8 @@ type Command struct {
|
||||||
Usage string
|
Usage string
|
||||||
// A longer explanation of how the command works
|
// A longer explanation of how the command works
|
||||||
Description string
|
Description string
|
||||||
|
// A short description of the arguments of this command
|
||||||
|
ArgsUsage string
|
||||||
// The function to call when checking for bash command completions
|
// The function to call when checking for bash command completions
|
||||||
BashComplete func(context *Context)
|
BashComplete func(context *Context)
|
||||||
// An action to execute before any sub-subcommands are run, but after the context is ready
|
// An action to execute before any sub-subcommands are run, but after the context is ready
|
||||||
|
@ -36,11 +38,23 @@ type Command struct {
|
||||||
SkipFlagParsing bool
|
SkipFlagParsing bool
|
||||||
// Boolean to hide built-in help command
|
// Boolean to hide built-in help command
|
||||||
HideHelp bool
|
HideHelp bool
|
||||||
|
|
||||||
|
// Full name of command for help, defaults to full command name, including parent commands.
|
||||||
|
HelpName string
|
||||||
|
commandNamePath []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the full name of the command.
|
||||||
|
// For subcommands this ensures that parent commands are part of the command path
|
||||||
|
func (c Command) FullName() string {
|
||||||
|
if c.commandNamePath == nil {
|
||||||
|
return c.Name
|
||||||
|
}
|
||||||
|
return strings.Join(c.commandNamePath, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
||||||
func (c Command) Run(ctx *Context) error {
|
func (c Command) Run(ctx *Context) error {
|
||||||
|
|
||||||
if len(c.Subcommands) > 0 || c.Before != nil || c.After != nil {
|
if len(c.Subcommands) > 0 || c.Before != nil || c.After != nil {
|
||||||
return c.startApp(ctx)
|
return c.startApp(ctx)
|
||||||
}
|
}
|
||||||
|
@ -91,9 +105,9 @@ func (c Command) Run(ctx *Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprint(ctx.App.Writer, "Incorrect Usage.\n\n")
|
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
|
||||||
ShowCommandHelp(ctx, c.Name)
|
|
||||||
fmt.Fprintln(ctx.App.Writer)
|
fmt.Fprintln(ctx.App.Writer)
|
||||||
|
ShowCommandHelp(ctx, c.Name)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,10 +116,9 @@ func (c Command) Run(ctx *Context) error {
|
||||||
fmt.Fprintln(ctx.App.Writer, nerr)
|
fmt.Fprintln(ctx.App.Writer, nerr)
|
||||||
fmt.Fprintln(ctx.App.Writer)
|
fmt.Fprintln(ctx.App.Writer)
|
||||||
ShowCommandHelp(ctx, c.Name)
|
ShowCommandHelp(ctx, c.Name)
|
||||||
fmt.Fprintln(ctx.App.Writer)
|
|
||||||
return nerr
|
return nerr
|
||||||
}
|
}
|
||||||
context := NewContext(ctx.App, set, ctx.globalSet)
|
context := NewContext(ctx.App, set, ctx)
|
||||||
|
|
||||||
if checkCommandCompletions(context, c.Name) {
|
if checkCommandCompletions(context, c.Name) {
|
||||||
return nil
|
return nil
|
||||||
|
@ -144,6 +157,12 @@ func (c Command) startApp(ctx *Context) error {
|
||||||
|
|
||||||
// set the name and usage
|
// set the name and usage
|
||||||
app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
|
app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
|
||||||
|
if c.HelpName == "" {
|
||||||
|
app.HelpName = c.HelpName
|
||||||
|
} else {
|
||||||
|
app.HelpName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
|
||||||
|
}
|
||||||
|
|
||||||
if c.Description != "" {
|
if c.Description != "" {
|
||||||
app.Usage = c.Description
|
app.Usage = c.Description
|
||||||
} else {
|
} else {
|
||||||
|
@ -158,6 +177,13 @@ func (c Command) startApp(ctx *Context) error {
|
||||||
app.Flags = c.Flags
|
app.Flags = c.Flags
|
||||||
app.HideHelp = c.HideHelp
|
app.HideHelp = c.HideHelp
|
||||||
|
|
||||||
|
app.Version = ctx.App.Version
|
||||||
|
app.HideVersion = ctx.App.HideVersion
|
||||||
|
app.Compiled = ctx.App.Compiled
|
||||||
|
app.Author = ctx.App.Author
|
||||||
|
app.Email = ctx.App.Email
|
||||||
|
app.Writer = ctx.App.Writer
|
||||||
|
|
||||||
// bash completion
|
// bash completion
|
||||||
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
||||||
if c.BashComplete != nil {
|
if c.BashComplete != nil {
|
||||||
|
@ -173,5 +199,12 @@ func (c Command) startApp(ctx *Context) error {
|
||||||
app.Action = helpSubcommand.Action
|
app.Action = helpSubcommand.Action
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var newCmds []Command
|
||||||
|
for _, cc := range app.Commands {
|
||||||
|
cc.commandNamePath = []string{c.Name, cc.Name}
|
||||||
|
newCmds = append(newCmds, cc)
|
||||||
|
}
|
||||||
|
app.Commands = newCmds
|
||||||
|
|
||||||
return app.RunAsSubcommand(ctx)
|
return app.RunAsSubcommand(ctx)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCommandDoNotIgnoreFlags(t *testing.T) {
|
|
||||||
app := cli.NewApp()
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
test := []string{"blah", "blah", "-break"}
|
|
||||||
set.Parse(test)
|
|
||||||
|
|
||||||
c := cli.NewContext(app, set, set)
|
|
||||||
|
|
||||||
command := cli.Command{
|
|
||||||
Name: "test-cmd",
|
|
||||||
Aliases: []string{"tc"},
|
|
||||||
Usage: "this is for testing",
|
|
||||||
Description: "testing",
|
|
||||||
Action: func(_ *cli.Context) {},
|
|
||||||
}
|
|
||||||
err := command.Run(c)
|
|
||||||
|
|
||||||
expect(t, err.Error(), "flag provided but not defined: -break")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCommandIgnoreFlags(t *testing.T) {
|
|
||||||
app := cli.NewApp()
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
test := []string{"blah", "blah"}
|
|
||||||
set.Parse(test)
|
|
||||||
|
|
||||||
c := cli.NewContext(app, set, set)
|
|
||||||
|
|
||||||
command := cli.Command{
|
|
||||||
Name: "test-cmd",
|
|
||||||
Aliases: []string{"tc"},
|
|
||||||
Usage: "this is for testing",
|
|
||||||
Description: "testing",
|
|
||||||
Action: func(_ *cli.Context) {},
|
|
||||||
SkipFlagParsing: true,
|
|
||||||
}
|
|
||||||
err := command.Run(c)
|
|
||||||
|
|
||||||
expect(t, err, nil)
|
|
||||||
}
|
|
|
@ -16,14 +16,14 @@ type Context struct {
|
||||||
App *App
|
App *App
|
||||||
Command Command
|
Command Command
|
||||||
flagSet *flag.FlagSet
|
flagSet *flag.FlagSet
|
||||||
globalSet *flag.FlagSet
|
|
||||||
setFlags map[string]bool
|
setFlags map[string]bool
|
||||||
globalSetFlags map[string]bool
|
globalSetFlags map[string]bool
|
||||||
|
parentContext *Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new context. For use in when invoking an App or Command action.
|
// Creates a new context. For use in when invoking an App or Command action.
|
||||||
func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
|
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
||||||
return &Context{App: app, flagSet: set, globalSet: globalSet}
|
return &Context{App: app, flagSet: set, parentContext: parentCtx}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a local int flag, returns 0 if no int flag exists
|
// Looks up the value of a local int flag, returns 0 if no int flag exists
|
||||||
|
@ -73,37 +73,58 @@ func (c *Context) Generic(name string) interface{} {
|
||||||
|
|
||||||
// Looks up the value of a global int flag, returns 0 if no int flag exists
|
// Looks up the value of a global int flag, returns 0 if no int flag exists
|
||||||
func (c *Context) GlobalInt(name string) int {
|
func (c *Context) GlobalInt(name string) int {
|
||||||
return lookupInt(name, c.globalSet)
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupInt(name, fs)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
|
// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
|
||||||
func (c *Context) GlobalDuration(name string) time.Duration {
|
func (c *Context) GlobalDuration(name string) time.Duration {
|
||||||
return lookupDuration(name, c.globalSet)
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupDuration(name, fs)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a global bool flag, returns false if no bool flag exists
|
// Looks up the value of a global bool flag, returns false if no bool flag exists
|
||||||
func (c *Context) GlobalBool(name string) bool {
|
func (c *Context) GlobalBool(name string) bool {
|
||||||
return lookupBool(name, c.globalSet)
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupBool(name, fs)
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a global string flag, returns "" if no string flag exists
|
// Looks up the value of a global string flag, returns "" if no string flag exists
|
||||||
func (c *Context) GlobalString(name string) string {
|
func (c *Context) GlobalString(name string) string {
|
||||||
return lookupString(name, c.globalSet)
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupString(name, fs)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a global string slice flag, returns nil if no string slice flag exists
|
// Looks up the value of a global string slice flag, returns nil if no string slice flag exists
|
||||||
func (c *Context) GlobalStringSlice(name string) []string {
|
func (c *Context) GlobalStringSlice(name string) []string {
|
||||||
return lookupStringSlice(name, c.globalSet)
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupStringSlice(name, fs)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a global int slice flag, returns nil if no int slice flag exists
|
// Looks up the value of a global int slice flag, returns nil if no int slice flag exists
|
||||||
func (c *Context) GlobalIntSlice(name string) []int {
|
func (c *Context) GlobalIntSlice(name string) []int {
|
||||||
return lookupIntSlice(name, c.globalSet)
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupIntSlice(name, fs)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a global generic flag, returns nil if no generic flag exists
|
// Looks up the value of a global generic flag, returns nil if no generic flag exists
|
||||||
func (c *Context) GlobalGeneric(name string) interface{} {
|
func (c *Context) GlobalGeneric(name string) interface{} {
|
||||||
return lookupGeneric(name, c.globalSet)
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupGeneric(name, fs)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of flags set
|
// Returns the number of flags set
|
||||||
|
@ -126,11 +147,17 @@ func (c *Context) IsSet(name string) bool {
|
||||||
func (c *Context) GlobalIsSet(name string) bool {
|
func (c *Context) GlobalIsSet(name string) bool {
|
||||||
if c.globalSetFlags == nil {
|
if c.globalSetFlags == nil {
|
||||||
c.globalSetFlags = make(map[string]bool)
|
c.globalSetFlags = make(map[string]bool)
|
||||||
c.globalSet.Visit(func(f *flag.Flag) {
|
ctx := c
|
||||||
c.globalSetFlags[f.Name] = true
|
if ctx.parentContext != nil {
|
||||||
})
|
ctx = ctx.parentContext
|
||||||
|
}
|
||||||
|
for ; ctx != nil && c.globalSetFlags[name] == false; ctx = ctx.parentContext {
|
||||||
|
ctx.flagSet.Visit(func(f *flag.Flag) {
|
||||||
|
c.globalSetFlags[f.Name] = true
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return c.globalSetFlags[name] == true
|
return c.globalSetFlags[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a slice of flag names used in this context.
|
// Returns a slice of flag names used in this context.
|
||||||
|
@ -157,6 +184,11 @@ func (c *Context) GlobalFlagNames() (names []string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the parent context, if any
|
||||||
|
func (c *Context) Parent() *Context {
|
||||||
|
return c.parentContext
|
||||||
|
}
|
||||||
|
|
||||||
type Args []string
|
type Args []string
|
||||||
|
|
||||||
// Returns the command line arguments associated with the context.
|
// Returns the command line arguments associated with the context.
|
||||||
|
@ -201,6 +233,18 @@ func (a Args) Swap(from, to int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
|
||||||
|
if ctx.parentContext != nil {
|
||||||
|
ctx = ctx.parentContext
|
||||||
|
}
|
||||||
|
for ; ctx != nil; ctx = ctx.parentContext {
|
||||||
|
if f := ctx.flagSet.Lookup(name); f != nil {
|
||||||
|
return ctx.flagSet
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func lookupInt(name string, set *flag.FlagSet) int {
|
func lookupInt(name string, set *flag.FlagSet) int {
|
||||||
f := set.Lookup(name)
|
f := set.Lookup(name)
|
||||||
if f != nil {
|
if f != nil {
|
||||||
|
|
|
@ -1,111 +0,0 @@
|
||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNewContext(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Int("myflag", 12, "doc")
|
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
|
||||||
globalSet.Int("myflag", 42, "doc")
|
|
||||||
command := cli.Command{Name: "mycommand"}
|
|
||||||
c := cli.NewContext(nil, set, globalSet)
|
|
||||||
c.Command = command
|
|
||||||
expect(t, c.Int("myflag"), 12)
|
|
||||||
expect(t, c.GlobalInt("myflag"), 42)
|
|
||||||
expect(t, c.Command.Name, "mycommand")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_Int(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Int("myflag", 12, "doc")
|
|
||||||
c := cli.NewContext(nil, set, set)
|
|
||||||
expect(t, c.Int("myflag"), 12)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_Duration(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Duration("myflag", time.Duration(12*time.Second), "doc")
|
|
||||||
c := cli.NewContext(nil, set, set)
|
|
||||||
expect(t, c.Duration("myflag"), time.Duration(12*time.Second))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_String(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.String("myflag", "hello world", "doc")
|
|
||||||
c := cli.NewContext(nil, set, set)
|
|
||||||
expect(t, c.String("myflag"), "hello world")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_Bool(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", false, "doc")
|
|
||||||
c := cli.NewContext(nil, set, set)
|
|
||||||
expect(t, c.Bool("myflag"), false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_BoolT(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", true, "doc")
|
|
||||||
c := cli.NewContext(nil, set, set)
|
|
||||||
expect(t, c.BoolT("myflag"), true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_Args(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", false, "doc")
|
|
||||||
c := cli.NewContext(nil, set, set)
|
|
||||||
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
||||||
expect(t, len(c.Args()), 2)
|
|
||||||
expect(t, c.Bool("myflag"), true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_IsSet(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", false, "doc")
|
|
||||||
set.String("otherflag", "hello world", "doc")
|
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
|
||||||
globalSet.Bool("myflagGlobal", true, "doc")
|
|
||||||
c := cli.NewContext(nil, set, globalSet)
|
|
||||||
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
||||||
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
|
||||||
expect(t, c.IsSet("myflag"), true)
|
|
||||||
expect(t, c.IsSet("otherflag"), false)
|
|
||||||
expect(t, c.IsSet("bogusflag"), false)
|
|
||||||
expect(t, c.IsSet("myflagGlobal"), false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_GlobalIsSet(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", false, "doc")
|
|
||||||
set.String("otherflag", "hello world", "doc")
|
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
|
||||||
globalSet.Bool("myflagGlobal", true, "doc")
|
|
||||||
globalSet.Bool("myflagGlobalUnset", true, "doc")
|
|
||||||
c := cli.NewContext(nil, set, globalSet)
|
|
||||||
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
||||||
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
|
||||||
expect(t, c.GlobalIsSet("myflag"), false)
|
|
||||||
expect(t, c.GlobalIsSet("otherflag"), false)
|
|
||||||
expect(t, c.GlobalIsSet("bogusflag"), false)
|
|
||||||
expect(t, c.GlobalIsSet("myflagGlobal"), true)
|
|
||||||
expect(t, c.GlobalIsSet("myflagGlobalUnset"), false)
|
|
||||||
expect(t, c.GlobalIsSet("bogusGlobal"), false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_NumFlags(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", false, "doc")
|
|
||||||
set.String("otherflag", "hello world", "doc")
|
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
|
||||||
globalSet.Bool("myflagGlobal", true, "doc")
|
|
||||||
c := cli.NewContext(nil, set, globalSet)
|
|
||||||
set.Parse([]string{"--myflag", "--otherflag=foo"})
|
|
||||||
globalSet.Parse([]string{"--myflagGlobal"})
|
|
||||||
expect(t, c.NumFlags(), 2)
|
|
||||||
}
|
|
|
@ -99,21 +99,27 @@ func (f GenericFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringSlice is an opaque type for []string to satisfy flag.Value
|
||||||
type StringSlice []string
|
type StringSlice []string
|
||||||
|
|
||||||
|
// Set appends the string value to the list of values
|
||||||
func (f *StringSlice) Set(value string) error {
|
func (f *StringSlice) Set(value string) error {
|
||||||
*f = append(*f, value)
|
*f = append(*f, value)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f *StringSlice) String() string {
|
func (f *StringSlice) String() string {
|
||||||
return fmt.Sprintf("%s", *f)
|
return fmt.Sprintf("%s", *f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Value returns the slice of strings set by this flag
|
||||||
func (f *StringSlice) Value() []string {
|
func (f *StringSlice) Value() []string {
|
||||||
return *f
|
return *f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringSlice is a string flag that can be specified multiple times on the
|
||||||
|
// command-line
|
||||||
type StringSliceFlag struct {
|
type StringSliceFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value *StringSlice
|
Value *StringSlice
|
||||||
|
@ -121,12 +127,14 @@ type StringSliceFlag struct {
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the usage
|
||||||
func (f StringSliceFlag) String() string {
|
func (f StringSliceFlag) String() string {
|
||||||
firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ")
|
firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ")
|
||||||
pref := prefixFor(firstName)
|
pref := prefixFor(firstName)
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f StringSliceFlag) Apply(set *flag.FlagSet) {
|
func (f StringSliceFlag) Apply(set *flag.FlagSet) {
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
||||||
|
@ -144,6 +152,9 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) {
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
eachName(f.Name, func(name string) {
|
||||||
|
if f.Value == nil {
|
||||||
|
f.Value = &StringSlice{}
|
||||||
|
}
|
||||||
set.Var(f.Value, name, f.Usage)
|
set.Var(f.Value, name, f.Usage)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -152,10 +163,11 @@ func (f StringSliceFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringSlice is an opaque type for []int to satisfy flag.Value
|
||||||
type IntSlice []int
|
type IntSlice []int
|
||||||
|
|
||||||
|
// Set parses the value into an integer and appends it to the list of values
|
||||||
func (f *IntSlice) Set(value string) error {
|
func (f *IntSlice) Set(value string) error {
|
||||||
|
|
||||||
tmp, err := strconv.Atoi(value)
|
tmp, err := strconv.Atoi(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -165,14 +177,18 @@ func (f *IntSlice) Set(value string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f *IntSlice) String() string {
|
func (f *IntSlice) String() string {
|
||||||
return fmt.Sprintf("%d", *f)
|
return fmt.Sprintf("%d", *f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Value returns the slice of ints set by this flag
|
||||||
func (f *IntSlice) Value() []int {
|
func (f *IntSlice) Value() []int {
|
||||||
return *f
|
return *f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IntSliceFlag is an int flag that can be specified multiple times on the
|
||||||
|
// command-line
|
||||||
type IntSliceFlag struct {
|
type IntSliceFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value *IntSlice
|
Value *IntSlice
|
||||||
|
@ -180,12 +196,14 @@ type IntSliceFlag struct {
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the usage
|
||||||
func (f IntSliceFlag) String() string {
|
func (f IntSliceFlag) String() string {
|
||||||
firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ")
|
firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ")
|
||||||
pref := prefixFor(firstName)
|
pref := prefixFor(firstName)
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
|
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
||||||
|
@ -206,6 +224,9 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) {
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
eachName(f.Name, func(name string) {
|
||||||
|
if f.Value == nil {
|
||||||
|
f.Value = &IntSlice{}
|
||||||
|
}
|
||||||
set.Var(f.Value, name, f.Usage)
|
set.Var(f.Value, name, f.Usage)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -214,16 +235,19 @@ func (f IntSliceFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BoolFlag is a switch that defaults to false
|
||||||
type BoolFlag struct {
|
type BoolFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f BoolFlag) String() string {
|
func (f BoolFlag) String() string {
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f BoolFlag) Apply(set *flag.FlagSet) {
|
func (f BoolFlag) Apply(set *flag.FlagSet) {
|
||||||
val := false
|
val := false
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
|
@ -248,16 +272,20 @@ func (f BoolFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BoolTFlag this represents a boolean flag that is true by default, but can
|
||||||
|
// still be set to false by --some-flag=false
|
||||||
type BoolTFlag struct {
|
type BoolTFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f BoolTFlag) String() string {
|
func (f BoolTFlag) String() string {
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f BoolTFlag) Apply(set *flag.FlagSet) {
|
func (f BoolTFlag) Apply(set *flag.FlagSet) {
|
||||||
val := true
|
val := true
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
|
@ -282,6 +310,7 @@ func (f BoolTFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringFlag represents a flag that takes as string value
|
||||||
type StringFlag struct {
|
type StringFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value string
|
Value string
|
||||||
|
@ -289,6 +318,7 @@ type StringFlag struct {
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the usage
|
||||||
func (f StringFlag) String() string {
|
func (f StringFlag) String() string {
|
||||||
var fmtString string
|
var fmtString string
|
||||||
fmtString = "%s %v\t%v"
|
fmtString = "%s %v\t%v"
|
||||||
|
@ -302,6 +332,7 @@ func (f StringFlag) String() string {
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f StringFlag) Apply(set *flag.FlagSet) {
|
func (f StringFlag) Apply(set *flag.FlagSet) {
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
||||||
|
@ -322,6 +353,8 @@ func (f StringFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IntFlag is a flag that takes an integer
|
||||||
|
// Errors if the value provided cannot be parsed
|
||||||
type IntFlag struct {
|
type IntFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value int
|
Value int
|
||||||
|
@ -329,10 +362,12 @@ type IntFlag struct {
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the usage
|
||||||
func (f IntFlag) String() string {
|
func (f IntFlag) String() string {
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f IntFlag) Apply(set *flag.FlagSet) {
|
func (f IntFlag) Apply(set *flag.FlagSet) {
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
||||||
|
@ -356,6 +391,8 @@ func (f IntFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DurationFlag is a flag that takes a duration specified in Go's duration
|
||||||
|
// format: https://golang.org/pkg/time/#ParseDuration
|
||||||
type DurationFlag struct {
|
type DurationFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value time.Duration
|
Value time.Duration
|
||||||
|
@ -363,10 +400,12 @@ type DurationFlag struct {
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f DurationFlag) String() string {
|
func (f DurationFlag) String() string {
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f DurationFlag) Apply(set *flag.FlagSet) {
|
func (f DurationFlag) Apply(set *flag.FlagSet) {
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
||||||
|
@ -390,6 +429,8 @@ func (f DurationFlag) getName() string {
|
||||||
return f.Name
|
return f.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Float64Flag is a flag that takes an float value
|
||||||
|
// Errors if the value provided cannot be parsed
|
||||||
type Float64Flag struct {
|
type Float64Flag struct {
|
||||||
Name string
|
Name string
|
||||||
Value float64
|
Value float64
|
||||||
|
@ -397,10 +438,12 @@ type Float64Flag struct {
|
||||||
EnvVar string
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the usage
|
||||||
func (f Float64Flag) String() string {
|
func (f Float64Flag) String() string {
|
||||||
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
func (f Float64Flag) Apply(set *flag.FlagSet) {
|
func (f Float64Flag) Apply(set *flag.FlagSet) {
|
||||||
if f.EnvVar != "" {
|
if f.EnvVar != "" {
|
||||||
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
||||||
|
|
|
@ -1,742 +0,0 @@
|
||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
var boolFlagTests = []struct {
|
|
||||||
name string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"help", "--help\t"},
|
|
||||||
{"h", "-h\t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBoolFlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range boolFlagTests {
|
|
||||||
flag := cli.BoolFlag{Name: test.name}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%s does not match %s", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var stringFlagTests = []struct {
|
|
||||||
name string
|
|
||||||
value string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"help", "", "--help \t"},
|
|
||||||
{"h", "", "-h \t"},
|
|
||||||
{"h", "", "-h \t"},
|
|
||||||
{"test", "Something", "--test \"Something\"\t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStringFlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range stringFlagTests {
|
|
||||||
flag := cli.StringFlag{Name: test.name, Value: test.value}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%s does not match %s", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStringFlagWithEnvVarHelpOutput(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_FOO", "derp")
|
|
||||||
for _, test := range stringFlagTests {
|
|
||||||
flag := cli.StringFlag{Name: test.name, Value: test.value, EnvVar: "APP_FOO"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if !strings.HasSuffix(output, " [$APP_FOO]") {
|
|
||||||
t.Errorf("%s does not end with [$APP_FOO]", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var stringSliceFlagTests = []struct {
|
|
||||||
name string
|
|
||||||
value *cli.StringSlice
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"help", func() *cli.StringSlice {
|
|
||||||
s := &cli.StringSlice{}
|
|
||||||
s.Set("")
|
|
||||||
return s
|
|
||||||
}(), "--help [--help option --help option]\t"},
|
|
||||||
{"h", func() *cli.StringSlice {
|
|
||||||
s := &cli.StringSlice{}
|
|
||||||
s.Set("")
|
|
||||||
return s
|
|
||||||
}(), "-h [-h option -h option]\t"},
|
|
||||||
{"h", func() *cli.StringSlice {
|
|
||||||
s := &cli.StringSlice{}
|
|
||||||
s.Set("")
|
|
||||||
return s
|
|
||||||
}(), "-h [-h option -h option]\t"},
|
|
||||||
{"test", func() *cli.StringSlice {
|
|
||||||
s := &cli.StringSlice{}
|
|
||||||
s.Set("Something")
|
|
||||||
return s
|
|
||||||
}(), "--test [--test option --test option]\t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStringSliceFlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range stringSliceFlagTests {
|
|
||||||
flag := cli.StringSliceFlag{Name: test.name, Value: test.value}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%q does not match %q", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStringSliceFlagWithEnvVarHelpOutput(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_QWWX", "11,4")
|
|
||||||
for _, test := range stringSliceFlagTests {
|
|
||||||
flag := cli.StringSliceFlag{Name: test.name, Value: test.value, EnvVar: "APP_QWWX"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if !strings.HasSuffix(output, " [$APP_QWWX]") {
|
|
||||||
t.Errorf("%q does not end with [$APP_QWWX]", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var intFlagTests = []struct {
|
|
||||||
name string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"help", "--help \"0\"\t"},
|
|
||||||
{"h", "-h \"0\"\t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIntFlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range intFlagTests {
|
|
||||||
flag := cli.IntFlag{Name: test.name}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%s does not match %s", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIntFlagWithEnvVarHelpOutput(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_BAR", "2")
|
|
||||||
for _, test := range intFlagTests {
|
|
||||||
flag := cli.IntFlag{Name: test.name, EnvVar: "APP_BAR"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if !strings.HasSuffix(output, " [$APP_BAR]") {
|
|
||||||
t.Errorf("%s does not end with [$APP_BAR]", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var durationFlagTests = []struct {
|
|
||||||
name string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"help", "--help \"0\"\t"},
|
|
||||||
{"h", "-h \"0\"\t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDurationFlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range durationFlagTests {
|
|
||||||
flag := cli.DurationFlag{Name: test.name}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%s does not match %s", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDurationFlagWithEnvVarHelpOutput(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_BAR", "2h3m6s")
|
|
||||||
for _, test := range durationFlagTests {
|
|
||||||
flag := cli.DurationFlag{Name: test.name, EnvVar: "APP_BAR"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if !strings.HasSuffix(output, " [$APP_BAR]") {
|
|
||||||
t.Errorf("%s does not end with [$APP_BAR]", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var intSliceFlagTests = []struct {
|
|
||||||
name string
|
|
||||||
value *cli.IntSlice
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"help", &cli.IntSlice{}, "--help [--help option --help option]\t"},
|
|
||||||
{"h", &cli.IntSlice{}, "-h [-h option -h option]\t"},
|
|
||||||
{"h", &cli.IntSlice{}, "-h [-h option -h option]\t"},
|
|
||||||
{"test", func() *cli.IntSlice {
|
|
||||||
i := &cli.IntSlice{}
|
|
||||||
i.Set("9")
|
|
||||||
return i
|
|
||||||
}(), "--test [--test option --test option]\t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIntSliceFlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range intSliceFlagTests {
|
|
||||||
flag := cli.IntSliceFlag{Name: test.name, Value: test.value}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%q does not match %q", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIntSliceFlagWithEnvVarHelpOutput(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_SMURF", "42,3")
|
|
||||||
for _, test := range intSliceFlagTests {
|
|
||||||
flag := cli.IntSliceFlag{Name: test.name, Value: test.value, EnvVar: "APP_SMURF"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if !strings.HasSuffix(output, " [$APP_SMURF]") {
|
|
||||||
t.Errorf("%q does not end with [$APP_SMURF]", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var float64FlagTests = []struct {
|
|
||||||
name string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"help", "--help \"0\"\t"},
|
|
||||||
{"h", "-h \"0\"\t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFloat64FlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range float64FlagTests {
|
|
||||||
flag := cli.Float64Flag{Name: test.name}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%s does not match %s", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFloat64FlagWithEnvVarHelpOutput(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_BAZ", "99.4")
|
|
||||||
for _, test := range float64FlagTests {
|
|
||||||
flag := cli.Float64Flag{Name: test.name, EnvVar: "APP_BAZ"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if !strings.HasSuffix(output, " [$APP_BAZ]") {
|
|
||||||
t.Errorf("%s does not end with [$APP_BAZ]", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var genericFlagTests = []struct {
|
|
||||||
name string
|
|
||||||
value cli.Generic
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{"test", &Parser{"abc", "def"}, "--test \"abc,def\"\ttest flag"},
|
|
||||||
{"t", &Parser{"abc", "def"}, "-t \"abc,def\"\ttest flag"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenericFlagHelpOutput(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range genericFlagTests {
|
|
||||||
flag := cli.GenericFlag{Name: test.name, Value: test.value, Usage: "test flag"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if output != test.expected {
|
|
||||||
t.Errorf("%q does not match %q", output, test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenericFlagWithEnvVarHelpOutput(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_ZAP", "3")
|
|
||||||
for _, test := range genericFlagTests {
|
|
||||||
flag := cli.GenericFlag{Name: test.name, EnvVar: "APP_ZAP"}
|
|
||||||
output := flag.String()
|
|
||||||
|
|
||||||
if !strings.HasSuffix(output, " [$APP_ZAP]") {
|
|
||||||
t.Errorf("%s does not end with [$APP_ZAP]", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiString(t *testing.T) {
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "serve, s"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.String("serve") != "10" {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.String("s") != "10" {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run", "-s", "10"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiStringFromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_COUNT", "20")
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "count, c", EnvVar: "APP_COUNT"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.String("count") != "20" {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.String("c") != "20" {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiStringFromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_COUNT", "20")
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{Name: "count, c", EnvVar: "COMPAT_COUNT,APP_COUNT"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.String("count") != "20" {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.String("c") != "20" {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiStringSlice(t *testing.T) {
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringSliceFlag{Name: "serve, s", Value: &cli.StringSlice{}},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run", "-s", "10", "-s", "20"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiStringSliceFromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
||||||
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringSliceFlag{Name: "intervals, i", Value: &cli.StringSlice{}, EnvVar: "APP_INTERVALS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiStringSliceFromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
||||||
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringSliceFlag{Name: "intervals, i", Value: &cli.StringSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiInt(t *testing.T) {
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntFlag{Name: "serve, s"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Int("serve") != 10 {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.Int("s") != 10 {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run", "-s", "10"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiIntFromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_TIMEOUT_SECONDS", "10")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntFlag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Int("timeout") != 10 {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.Int("t") != 10 {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiIntFromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_TIMEOUT_SECONDS", "10")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntFlag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Int("timeout") != 10 {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.Int("t") != 10 {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiIntSlice(t *testing.T) {
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntSliceFlag{Name: "serve, s", Value: &cli.IntSlice{}},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{10, 20}) {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.IntSlice("s"), []int{10, 20}) {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run", "-s", "10", "-s", "20"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiIntSliceFromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
||||||
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntSliceFlag{Name: "intervals, i", Value: &cli.IntSlice{}, EnvVar: "APP_INTERVALS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiIntSliceFromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
||||||
|
|
||||||
(&cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntSliceFlag{Name: "intervals, i", Value: &cli.IntSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}).Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiFloat64(t *testing.T) {
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.Float64Flag{Name: "serve, s"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Float64("serve") != 10.2 {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.Float64("s") != 10.2 {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run", "-s", "10.2"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiFloat64FromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Float64("timeout") != 15.5 {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.Float64("t") != 15.5 {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiFloat64FromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.Float64Flag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Float64("timeout") != 15.5 {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.Float64("t") != 15.5 {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiBool(t *testing.T) {
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.BoolFlag{Name: "serve, s"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Bool("serve") != true {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.Bool("s") != true {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run", "--serve"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiBoolFromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_DEBUG", "1")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.BoolFlag{Name: "debug, d", EnvVar: "APP_DEBUG"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Bool("debug") != true {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if ctx.Bool("d") != true {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiBoolFromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_DEBUG", "1")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.BoolFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.Bool("debug") != true {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if ctx.Bool("d") != true {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiBoolT(t *testing.T) {
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.BoolTFlag{Name: "serve, s"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.BoolT("serve") != true {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if ctx.BoolT("s") != true {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run", "--serve"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiBoolTFromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_DEBUG", "0")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.BoolTFlag{Name: "debug, d", EnvVar: "APP_DEBUG"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.BoolT("debug") != false {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if ctx.BoolT("d") != false {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMultiBoolTFromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_DEBUG", "0")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.BoolTFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if ctx.BoolT("debug") != false {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if ctx.BoolT("d") != false {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
type Parser [2]string
|
|
||||||
|
|
||||||
func (p *Parser) Set(value string) error {
|
|
||||||
parts := strings.Split(value, ",")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return fmt.Errorf("invalid format")
|
|
||||||
}
|
|
||||||
|
|
||||||
(*p)[0] = parts[0]
|
|
||||||
(*p)[1] = parts[1]
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) String() string {
|
|
||||||
return fmt.Sprintf("%s,%s", p[0], p[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseGeneric(t *testing.T) {
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.GenericFlag{Name: "serve, s", Value: &Parser{}},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"10", "20"}) {
|
|
||||||
t.Errorf("main name not set")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"10", "20"}) {
|
|
||||||
t.Errorf("short name not set")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run", "-s", "10,20"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseGenericFromEnv(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_SERVE", "20,30")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.GenericFlag{Name: "serve, s", Value: &Parser{}, EnvVar: "APP_SERVE"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"20", "30"}) {
|
|
||||||
t.Errorf("main name not set from env")
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"20", "30"}) {
|
|
||||||
t.Errorf("short name not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseGenericFromEnvCascade(t *testing.T) {
|
|
||||||
os.Clearenv()
|
|
||||||
os.Setenv("APP_FOO", "99,2000")
|
|
||||||
a := cli.App{
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.GenericFlag{Name: "foos", Value: &Parser{}, EnvVar: "COMPAT_FOO,APP_FOO"},
|
|
||||||
},
|
|
||||||
Action: func(ctx *cli.Context) {
|
|
||||||
if !reflect.DeepEqual(ctx.Generic("foos"), &Parser{"99", "2000"}) {
|
|
||||||
t.Errorf("value not set from env")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
a.Run([]string{"run"})
|
|
||||||
}
|
|
|
@ -1,6 +1,12 @@
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
|
"text/template"
|
||||||
|
)
|
||||||
|
|
||||||
// The text template for the Default help topic.
|
// The text template for the Default help topic.
|
||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
|
@ -9,30 +15,33 @@ var AppHelpTemplate = `NAME:
|
||||||
{{.Name}} - {{.Usage}}
|
{{.Name}} - {{.Usage}}
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
|
{{.HelpName}} {{if .Flags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
||||||
|
{{if .Version}}
|
||||||
VERSION:
|
VERSION:
|
||||||
{{.Version}}
|
{{.Version}}
|
||||||
|
{{end}}{{if len .Authors}}
|
||||||
AUTHOR(S):
|
AUTHOR(S):
|
||||||
{{range .Authors}}{{ . }}
|
{{range .Authors}}{{ . }}{{end}}
|
||||||
{{end}}
|
{{end}}{{if .Commands}}
|
||||||
COMMANDS:
|
COMMANDS:
|
||||||
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
||||||
{{end}}{{if .Flags}}
|
{{end}}{{end}}{{if .Flags}}
|
||||||
GLOBAL OPTIONS:
|
GLOBAL OPTIONS:
|
||||||
{{range .Flags}}{{.}}
|
{{range .Flags}}{{.}}
|
||||||
{{end}}{{end}}
|
{{end}}{{end}}{{if .Copyright }}
|
||||||
|
COPYRIGHT:
|
||||||
|
{{.Copyright}}
|
||||||
|
{{end}}
|
||||||
`
|
`
|
||||||
|
|
||||||
// The text template for the command help topic.
|
// The text template for the command help topic.
|
||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
var CommandHelpTemplate = `NAME:
|
var CommandHelpTemplate = `NAME:
|
||||||
{{.Name}} - {{.Usage}}
|
{{.HelpName}} - {{.Usage}}
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
command {{.Name}}{{if .Flags}} [command options]{{end}} [arguments...]{{if .Description}}
|
{{.HelpName}}{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Description}}
|
||||||
|
|
||||||
DESCRIPTION:
|
DESCRIPTION:
|
||||||
{{.Description}}{{end}}{{if .Flags}}
|
{{.Description}}{{end}}{{if .Flags}}
|
||||||
|
@ -46,10 +55,10 @@ OPTIONS:
|
||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
var SubcommandHelpTemplate = `NAME:
|
var SubcommandHelpTemplate = `NAME:
|
||||||
{{.Name}} - {{.Usage}}
|
{{.HelpName}} - {{.Usage}}
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
{{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...]
|
{{.HelpName}} command{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
||||||
|
|
||||||
COMMANDS:
|
COMMANDS:
|
||||||
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
||||||
|
@ -60,9 +69,10 @@ OPTIONS:
|
||||||
`
|
`
|
||||||
|
|
||||||
var helpCommand = Command{
|
var helpCommand = Command{
|
||||||
Name: "help",
|
Name: "help",
|
||||||
Aliases: []string{"h"},
|
Aliases: []string{"h"},
|
||||||
Usage: "Shows a list of commands or help for one command",
|
Usage: "Shows a list of commands or help for one command",
|
||||||
|
ArgsUsage: "[command]",
|
||||||
Action: func(c *Context) {
|
Action: func(c *Context) {
|
||||||
args := c.Args()
|
args := c.Args()
|
||||||
if args.Present() {
|
if args.Present() {
|
||||||
|
@ -74,9 +84,10 @@ var helpCommand = Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
var helpSubcommand = Command{
|
var helpSubcommand = Command{
|
||||||
Name: "help",
|
Name: "help",
|
||||||
Aliases: []string{"h"},
|
Aliases: []string{"h"},
|
||||||
Usage: "Shows a list of commands or help for one command",
|
Usage: "Shows a list of commands or help for one command",
|
||||||
|
ArgsUsage: "[command]",
|
||||||
Action: func(c *Context) {
|
Action: func(c *Context) {
|
||||||
args := c.Args()
|
args := c.Args()
|
||||||
if args.Present() {
|
if args.Present() {
|
||||||
|
@ -87,16 +98,16 @@ var helpSubcommand = Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints help for the App
|
// Prints help for the App or Command
|
||||||
type helpPrinter func(templ string, data interface{})
|
type helpPrinter func(w io.Writer, templ string, data interface{})
|
||||||
|
|
||||||
var HelpPrinter helpPrinter = nil
|
var HelpPrinter helpPrinter = printHelp
|
||||||
|
|
||||||
// Prints version for the App
|
// Prints version for the App
|
||||||
var VersionPrinter = printVersion
|
var VersionPrinter = printVersion
|
||||||
|
|
||||||
func ShowAppHelp(c *Context) {
|
func ShowAppHelp(c *Context) {
|
||||||
HelpPrinter(AppHelpTemplate, c.App)
|
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the list of subcommands as the default app completion method
|
// Prints the list of subcommands as the default app completion method
|
||||||
|
@ -109,24 +120,24 @@ func DefaultAppComplete(c *Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints help for the given command
|
// Prints help for the given command
|
||||||
func ShowCommandHelp(c *Context, command string) {
|
func ShowCommandHelp(ctx *Context, command string) {
|
||||||
// show the subcommand help for a command with subcommands
|
// show the subcommand help for a command with subcommands
|
||||||
if command == "" {
|
if command == "" {
|
||||||
HelpPrinter(SubcommandHelpTemplate, c.App)
|
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range c.App.Commands {
|
for _, c := range ctx.App.Commands {
|
||||||
if c.HasName(command) {
|
if c.HasName(command) {
|
||||||
HelpPrinter(CommandHelpTemplate, c)
|
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.App.CommandNotFound != nil {
|
if ctx.App.CommandNotFound != nil {
|
||||||
c.App.CommandNotFound(c, command)
|
ctx.App.CommandNotFound(ctx, command)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(c.App.Writer, "No help topic for '%v'\n", command)
|
fmt.Fprintf(ctx.App.Writer, "No help topic for '%v'\n", command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,22 +171,42 @@ func ShowCommandCompletions(ctx *Context, command string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkVersion(c *Context) bool {
|
func printHelp(out io.Writer, templ string, data interface{}) {
|
||||||
if c.GlobalBool("version") {
|
funcMap := template.FuncMap{
|
||||||
ShowVersion(c)
|
"join": strings.Join,
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0)
|
||||||
|
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
||||||
|
err := t.Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
w.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkVersion(c *Context) bool {
|
||||||
|
found := false
|
||||||
|
if VersionFlag.Name != "" {
|
||||||
|
eachName(VersionFlag.Name, func(name string) {
|
||||||
|
if c.GlobalBool(name) || c.Bool(name) {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkHelp(c *Context) bool {
|
func checkHelp(c *Context) bool {
|
||||||
if c.GlobalBool("h") || c.GlobalBool("help") {
|
found := false
|
||||||
ShowAppHelp(c)
|
if HelpFlag.Name != "" {
|
||||||
return true
|
eachName(HelpFlag.Name, func(name string) {
|
||||||
|
if c.GlobalBool(name) || c.Bool(name) {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
return found
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkCommandHelp(c *Context, name string) bool {
|
func checkCommandHelp(c *Context, name string) bool {
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
/* Test Helpers */
|
|
||||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
|
||||||
if a != b {
|
|
||||||
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func refute(t *testing.T, a interface{}, b interface{}) {
|
|
||||||
if a == b {
|
|
||||||
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -74,7 +74,7 @@ func init() {
|
||||||
{
|
{
|
||||||
Action: blockRecovery,
|
Action: blockRecovery,
|
||||||
Name: "recover",
|
Name: "recover",
|
||||||
Usage: "attempts to recover a corrupted database by setting a new block by number or hash. See help recover.",
|
Usage: "Attempts to recover a corrupted database by setting a new block by number or hash",
|
||||||
Description: `
|
Description: `
|
||||||
The recover commands will attempt to read out the last
|
The recover commands will attempt to read out the last
|
||||||
block based on that.
|
block based on that.
|
||||||
|
@ -339,10 +339,8 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
|
||||||
utils.RPCCORSDomainFlag,
|
utils.RPCCORSDomainFlag,
|
||||||
utils.VerbosityFlag,
|
utils.VerbosityFlag,
|
||||||
utils.BacktraceAtFlag,
|
utils.BacktraceAtFlag,
|
||||||
utils.LogToStdErrFlag,
|
|
||||||
utils.LogVModuleFlag,
|
utils.LogVModuleFlag,
|
||||||
utils.LogFileFlag,
|
utils.LogFileFlag,
|
||||||
utils.LogJSONFlag,
|
|
||||||
utils.PProfEanbledFlag,
|
utils.PProfEanbledFlag,
|
||||||
utils.PProfPortFlag,
|
utils.PProfPortFlag,
|
||||||
utils.MetricsEnabledFlag,
|
utils.MetricsEnabledFlag,
|
||||||
|
@ -402,7 +400,6 @@ func makeDefaultExtra() []byte {
|
||||||
glog.V(logger.Debug).Infof("extra: %x\n", extra)
|
glog.V(logger.Debug).Infof("extra: %x\n", extra)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return extra
|
return extra
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,212 @@
|
||||||
|
// Copyright 2015 The go-ethereum Authors
|
||||||
|
// This file is part of go-ethereum.
|
||||||
|
//
|
||||||
|
// go-ethereum is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// go-ethereum is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// Contains the geth command usage template and generator.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/codegangsta/cli"
|
||||||
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AppHelpTemplate is the test template for the default, global app help topic.
|
||||||
|
var AppHelpTemplate = `NAME:
|
||||||
|
{{.App.Name}} - {{.App.Usage}}
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
{{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
|
||||||
|
{{if .App.Version}}
|
||||||
|
VERSION:
|
||||||
|
{{.App.Version}}
|
||||||
|
{{end}}{{if len .App.Authors}}
|
||||||
|
AUTHOR(S):
|
||||||
|
{{range .App.Authors}}{{ . }}{{end}}
|
||||||
|
{{end}}{{if .App.Commands}}
|
||||||
|
COMMANDS:
|
||||||
|
{{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
||||||
|
{{end}}{{end}}{{if .FlagGroups}}
|
||||||
|
{{range .FlagGroups}}{{.Name}} OPTIONS:
|
||||||
|
{{range .Flags}}{{.}}
|
||||||
|
{{end}}
|
||||||
|
{{end}}{{end}}{{if .App.Copyright }}
|
||||||
|
COPYRIGHT:
|
||||||
|
{{.App.Copyright}}
|
||||||
|
{{end}}
|
||||||
|
`
|
||||||
|
|
||||||
|
// flagGroup is a collection of flags belonging to a single topic.
|
||||||
|
type flagGroup struct {
|
||||||
|
Name string
|
||||||
|
Flags []cli.Flag
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppHelpFlagGroups is the application flags, grouped by functionality.
|
||||||
|
var AppHelpFlagGroups = []flagGroup{
|
||||||
|
{
|
||||||
|
Name: "ETHEREUM",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.DataDirFlag,
|
||||||
|
utils.NetworkIdFlag,
|
||||||
|
utils.OlympicFlag,
|
||||||
|
utils.TestNetFlag,
|
||||||
|
utils.DevModeFlag,
|
||||||
|
utils.GenesisFileFlag,
|
||||||
|
utils.IdentityFlag,
|
||||||
|
utils.FastSyncFlag,
|
||||||
|
utils.CacheFlag,
|
||||||
|
utils.BlockchainVersionFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "ACCOUNT",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.UnlockedAccountFlag,
|
||||||
|
utils.PasswordFileFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "API AND CONSOLE",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.RPCEnabledFlag,
|
||||||
|
utils.RPCListenAddrFlag,
|
||||||
|
utils.RPCPortFlag,
|
||||||
|
utils.RpcApiFlag,
|
||||||
|
utils.IPCDisabledFlag,
|
||||||
|
utils.IPCApiFlag,
|
||||||
|
utils.IPCPathFlag,
|
||||||
|
utils.RPCCORSDomainFlag,
|
||||||
|
utils.JSpathFlag,
|
||||||
|
utils.ExecFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "NETWORKING",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.BootnodesFlag,
|
||||||
|
utils.ListenPortFlag,
|
||||||
|
utils.MaxPeersFlag,
|
||||||
|
utils.MaxPendingPeersFlag,
|
||||||
|
utils.NATFlag,
|
||||||
|
utils.NoDiscoverFlag,
|
||||||
|
utils.NodeKeyFileFlag,
|
||||||
|
utils.NodeKeyHexFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "MINER",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.MiningEnabledFlag,
|
||||||
|
utils.MinerThreadsFlag,
|
||||||
|
utils.MiningGPUFlag,
|
||||||
|
utils.AutoDAGFlag,
|
||||||
|
utils.EtherbaseFlag,
|
||||||
|
utils.GasPriceFlag,
|
||||||
|
utils.ExtraDataFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "GAS PRICE ORACLE",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.GpoMinGasPriceFlag,
|
||||||
|
utils.GpoMaxGasPriceFlag,
|
||||||
|
utils.GpoFullBlockRatioFlag,
|
||||||
|
utils.GpobaseStepDownFlag,
|
||||||
|
utils.GpobaseStepUpFlag,
|
||||||
|
utils.GpobaseCorrectionFactorFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "VIRTUAL MACHINE",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.VMDebugFlag,
|
||||||
|
utils.VMEnableJitFlag,
|
||||||
|
utils.VMForceJitFlag,
|
||||||
|
utils.VMJitCacheFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "LOGGING AND DEBUGGING",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.VerbosityFlag,
|
||||||
|
utils.LogVModuleFlag,
|
||||||
|
utils.BacktraceAtFlag,
|
||||||
|
utils.LogFileFlag,
|
||||||
|
utils.PProfEanbledFlag,
|
||||||
|
utils.PProfPortFlag,
|
||||||
|
utils.MetricsEnabledFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "EXPERIMENTAL",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.WhisperEnabledFlag,
|
||||||
|
utils.NatspecEnabledFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "MISCELLANEOUS",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
utils.SolcPathFlag,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Override the default app help template
|
||||||
|
cli.AppHelpTemplate = AppHelpTemplate
|
||||||
|
|
||||||
|
// Define a one shot struct to pass to the usage template
|
||||||
|
type helpData struct {
|
||||||
|
App interface{}
|
||||||
|
FlagGroups []flagGroup
|
||||||
|
}
|
||||||
|
// Override the default app help printer, but only for the global app help
|
||||||
|
originalHelpPrinter := cli.HelpPrinter
|
||||||
|
cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
|
||||||
|
if tmpl == AppHelpTemplate {
|
||||||
|
// Iterate over all the flags and add any uncategorized ones
|
||||||
|
categorized := make(map[string]struct{})
|
||||||
|
for _, group := range AppHelpFlagGroups {
|
||||||
|
for _, flag := range group.Flags {
|
||||||
|
categorized[flag.String()] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uncategorized := []cli.Flag{}
|
||||||
|
for _, flag := range data.(*cli.App).Flags {
|
||||||
|
if _, ok := categorized[flag.String()]; !ok {
|
||||||
|
uncategorized = append(uncategorized, flag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(uncategorized) > 0 {
|
||||||
|
// Append all ungategorized options to the misc group
|
||||||
|
miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
|
||||||
|
AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
|
||||||
|
|
||||||
|
// Make sure they are removed afterwards
|
||||||
|
defer func() {
|
||||||
|
AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
// Render out custom usage screen
|
||||||
|
originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
|
||||||
|
} else {
|
||||||
|
originalHelpPrinter(w, tmpl, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -101,35 +101,29 @@ var (
|
||||||
// General settings
|
// General settings
|
||||||
DataDirFlag = DirectoryFlag{
|
DataDirFlag = DirectoryFlag{
|
||||||
Name: "datadir",
|
Name: "datadir",
|
||||||
Usage: "Data directory to be used",
|
Usage: "Data directory for the databases and keystore",
|
||||||
Value: DirectoryString{common.DefaultDataDir()},
|
Value: DirectoryString{common.DefaultDataDir()},
|
||||||
}
|
}
|
||||||
NetworkIdFlag = cli.IntFlag{
|
NetworkIdFlag = cli.IntFlag{
|
||||||
Name: "networkid",
|
Name: "networkid",
|
||||||
Usage: "Network Id (integer)",
|
Usage: "Network identifier (integer, 0=Olympic, 1=Frontier, 2=Morden)",
|
||||||
Value: eth.NetworkId,
|
Value: eth.NetworkId,
|
||||||
}
|
}
|
||||||
BlockchainVersionFlag = cli.IntFlag{
|
OlympicFlag = cli.BoolFlag{
|
||||||
Name: "blockchainversion",
|
Name: "olympic",
|
||||||
Usage: "Blockchain version (integer)",
|
Usage: "Olympic network: pre-configured pre-release test network",
|
||||||
Value: core.BlockChainVersion,
|
|
||||||
}
|
|
||||||
GenesisNonceFlag = cli.IntFlag{
|
|
||||||
Name: "genesisnonce",
|
|
||||||
Usage: "Sets the genesis nonce",
|
|
||||||
Value: 42,
|
|
||||||
}
|
|
||||||
GenesisFileFlag = cli.StringFlag{
|
|
||||||
Name: "genesis",
|
|
||||||
Usage: "Inserts/Overwrites the genesis block (json format)",
|
|
||||||
}
|
|
||||||
DevModeFlag = cli.BoolFlag{
|
|
||||||
Name: "dev",
|
|
||||||
Usage: "Developer mode. This mode creates a private network and sets several debugging flags",
|
|
||||||
}
|
}
|
||||||
TestNetFlag = cli.BoolFlag{
|
TestNetFlag = cli.BoolFlag{
|
||||||
Name: "testnet",
|
Name: "testnet",
|
||||||
Usage: "Testnet mode. This enables your node to operate on the testnet",
|
Usage: "Morden network: pre-configured test network with modified starting nonces (replay protection)",
|
||||||
|
}
|
||||||
|
DevModeFlag = cli.BoolFlag{
|
||||||
|
Name: "dev",
|
||||||
|
Usage: "Developer mode: pre-configured private network with several debugging flags",
|
||||||
|
}
|
||||||
|
GenesisFileFlag = cli.StringFlag{
|
||||||
|
Name: "genesis",
|
||||||
|
Usage: "Insert/overwrite the genesis block (JSON format)",
|
||||||
}
|
}
|
||||||
IdentityFlag = cli.StringFlag{
|
IdentityFlag = cli.StringFlag{
|
||||||
Name: "identity",
|
Name: "identity",
|
||||||
|
@ -146,12 +140,13 @@ var (
|
||||||
}
|
}
|
||||||
CacheFlag = cli.IntFlag{
|
CacheFlag = cli.IntFlag{
|
||||||
Name: "cache",
|
Name: "cache",
|
||||||
Usage: "Megabytes of memory allocated to internal caching",
|
Usage: "Megabytes of memory allocated to internal caching (min 16MB / database forced)",
|
||||||
Value: 0,
|
Value: 0,
|
||||||
}
|
}
|
||||||
OlympicFlag = cli.BoolFlag{
|
BlockchainVersionFlag = cli.IntFlag{
|
||||||
Name: "olympic",
|
Name: "blockchainversion",
|
||||||
Usage: "Use olympic style protocol",
|
Usage: "Blockchain version (integer)",
|
||||||
|
Value: core.BlockChainVersion,
|
||||||
}
|
}
|
||||||
FastSyncFlag = cli.BoolFlag{
|
FastSyncFlag = cli.BoolFlag{
|
||||||
Name: "fast",
|
Name: "fast",
|
||||||
|
@ -161,50 +156,48 @@ var (
|
||||||
Name: "lightkdf",
|
Name: "lightkdf",
|
||||||
Usage: "Reduce KDF memory & CPU usage at some expense of KDF strength",
|
Usage: "Reduce KDF memory & CPU usage at some expense of KDF strength",
|
||||||
}
|
}
|
||||||
|
// Miner settings
|
||||||
// miner settings
|
|
||||||
// TODO: refactor CPU vs GPU mining flags
|
// TODO: refactor CPU vs GPU mining flags
|
||||||
MiningGPUFlag = cli.StringFlag{
|
|
||||||
Name: "minegpu",
|
|
||||||
Usage: "Mine with given GPUs. '--minegpu 0,1' will mine with the first two GPUs found.",
|
|
||||||
}
|
|
||||||
|
|
||||||
MinerThreadsFlag = cli.IntFlag{
|
|
||||||
Name: "minerthreads",
|
|
||||||
Usage: "Number of miner threads",
|
|
||||||
Value: runtime.NumCPU(),
|
|
||||||
}
|
|
||||||
MiningEnabledFlag = cli.BoolFlag{
|
MiningEnabledFlag = cli.BoolFlag{
|
||||||
Name: "mine",
|
Name: "mine",
|
||||||
Usage: "Enable mining",
|
Usage: "Enable mining",
|
||||||
}
|
}
|
||||||
|
MinerThreadsFlag = cli.IntFlag{
|
||||||
|
Name: "minerthreads",
|
||||||
|
Usage: "Number of CPU threads to use for mining",
|
||||||
|
Value: runtime.NumCPU(),
|
||||||
|
}
|
||||||
|
MiningGPUFlag = cli.StringFlag{
|
||||||
|
Name: "minergpus",
|
||||||
|
Usage: "List of GPUs to use for mining (e.g. '0,1' will use the first two GPUs found)",
|
||||||
|
}
|
||||||
AutoDAGFlag = cli.BoolFlag{
|
AutoDAGFlag = cli.BoolFlag{
|
||||||
Name: "autodag",
|
Name: "autodag",
|
||||||
Usage: "Enable automatic DAG pregeneration",
|
Usage: "Enable automatic DAG pregeneration",
|
||||||
}
|
}
|
||||||
EtherbaseFlag = cli.StringFlag{
|
EtherbaseFlag = cli.StringFlag{
|
||||||
Name: "etherbase",
|
Name: "etherbase",
|
||||||
Usage: "Public address for block mining rewards. By default the address first created is used",
|
Usage: "Public address for block mining rewards (default = first account created)",
|
||||||
Value: "0",
|
Value: "0",
|
||||||
}
|
}
|
||||||
GasPriceFlag = cli.StringFlag{
|
GasPriceFlag = cli.StringFlag{
|
||||||
Name: "gasprice",
|
Name: "gasprice",
|
||||||
Usage: "Sets the minimal gasprice when mining transactions",
|
Usage: "Minimal gas price to accept for mining a transactions",
|
||||||
Value: new(big.Int).Mul(big.NewInt(50), common.Shannon).String(),
|
Value: new(big.Int).Mul(big.NewInt(50), common.Shannon).String(),
|
||||||
}
|
}
|
||||||
ExtraDataFlag = cli.StringFlag{
|
ExtraDataFlag = cli.StringFlag{
|
||||||
Name: "extradata",
|
Name: "extradata",
|
||||||
Usage: "Extra data for the miner",
|
Usage: "Block extra data set by the miner (default = client version)",
|
||||||
}
|
}
|
||||||
|
// Account settings
|
||||||
UnlockedAccountFlag = cli.StringFlag{
|
UnlockedAccountFlag = cli.StringFlag{
|
||||||
Name: "unlock",
|
Name: "unlock",
|
||||||
Usage: "Unlock the account given until this program exits (prompts for password). '--unlock n' unlocks the n-th account in order or creation.",
|
Usage: "Unlock an account (may be creation index) until this program exits (prompts for password)",
|
||||||
Value: "",
|
Value: "",
|
||||||
}
|
}
|
||||||
PasswordFileFlag = cli.StringFlag{
|
PasswordFileFlag = cli.StringFlag{
|
||||||
Name: "password",
|
Name: "password",
|
||||||
Usage: "Path to password file to use with options and subcommands needing a password",
|
Usage: "Password file to use with options/subcommands needing a pass phrase",
|
||||||
Value: "",
|
Value: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,32 +221,24 @@ var (
|
||||||
}
|
}
|
||||||
|
|
||||||
// logging and debug settings
|
// logging and debug settings
|
||||||
LogFileFlag = cli.StringFlag{
|
|
||||||
Name: "logfile",
|
|
||||||
Usage: "Send log output to a file",
|
|
||||||
}
|
|
||||||
VerbosityFlag = cli.IntFlag{
|
VerbosityFlag = cli.IntFlag{
|
||||||
Name: "verbosity",
|
Name: "verbosity",
|
||||||
Usage: "Logging verbosity: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)",
|
Usage: "Logging verbosity: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)",
|
||||||
Value: int(logger.InfoLevel),
|
Value: int(logger.InfoLevel),
|
||||||
}
|
}
|
||||||
LogJSONFlag = cli.StringFlag{
|
LogFileFlag = cli.StringFlag{
|
||||||
Name: "logjson",
|
Name: "logfile",
|
||||||
Usage: "Send json structured log output to a file or '-' for standard output (default: no json output)",
|
Usage: "Log output file within the data dir (default = no log file generated)",
|
||||||
Value: "",
|
Value: "",
|
||||||
}
|
}
|
||||||
LogToStdErrFlag = cli.BoolFlag{
|
|
||||||
Name: "logtostderr",
|
|
||||||
Usage: "Logs are written to standard error instead of to files.",
|
|
||||||
}
|
|
||||||
LogVModuleFlag = cli.GenericFlag{
|
LogVModuleFlag = cli.GenericFlag{
|
||||||
Name: "vmodule",
|
Name: "vmodule",
|
||||||
Usage: "The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the \".go\" suffix) or \"glob\" pattern and N is a log verbosity level.",
|
Usage: "Per-module verbosity: comma-separated list of <module>=<level>, where <module> is file literal or a glog pattern",
|
||||||
Value: glog.GetVModule(),
|
Value: glog.GetVModule(),
|
||||||
}
|
}
|
||||||
BacktraceAtFlag = cli.GenericFlag{
|
BacktraceAtFlag = cli.GenericFlag{
|
||||||
Name: "backtrace_at",
|
Name: "backtrace",
|
||||||
Usage: "If set to a file and line number (e.g., \"block.go:271\") holding a logging statement, a stack trace will be logged",
|
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
|
||||||
Value: glog.GetTraceLocation(),
|
Value: glog.GetTraceLocation(),
|
||||||
}
|
}
|
||||||
PProfEanbledFlag = cli.BoolFlag{
|
PProfEanbledFlag = cli.BoolFlag{
|
||||||
|
@ -262,37 +247,37 @@ var (
|
||||||
}
|
}
|
||||||
PProfPortFlag = cli.IntFlag{
|
PProfPortFlag = cli.IntFlag{
|
||||||
Name: "pprofport",
|
Name: "pprofport",
|
||||||
Usage: "Port on which the profiler should listen",
|
Usage: "Profile server listening port",
|
||||||
Value: 6060,
|
Value: 6060,
|
||||||
}
|
}
|
||||||
MetricsEnabledFlag = cli.BoolFlag{
|
MetricsEnabledFlag = cli.BoolFlag{
|
||||||
Name: metrics.MetricsEnabledFlag,
|
Name: metrics.MetricsEnabledFlag,
|
||||||
Usage: "Enables metrics collection and reporting",
|
Usage: "Enable metrics collection and reporting",
|
||||||
}
|
}
|
||||||
|
|
||||||
// RPC settings
|
// RPC settings
|
||||||
RPCEnabledFlag = cli.BoolFlag{
|
RPCEnabledFlag = cli.BoolFlag{
|
||||||
Name: "rpc",
|
Name: "rpc",
|
||||||
Usage: "Enable the JSON-RPC server",
|
Usage: "Enable the HTTP-RPC server",
|
||||||
}
|
}
|
||||||
RPCListenAddrFlag = cli.StringFlag{
|
RPCListenAddrFlag = cli.StringFlag{
|
||||||
Name: "rpcaddr",
|
Name: "rpcaddr",
|
||||||
Usage: "Listening address for the JSON-RPC server",
|
Usage: "HTTP-RPC server listening interface",
|
||||||
Value: "127.0.0.1",
|
Value: "127.0.0.1",
|
||||||
}
|
}
|
||||||
RPCPortFlag = cli.IntFlag{
|
RPCPortFlag = cli.IntFlag{
|
||||||
Name: "rpcport",
|
Name: "rpcport",
|
||||||
Usage: "Port on which the JSON-RPC server should listen",
|
Usage: "HTTP-RPC server listening port",
|
||||||
Value: 8545,
|
Value: 8545,
|
||||||
}
|
}
|
||||||
RPCCORSDomainFlag = cli.StringFlag{
|
RPCCORSDomainFlag = cli.StringFlag{
|
||||||
Name: "rpccorsdomain",
|
Name: "rpccorsdomain",
|
||||||
Usage: "Domain on which to send Access-Control-Allow-Origin header",
|
Usage: "Domains from which to accept cross origin requests (browser enforced)",
|
||||||
Value: "",
|
Value: "",
|
||||||
}
|
}
|
||||||
RpcApiFlag = cli.StringFlag{
|
RpcApiFlag = cli.StringFlag{
|
||||||
Name: "rpcapi",
|
Name: "rpcapi",
|
||||||
Usage: "Specify the API's which are offered over the HTTP RPC interface",
|
Usage: "API's offered over the HTTP-RPC interface",
|
||||||
Value: comms.DefaultHttpRpcApis,
|
Value: comms.DefaultHttpRpcApis,
|
||||||
}
|
}
|
||||||
IPCDisabledFlag = cli.BoolFlag{
|
IPCDisabledFlag = cli.BoolFlag{
|
||||||
|
@ -301,7 +286,7 @@ var (
|
||||||
}
|
}
|
||||||
IPCApiFlag = cli.StringFlag{
|
IPCApiFlag = cli.StringFlag{
|
||||||
Name: "ipcapi",
|
Name: "ipcapi",
|
||||||
Usage: "Specify the API's which are offered over the IPC interface",
|
Usage: "API's offered over the IPC-RPC interface",
|
||||||
Value: comms.DefaultIpcApis,
|
Value: comms.DefaultIpcApis,
|
||||||
}
|
}
|
||||||
IPCPathFlag = DirectoryFlag{
|
IPCPathFlag = DirectoryFlag{
|
||||||
|
@ -311,7 +296,7 @@ var (
|
||||||
}
|
}
|
||||||
ExecFlag = cli.StringFlag{
|
ExecFlag = cli.StringFlag{
|
||||||
Name: "exec",
|
Name: "exec",
|
||||||
Usage: "Execute javascript statement (only in combination with console/attach)",
|
Usage: "Execute JavaScript statement (only in combination with console/attach)",
|
||||||
}
|
}
|
||||||
// Network Settings
|
// Network Settings
|
||||||
MaxPeersFlag = cli.IntFlag{
|
MaxPeersFlag = cli.IntFlag{
|
||||||
|
@ -331,7 +316,7 @@ var (
|
||||||
}
|
}
|
||||||
BootnodesFlag = cli.StringFlag{
|
BootnodesFlag = cli.StringFlag{
|
||||||
Name: "bootnodes",
|
Name: "bootnodes",
|
||||||
Usage: "Space-separated enode URLs for p2p discovery bootstrap",
|
Usage: "Space-separated enode URLs for P2P discovery bootstrap",
|
||||||
Value: "",
|
Value: "",
|
||||||
}
|
}
|
||||||
NodeKeyFileFlag = cli.StringFlag{
|
NodeKeyFileFlag = cli.StringFlag{
|
||||||
|
@ -353,19 +338,21 @@ var (
|
||||||
}
|
}
|
||||||
WhisperEnabledFlag = cli.BoolFlag{
|
WhisperEnabledFlag = cli.BoolFlag{
|
||||||
Name: "shh",
|
Name: "shh",
|
||||||
Usage: "Enable whisper",
|
Usage: "Enable Whisper",
|
||||||
}
|
}
|
||||||
// ATM the url is left to the user and deployment to
|
// ATM the url is left to the user and deployment to
|
||||||
JSpathFlag = cli.StringFlag{
|
JSpathFlag = cli.StringFlag{
|
||||||
Name: "jspath",
|
Name: "jspath",
|
||||||
Usage: "JS root path for loadScript and document root for admin.httpGet",
|
Usage: "JavaSript root path for `loadScript` and document root for `admin.httpGet`",
|
||||||
Value: ".",
|
Value: ".",
|
||||||
}
|
}
|
||||||
SolcPathFlag = cli.StringFlag{
|
SolcPathFlag = cli.StringFlag{
|
||||||
Name: "solc",
|
Name: "solc",
|
||||||
Usage: "solidity compiler to be used",
|
Usage: "Solidity compiler command to be used",
|
||||||
Value: "solc",
|
Value: "solc",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gas price oracle settings
|
||||||
GpoMinGasPriceFlag = cli.StringFlag{
|
GpoMinGasPriceFlag = cli.StringFlag{
|
||||||
Name: "gpomin",
|
Name: "gpomin",
|
||||||
Usage: "Minimum suggested gas price",
|
Usage: "Minimum suggested gas price",
|
||||||
|
@ -441,7 +428,6 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
|
||||||
cfg := ð.Config{
|
cfg := ð.Config{
|
||||||
Name: common.MakeName(clientID, version),
|
Name: common.MakeName(clientID, version),
|
||||||
DataDir: MustDataDir(ctx),
|
DataDir: MustDataDir(ctx),
|
||||||
GenesisNonce: ctx.GlobalInt(GenesisNonceFlag.Name),
|
|
||||||
GenesisFile: ctx.GlobalString(GenesisFileFlag.Name),
|
GenesisFile: ctx.GlobalString(GenesisFileFlag.Name),
|
||||||
FastSync: ctx.GlobalBool(FastSyncFlag.Name),
|
FastSync: ctx.GlobalBool(FastSyncFlag.Name),
|
||||||
BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
|
BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
|
||||||
|
@ -450,7 +436,6 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
|
||||||
NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
|
NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
|
||||||
LogFile: ctx.GlobalString(LogFileFlag.Name),
|
LogFile: ctx.GlobalString(LogFileFlag.Name),
|
||||||
Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
|
Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
|
||||||
LogJSON: ctx.GlobalString(LogJSONFlag.Name),
|
|
||||||
Etherbase: common.HexToAddress(etherbase),
|
Etherbase: common.HexToAddress(etherbase),
|
||||||
MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
|
MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
|
||||||
AccountManager: am,
|
AccountManager: am,
|
||||||
|
|
|
@ -91,7 +91,6 @@ type Config struct {
|
||||||
|
|
||||||
Name string
|
Name string
|
||||||
NetworkId int
|
NetworkId int
|
||||||
GenesisNonce int
|
|
||||||
GenesisFile string
|
GenesisFile string
|
||||||
GenesisBlock *types.Block // used by block tests
|
GenesisBlock *types.Block // used by block tests
|
||||||
FastSync bool
|
FastSync bool
|
||||||
|
@ -104,7 +103,6 @@ type Config struct {
|
||||||
DataDir string
|
DataDir string
|
||||||
LogFile string
|
LogFile string
|
||||||
Verbosity int
|
Verbosity int
|
||||||
LogJSON string
|
|
||||||
VmDebug bool
|
VmDebug bool
|
||||||
NatSpec bool
|
NatSpec bool
|
||||||
DocRoot string
|
DocRoot string
|
||||||
|
@ -273,11 +271,7 @@ type Ethereum struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config *Config) (*Ethereum, error) {
|
func New(config *Config) (*Ethereum, error) {
|
||||||
// Bootstrap database
|
|
||||||
logger.New(config.DataDir, config.LogFile, config.Verbosity)
|
logger.New(config.DataDir, config.LogFile, config.Verbosity)
|
||||||
if len(config.LogJSON) > 0 {
|
|
||||||
logger.NewJSONsystem(config.DataDir, config.LogJSON)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let the database take 3/4 of the max open files (TODO figure out a way to get the actual limit of the open files)
|
// Let the database take 3/4 of the max open files (TODO figure out a way to get the actual limit of the open files)
|
||||||
const dbCount = 3
|
const dbCount = 3
|
||||||
|
|
Loading…
Reference in New Issue