Merge pull request #142 from harshavardhana/pr_out_update_codegangsta_to_upstream_changes

This commit is contained in:
Harshavardhana
2015-03-18 10:49:40 -07:00
8 changed files with 140 additions and 49 deletions
+6 -6
View File
@@ -210,7 +210,7 @@ Subcommands can be defined for a more git-like command line app.
app.Commands = []cli.Command{
{
Name: "add",
ShortName: "a",
Names: []string{"a"},
Usage: "add a task to the list",
Action: func(c *cli.Context) {
println("added task: ", c.Args().First())
@@ -218,7 +218,7 @@ app.Commands = []cli.Command{
},
{
Name: "complete",
ShortName: "c",
Names: []string{"c"},
Usage: "complete a task on the list",
Action: func(c *cli.Context) {
println("completed task: ", c.Args().First())
@@ -226,7 +226,7 @@ app.Commands = []cli.Command{
},
{
Name: "template",
ShortName: "r",
Names: []string{"r"},
Usage: "options for task templates",
Subcommands: []cli.Command{
{
@@ -244,7 +244,7 @@ app.Commands = []cli.Command{
},
},
},
},
},
}
...
```
@@ -262,8 +262,8 @@ app := cli.NewApp()
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "complete",
ShortName: "c",
Name: "complete",
Names: []string{"c"},
Usage: "complete a task on the list",
Action: func(c *cli.Context) {
println("completed task: ", c.Args().First())
+6 -1
View File
@@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"os"
"strings"
"text/tabwriter"
"text/template"
"time"
@@ -88,8 +89,12 @@ func (a *App) Run(arguments []string) (err error) {
}()
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").Parse(templ))
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
err := t.Execute(w, data)
if err != nil {
panic(err)
+7 -7
View File
@@ -23,7 +23,7 @@ func ExampleApp() {
}
app.Author = "Harrison"
app.Email = "harrison@lolwut.com"
app.Authors = []cli.Author{{"Oliver Allen", "oliver@toyshop.com"}}
app.Authors = []cli.Author{cli.Author{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
app.Run(os.Args)
// Output:
// Hello Jeremy
@@ -37,13 +37,13 @@ func ExampleAppSubcommand() {
app.Commands = []cli.Command{
{
Name: "hello",
ShortName: "hi",
Aliases: []string{"hi"},
Usage: "use it to see a description",
Description: "This is how we describe hello the function",
Subcommands: []cli.Command{
{
Name: "english",
ShortName: "en",
Aliases: []string{"en"},
Usage: "sends a greeting in english",
Description: "greets someone in english",
Flags: []cli.Flag{
@@ -78,7 +78,7 @@ func ExampleAppHelp() {
app.Commands = []cli.Command{
{
Name: "describeit",
ShortName: "d",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(c *cli.Context) {
@@ -108,7 +108,7 @@ func ExampleAppBashComplete() {
app.Commands = []cli.Command{
{
Name: "describeit",
ShortName: "d",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(c *cli.Context) {
@@ -162,8 +162,8 @@ var commandAppTests = []struct {
func TestApp_Command(t *testing.T) {
app := cli.NewApp()
fooCommand := cli.Command{Name: "foobar", ShortName: "f"}
batCommand := cli.Command{Name: "batbaz", ShortName: "b"}
fooCommand := cli.Command{Name: "foobar", Aliases: []string{"f"}}
batCommand := cli.Command{Name: "batbaz", Aliases: []string{"b"}}
app.Commands = []cli.Command{
fooCommand,
batCommand,
+14 -14
View File
@@ -12,17 +12,17 @@ func Example() {
app.Usage = "task list on the command line"
app.Commands = []cli.Command{
{
Name: "add",
ShortName: "a",
Usage: "add a task to the list",
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",
ShortName: "c",
Usage: "complete a task on the list",
Name: "complete",
Aliases: []string{"c"},
Usage: "complete a task on the list",
Action: func(c *cli.Context) {
println("completed task: ", c.Args().First())
},
@@ -38,13 +38,13 @@ func ExampleSubcommand() {
app.Commands = []cli.Command{
{
Name: "hello",
ShortName: "hi",
Aliases: []string{"hi"},
Usage: "use it to see a description",
Description: "This is how we describe hello the function",
Subcommands: []cli.Command{
{
Name: "english",
ShortName: "en",
Aliases: []string{"en"},
Usage: "sends a greeting in english",
Description: "greets someone in english",
Flags: []cli.Flag{
@@ -58,9 +58,9 @@ func ExampleSubcommand() {
println("Hello, ", c.String("name"))
},
}, {
Name: "spanish",
ShortName: "sp",
Usage: "sends a greeting in spanish",
Name: "spanish",
Aliases: []string{"sp"},
Usage: "sends a greeting in spanish",
Flags: []cli.Flag{
cli.StringFlag{
Name: "surname",
@@ -72,9 +72,9 @@ func ExampleSubcommand() {
println("Hola, ", c.String("surname"))
},
}, {
Name: "french",
ShortName: "fr",
Usage: "sends a greeting in french",
Name: "french",
Aliases: []string{"fr"},
Usage: "sends a greeting in french",
Flags: []cli.Flag{
cli.StringFlag{
Name: "nickname",
+19 -2
View File
@@ -10,8 +10,10 @@ import (
type Command struct {
// The name of the command
Name string
// short name of the command. Typically one character
// short name of the command. Typically one character (deprecated, use `Aliases`)
ShortName string
// A list of aliases for the command
Aliases []string
// A short description of the usage of this command
Usage string
// A longer explanation of how the command works
@@ -117,9 +119,24 @@ func (c Command) Run(ctx *Context) error {
return nil
}
func (c Command) Names() []string {
names := []string{c.Name}
if c.ShortName != "" {
names = append(names, c.ShortName)
}
return append(names, c.Aliases...)
}
// Returns true if Command.Name or Command.ShortName matches given name
func (c Command) HasName(name string) bool {
return c.Name == name || (c.ShortName != "" && c.ShortName == name)
for _, n := range c.Names() {
if n == name {
return true
}
}
return false
}
func (c Command) startApp(ctx *Context) error {
+2 -2
View File
@@ -17,7 +17,7 @@ func TestCommandDoNotIgnoreFlags(t *testing.T) {
command := cli.Command{
Name: "test-cmd",
ShortName: "tc",
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *cli.Context) {},
@@ -37,7 +37,7 @@ func TestCommandIgnoreFlags(t *testing.T) {
command := cli.Command{
Name: "test-cmd",
ShortName: "tc",
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(_ *cli.Context) {},
+47 -1
View File
@@ -12,6 +12,7 @@ import (
// This flag enables bash-completion for all commands and subcommands
var BashCompletionFlag = BoolFlag{
Name: "generate-bash-completion",
Hide: true,
}
// This flag prints the version for the application
@@ -36,6 +37,7 @@ type Flag interface {
// Apply Flag settings to the given flag set
Apply(*flag.FlagSet)
getName() string
isNotHidden() bool
}
func flagSet(name string, flags []Flag) *flag.FlagSet {
@@ -67,6 +69,7 @@ type GenericFlag struct {
Value Generic
Usage string
EnvVar string
Hide bool
}
// String returns the string representation of the generic flag to display the
@@ -99,6 +102,10 @@ func (f GenericFlag) getName() string {
return f.Name
}
func (f GenericFlag) isNotHidden() bool {
return !f.Hide
}
type StringSlice []string
func (f *StringSlice) Set(value string) error {
@@ -119,6 +126,7 @@ type StringSliceFlag struct {
Value *StringSlice
Usage string
EnvVar string
Hide bool
}
func (f StringSliceFlag) String() string {
@@ -152,6 +160,10 @@ func (f StringSliceFlag) getName() string {
return f.Name
}
func (f StringSliceFlag) isNotHidden() bool {
return !f.Hide
}
type IntSlice []int
func (f *IntSlice) Set(value string) error {
@@ -178,6 +190,7 @@ type IntSliceFlag struct {
Value *IntSlice
Usage string
EnvVar string
Hide bool
}
func (f IntSliceFlag) String() string {
@@ -214,10 +227,15 @@ func (f IntSliceFlag) getName() string {
return f.Name
}
func (f IntSliceFlag) isNotHidden() bool {
return !f.Hide
}
type BoolFlag struct {
Name string
Usage string
EnvVar string
Hide bool
}
func (f BoolFlag) String() string {
@@ -248,10 +266,15 @@ func (f BoolFlag) getName() string {
return f.Name
}
func (f BoolFlag) isNotHidden() bool {
return !f.Hide
}
type BoolTFlag struct {
Name string
Usage string
EnvVar string
Hide bool
}
func (f BoolTFlag) String() string {
@@ -282,11 +305,16 @@ func (f BoolTFlag) getName() string {
return f.Name
}
func (f BoolTFlag) isNotHidden() bool {
return !f.Hide
}
type StringFlag struct {
Name string
Value string
Usage string
EnvVar string
Hide bool
}
func (f StringFlag) String() string {
@@ -298,7 +326,6 @@ func (f StringFlag) String() string {
} else {
fmtString = "%s %v\t%v"
}
return withEnvHint(f.EnvVar, fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage))
}
@@ -322,11 +349,16 @@ func (f StringFlag) getName() string {
return f.Name
}
func (f StringFlag) isNotHidden() bool {
return !f.Hide
}
type IntFlag struct {
Name string
Value int
Usage string
EnvVar string
Hide bool
}
func (f IntFlag) String() string {
@@ -356,11 +388,16 @@ func (f IntFlag) getName() string {
return f.Name
}
func (f IntFlag) isNotHidden() bool {
return !f.Hide
}
type DurationFlag struct {
Name string
Value time.Duration
Usage string
EnvVar string
Hide bool
}
func (f DurationFlag) String() string {
@@ -390,11 +427,16 @@ func (f DurationFlag) getName() string {
return f.Name
}
func (f DurationFlag) isNotHidden() bool {
return !f.Hide
}
type Float64Flag struct {
Name string
Value float64
Usage string
EnvVar string
Hide bool
}
func (f Float64Flag) String() string {
@@ -423,6 +465,10 @@ func (f Float64Flag) getName() string {
return f.Name
}
func (f Float64Flag) isNotHidden() bool {
return !f.Hide
}
func prefixFor(name string) (prefix string) {
if len(name) == 1 {
prefix = "-"
+39 -16
View File
@@ -14,11 +14,11 @@ USAGE:
VERSION:
{{.Version}}
AUTHOR(S):
{{range .Authors}}{{ . }} {{end}}
AUTHOR(S):
{{range .Authors}}{{ . }} {{end}}
COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{if .Flags}}
GLOBAL OPTIONS:
{{range .Flags}}{{.}}
@@ -52,7 +52,7 @@ USAGE:
{{.Name}} command{{if .Flags}} [command options]{{end}} [arguments...]
COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{if .Flags}}
OPTIONS:
{{range .Flags}}{{.}}
@@ -60,9 +60,9 @@ OPTIONS:
`
var helpCommand = Command{
Name: "help",
ShortName: "h",
Usage: "Shows a list of commands or help for one command",
Name: "help",
Aliases: []string{"h"},
Usage: "Shows a list of commands or help for one command",
Action: func(c *Context) {
args := c.Args()
if args.Present() {
@@ -74,9 +74,9 @@ var helpCommand = Command{
}
var helpSubcommand = Command{
Name: "help",
ShortName: "h",
Usage: "Shows a list of commands or help for one command",
Name: "help",
Aliases: []string{"h"},
Usage: "Shows a list of commands or help for one command",
Action: func(c *Context) {
args := c.Args()
if args.Present() {
@@ -96,15 +96,22 @@ var HelpPrinter helpPrinter = nil
var VersionPrinter = printVersion
func ShowAppHelp(c *Context) {
HelpPrinter(AppHelpTemplate, c.App)
// Make a copy of c.App context
app := *c.App
app.Flags = make([]Flag, 0)
for _, flag := range c.App.Flags {
if flag.isNotHidden() {
app.Flags = append(app.Flags, flag)
}
}
HelpPrinter(AppHelpTemplate, app)
}
// Prints the list of subcommands as the default app completion method
func DefaultAppComplete(c *Context) {
for _, command := range c.App.Commands {
fmt.Fprintln(c.App.Writer, command.Name)
if command.ShortName != "" {
fmt.Fprintln(c.App.Writer, command.ShortName)
for _, name := range command.Names() {
fmt.Fprintln(c.App.Writer, name)
}
}
}
@@ -113,13 +120,29 @@ func DefaultAppComplete(c *Context) {
func ShowCommandHelp(c *Context, command string) {
// show the subcommand help for a command with subcommands
if command == "" {
HelpPrinter(SubcommandHelpTemplate, c.App)
// Make a copy of c.App context
app := *c.App
app.Flags = make([]Flag, 0)
for _, flag := range c.App.Flags {
if flag.isNotHidden() {
app.Flags = append(app.Flags, flag)
}
}
HelpPrinter(SubcommandHelpTemplate, app)
return
}
for _, c := range c.App.Commands {
if c.HasName(command) {
HelpPrinter(CommandHelpTemplate, c)
// Make a copy of command context
c0 := c
c0.Flags = make([]Flag, 0)
for _, flag := range c.Flags {
if flag.isNotHidden() {
c0.Flags = append(c0.Flags, flag)
}
}
HelpPrinter(CommandHelpTemplate, c0)
return
}
}