Merge branch 'v2' into clean-stringlies

This commit is contained in:
Dan Buch 2016-05-23 22:08:42 -04:00
commit 80f444e0f5
4 changed files with 92 additions and 9 deletions

4
app.go
View File

@ -164,7 +164,7 @@ func (a *App) Run(arguments []string) (err error) {
HandleExitCoder(err) HandleExitCoder(err)
return err return err
} }
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") fmt.Fprintf(a.Writer, "Incorrect Usage: %s\n\n", err)
ShowAppHelp(context) ShowAppHelp(context)
return err return err
} }
@ -273,7 +273,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
HandleExitCoder(err) HandleExitCoder(err)
return err return err
} }
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") fmt.Fprintf(a.Writer, "Incorrect Usage: %s\n\n", err)
ShowSubcommandHelp(context) ShowSubcommandHelp(context)
return err return err
} }

View File

@ -97,7 +97,7 @@ func (c Command) Run(ctx *Context) (err error) {
HandleExitCoder(err) HandleExitCoder(err)
return err return err
} }
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.") fmt.Fprintf(ctx.App.Writer, "Incorrect Usage: %s\n\n", err)
fmt.Fprintln(ctx.App.Writer) fmt.Fprintln(ctx.App.Writer)
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
return err return err

12
flag.go
View File

@ -108,7 +108,7 @@ func (f GenericFlag) Apply(set *flag.FlagSet) {
} }
for _, name := range f.Names() { for _, name := range f.Names() {
set.Var(f.Value, name, f.Usage) set.Var(val, name, f.Usage)
} }
} }
@ -350,7 +350,7 @@ func (f BoolFlag) Apply(set *flag.FlagSet) {
for _, name := range f.Names() { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.BoolVar(f.Destination, name, f.Value, f.Usage) set.BoolVar(f.Destination, name, f.Value, f.Usage)
return continue
} }
set.Bool(name, f.Value, f.Usage) set.Bool(name, f.Value, f.Usage)
} }
@ -391,7 +391,7 @@ func (f StringFlag) Apply(set *flag.FlagSet) {
for _, name := range f.Names() { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.StringVar(f.Destination, name, f.Value, f.Usage) set.StringVar(f.Destination, name, f.Value, f.Usage)
return continue
} }
set.String(name, f.Value, f.Usage) set.String(name, f.Value, f.Usage)
} }
@ -436,7 +436,7 @@ func (f IntFlag) Apply(set *flag.FlagSet) {
for _, name := range f.Names() { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.IntVar(f.Destination, name, f.Value, f.Usage) set.IntVar(f.Destination, name, f.Value, f.Usage)
return continue
} }
set.Int(name, f.Value, f.Usage) set.Int(name, f.Value, f.Usage)
} }
@ -481,7 +481,7 @@ func (f DurationFlag) Apply(set *flag.FlagSet) {
for _, name := range f.Names() { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.DurationVar(f.Destination, name, f.Value, f.Usage) set.DurationVar(f.Destination, name, f.Value, f.Usage)
return continue
} }
set.Duration(name, f.Value, f.Usage) set.Duration(name, f.Value, f.Usage)
} }
@ -525,7 +525,7 @@ func (f Float64Flag) Apply(set *flag.FlagSet) {
for _, name := range f.Names() { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.Float64Var(f.Destination, name, f.Value, f.Usage) set.Float64Var(f.Destination, name, f.Value, f.Usage)
return continue
} }
set.Float64(name, f.Value, f.Usage) set.Float64(name, f.Value, f.Usage)
} }

View File

@ -1,6 +1,7 @@
package cli package cli
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
@ -29,6 +30,17 @@ func TestBoolFlagHelpOutput(t *testing.T) {
} }
} }
func TestBoolFlagApply_SetsAllNames(t *testing.T) {
v := false
fl := BoolFlag{Name: "wat", Aliases: []string{"W", "huh"}, Destination: &v}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--wat", "-W", "--huh"})
expect(t, err, nil)
expect(t, v, true)
}
var stringFlagTests = []struct { var stringFlagTests = []struct {
name string name string
aliases []string aliases []string
@ -72,6 +84,17 @@ func TestStringFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestStringFlagApply_SetsAllNames(t *testing.T) {
v := "mmm"
fl := StringFlag{Name: "hay", Aliases: []string{"H", "hayyy"}, Destination: &v}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--hay", "u", "-H", "yuu", "--hayyy", "YUUUU"})
expect(t, err, nil)
expect(t, v, "YUUUU")
}
var stringSliceFlagTests = []struct { var stringSliceFlagTests = []struct {
name string name string
aliases []string aliases []string
@ -113,6 +136,15 @@ func TestStringSliceFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestStringSliceFlagApply_SetsAllNames(t *testing.T) {
fl := StringSliceFlag{Name: "goat", Aliases: []string{"G", "gooots"}}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--goat", "aaa", "-G", "bbb", "--gooots", "eeeee"})
expect(t, err, nil)
}
var intFlagTests = []struct { var intFlagTests = []struct {
name string name string
expected string expected string
@ -149,6 +181,17 @@ func TestIntFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestIntFlagApply_SetsAllNames(t *testing.T) {
v := 3
fl := IntFlag{Name: "banana", Aliases: []string{"B", "banannanana"}, Destination: &v}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--banana", "1", "-B", "2", "--banannanana", "5"})
expect(t, err, nil)
expect(t, v, 5)
}
var durationFlagTests = []struct { var durationFlagTests = []struct {
name string name string
expected string expected string
@ -185,6 +228,17 @@ func TestDurationFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestDurationFlagApply_SetsAllNames(t *testing.T) {
v := time.Second * 20
fl := DurationFlag{Name: "howmuch", Aliases: []string{"H", "whyyy"}, Destination: &v}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--howmuch", "30s", "-H", "5m", "--whyyy", "30h"})
expect(t, err, nil)
expect(t, v, time.Hour*30)
}
var intSliceFlagTests = []struct { var intSliceFlagTests = []struct {
name string name string
aliases []string aliases []string
@ -224,6 +278,15 @@ func TestIntSliceFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestIntSliceFlagApply_SetsAllNames(t *testing.T) {
fl := IntSliceFlag{Name: "bits", Aliases: []string{"B", "bips"}}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--bits", "23", "-B", "3", "--bips", "99"})
expect(t, err, nil)
}
var float64FlagTests = []struct { var float64FlagTests = []struct {
name string name string
expected string expected string
@ -260,6 +323,17 @@ func TestFloat64FlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestFloat64FlagApply_SetsAllNames(t *testing.T) {
v := float64(99.1)
fl := Float64Flag{Name: "noodles", Aliases: []string{"N", "nurbles"}, Destination: &v}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--noodles", "1.3", "-N", "11", "--nurbles", "43.33333"})
expect(t, err, nil)
expect(t, v, float64(43.33333))
}
var genericFlagTests = []struct { var genericFlagTests = []struct {
name string name string
value Generic value Generic
@ -297,6 +371,15 @@ func TestGenericFlagWithEnvVarHelpOutput(t *testing.T) {
} }
} }
func TestGenericFlagApply_SetsAllNames(t *testing.T) {
fl := GenericFlag{Name: "orbs", Aliases: []string{"O", "obrs"}, Value: &Parser{}}
set := flag.NewFlagSet("test", 0)
fl.Apply(set)
err := set.Parse([]string{"--orbs", "eleventy,3", "-O", "4,bloop", "--obrs", "19,s"})
expect(t, err, nil)
}
func TestParseMultiString(t *testing.T) { func TestParseMultiString(t *testing.T) {
(&App{ (&App{
Flags: []Flag{ Flags: []Flag{