Merge branch 'master' into patch-1

This commit is contained in:
Ajitem Sahasrabuddhe 2020-01-13 22:09:06 +05:30 committed by GitHub
commit aee5241828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 185 additions and 194 deletions

12
.github/ISSUE_TEMPLATE/question.md vendored Normal file
View File

@ -0,0 +1,12 @@
---
name: ask a question
about: ask us question - assume stackoverflow's guidelines apply here
title: 'q: ( your question title goes here )'
labels: 'type: question, status: triage, version: v2'
assignees: lynncyrin
---
## my question is...
_**( Put the question text here )**_

View File

@ -28,6 +28,11 @@ jobs:
with: with:
go-version: ${{ matrix.go }} go-version: ${{ matrix.go }}
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: 12.x
- name: Set GOPATH and PATH - name: Set GOPATH and PATH
run: | run: |
echo "::set-env name=GOPATH::$(dirname $GITHUB_WORKSPACE)" echo "::set-env name=GOPATH::$(dirname $GITHUB_WORKSPACE)"
@ -44,7 +49,7 @@ jobs:
mkdir -p $GOPATH/bin mkdir -p $GOPATH/bin
curl -L -o $GOPATH/bin/gfmrun "https://github.com/urfave/gfmrun/releases/download/v1.2.14/gfmrun-$(go env GOOS)-amd64-v1.2.14" curl -L -o $GOPATH/bin/gfmrun "https://github.com/urfave/gfmrun/releases/download/v1.2.14/gfmrun-$(go env GOOS)-amd64-v1.2.14"
chmod +x $GOPATH/bin/gfmrun chmod +x $GOPATH/bin/gfmrun
npm install markdown-toc npm install -g markdown-toc@1.2.0
- name: Run Tests - name: Run Tests
run: | run: |

View File

@ -4,6 +4,7 @@ package altsrc
import ( import (
"flag" "flag"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )

View File

@ -3,12 +3,13 @@ package altsrc
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/urfave/cli/v2"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/urfave/cli/v2"
) )
type testApplyInputSource struct { type testApplyInputSource struct {

View File

@ -1,8 +1,9 @@
package altsrc package altsrc
import ( import (
"github.com/urfave/cli/v2"
"time" "time"
"github.com/urfave/cli/v2"
) )
// InputSourceContext is an interface used to allow // InputSourceContext is an interface used to allow

View File

@ -2,10 +2,11 @@ package altsrc
import ( import (
"flag" "flag"
"github.com/urfave/cli/v2"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"github.com/urfave/cli/v2"
) )
const ( const (

View File

@ -3,11 +3,12 @@ package altsrc
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/urfave/cli/v2"
"io" "io"
"io/ioutil" "io/ioutil"
"strings" "strings"
"time" "time"
"github.com/urfave/cli/v2"
) )
// NewJSONSourceFromFlagFunc returns a func that takes a cli.Context // NewJSONSourceFromFlagFunc returns a func that takes a cli.Context
@ -78,9 +79,9 @@ func (x *jsonSource) Duration(name string) (time.Duration, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
v, ok := (time.Duration)(0), false v, ok := i.(time.Duration)
if v, ok = i.(time.Duration); !ok { if !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name) return 0, fmt.Errorf("unexpected type %T for %q", i, name)
} }
return v, nil return v, nil
} }
@ -90,9 +91,9 @@ func (x *jsonSource) Float64(name string) (float64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
v, ok := (float64)(0), false v, ok := i.(float64)
if v, ok = i.(float64); !ok { if !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name) return 0, fmt.Errorf("unexpected type %T for %q", i, name)
} }
return v, nil return v, nil
} }
@ -102,9 +103,9 @@ func (x *jsonSource) String(name string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
v, ok := "", false v, ok := i.(string)
if v, ok = i.(string); !ok { if !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name) return "", fmt.Errorf("unexpected type %T for %q", i, name)
} }
return v, nil return v, nil
} }
@ -160,9 +161,9 @@ func (x *jsonSource) Generic(name string) (cli.Generic, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
v, ok := (cli.Generic)(nil), false v, ok := i.(cli.Generic)
if v, ok = i.(cli.Generic); !ok { if !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name) return nil, fmt.Errorf("unexpected type %T for %q", i, name)
} }
return v, nil return v, nil
} }
@ -172,9 +173,9 @@ func (x *jsonSource) Bool(name string) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
v, ok := false, false v, ok := i.(bool)
if v, ok = i.(bool); !ok { if !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name) return false, fmt.Errorf("unexpected type %T for %q", i, name)
} }
return v, nil return v, nil
} }

View File

@ -2,10 +2,11 @@ package altsrc
import ( import (
"fmt" "fmt"
"github.com/urfave/cli/v2"
"reflect" "reflect"
"strings" "strings"
"time" "time"
"github.com/urfave/cli/v2"
) )
// MapInputSource implements InputSourceContext to return // MapInputSource implements InputSourceContext to return

View File

@ -20,6 +20,6 @@ func TestMapDuration(t *testing.T) {
d, err = inputSource.Duration("duration_of_string_type") d, err = inputSource.Duration("duration_of_string_type")
expect(t, time.Minute, d) expect(t, time.Minute, d)
expect(t, nil, err) expect(t, nil, err)
d, err = inputSource.Duration("duration_of_int_type") _, err = inputSource.Duration("duration_of_int_type")
refute(t, nil, err) refute(t, nil, err)
} }

View File

@ -2,10 +2,11 @@ package altsrc
import ( import (
"flag" "flag"
"github.com/urfave/cli/v2"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"github.com/urfave/cli/v2"
) )
func TestCommandTomFileTest(t *testing.T) { func TestCommandTomFileTest(t *testing.T) {

View File

@ -2,10 +2,11 @@ package altsrc
import ( import (
"flag" "flag"
"github.com/urfave/cli/v2"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"github.com/urfave/cli/v2"
) )
func TestCommandYamlFileTest(t *testing.T) { func TestCommandYamlFileTest(t *testing.T) {

11
app.go
View File

@ -7,7 +7,6 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"sort" "sort"
"time" "time"
) )
@ -485,16 +484,6 @@ func (a *App) VisibleFlags() []Flag {
return visibleFlags(a.Flags) return visibleFlags(a.Flags)
} }
func (a *App) hasFlag(flag Flag) bool {
for _, f := range a.Flags {
if reflect.DeepEqual(flag, f) {
return true
}
}
return false
}
func (a *App) errWriter() io.Writer { func (a *App) errWriter() io.Writer {
// When the app ErrWriter is nil use the package level one. // When the app ErrWriter is nil use the package level one.
if a.ErrWriter == nil { if a.ErrWriter == nil {

View File

@ -612,7 +612,7 @@ func TestApp_UseShortOptionHandling(t *testing.T) {
var name string var name string
expected := "expectedName" expected := "expectedName"
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
app.Flags = []Flag{ app.Flags = []Flag{
&BoolFlag{Name: "one", Aliases: []string{"o"}}, &BoolFlag{Name: "one", Aliases: []string{"o"}},
@ -633,7 +633,7 @@ func TestApp_UseShortOptionHandling(t *testing.T) {
} }
func TestApp_UseShortOptionHandling_missing_value(t *testing.T) { func TestApp_UseShortOptionHandling_missing_value(t *testing.T) {
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
app.Flags = []Flag{ app.Flags = []Flag{
&StringFlag{Name: "name", Aliases: []string{"n"}}, &StringFlag{Name: "name", Aliases: []string{"n"}},
@ -648,7 +648,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
var name string var name string
expected := "expectedName" expected := "expectedName"
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -673,7 +673,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
} }
func TestApp_UseShortOptionHandlingCommand_missing_value(t *testing.T) { func TestApp_UseShortOptionHandlingCommand_missing_value(t *testing.T) {
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -692,7 +692,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
var name string var name string
expected := "expectedName" expected := "expectedName"
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -722,7 +722,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
} }
func TestApp_UseShortOptionHandlingSubCommand_missing_value(t *testing.T) { func TestApp_UseShortOptionHandlingSubCommand_missing_value(t *testing.T) {
app := NewApp() app := newTestApp()
app.UseShortOptionHandling = true app.UseShortOptionHandling = true
command := &Command{ command := &Command{
Name: "cmd", Name: "cmd",
@ -859,30 +859,12 @@ func TestApp_DefaultStdout(t *testing.T) {
} }
} }
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) { func TestApp_SetStdout(t *testing.T) {
w := &mockWriter{} var w bytes.Buffer
app := &App{ app := &App{
Name: "test", Name: "test",
Writer: w, Writer: &w,
} }
err := app.Run([]string{"help"}) err := app.Run([]string{"help"})
@ -891,7 +873,7 @@ func TestApp_SetStdout(t *testing.T) {
t.Fatalf("Run error: %s", err) t.Fatalf("Run error: %s", err)
} }
if len(w.written) == 0 { if w.Len() == 0 {
t.Error("App did not write output to desired writer.") t.Error("App did not write output to desired writer.")
} }
} }
@ -925,6 +907,7 @@ func TestApp_BeforeFunc(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
&StringFlag{Name: "opt"}, &StringFlag{Name: "opt"},
}, },
Writer: ioutil.Discard,
} }
// run with the Before() func succeeding // run with the Before() func succeeding
@ -1186,7 +1169,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
for _, test := range tdata { for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) { t.Run(test.testCase, func(t *testing.T) {
// setup // setup
app := NewApp() app := newTestApp()
app.Flags = test.appFlags app.Flags = test.appFlags
app.Commands = test.appCommands app.Commands = test.appCommands
@ -1280,7 +1263,6 @@ func TestApp_OrderOfOperations(t *testing.T) {
app := &App{ app := &App{
EnableBashCompletion: true, EnableBashCompletion: true,
BashComplete: func(c *Context) { BashComplete: func(c *Context) {
_, _ = fmt.Fprintf(os.Stderr, "---> BashComplete(%#v)\n", c)
counts.Total++ counts.Total++
counts.ShellComplete = counts.Total counts.ShellComplete = counts.Total
}, },
@ -1289,6 +1271,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
counts.OnUsageError = counts.Total counts.OnUsageError = counts.Total
return errors.New("hay OnUsageError") return errors.New("hay OnUsageError")
}, },
Writer: ioutil.Discard,
} }
beforeNoError := func(c *Context) error { beforeNoError := func(c *Context) error {
@ -1422,49 +1405,50 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
} }
for _, flagSet := range subcommandHelpTopics { for _, flagSet := range subcommandHelpTopics {
t.Logf("==> checking with flags %v", flagSet) t.Run(fmt.Sprintf("checking with flags %v", flagSet), func(t *testing.T) {
app := &App{} app := &App{}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
app.Writer = buf app.Writer = buf
subCmdBar := &Command{ subCmdBar := &Command{
Name: "bar", Name: "bar",
Usage: "does bar things", Usage: "does bar things",
}
subCmdBaz := &Command{
Name: "baz",
Usage: "does baz things",
}
cmd := &Command{
Name: "foo",
Description: "descriptive wall of text about how it does foo things",
Subcommands: []*Command{subCmdBar, subCmdBaz},
Action: func(c *Context) error { return nil },
}
app.Commands = []*Command{cmd}
err := app.Run(flagSet)
if err != nil {
t.Error(err)
}
output := buf.String()
if strings.Contains(output, "No help topic for") {
t.Errorf("expect a help topic, got none: \n%q", output)
}
for _, shouldContain := range []string{
cmd.Name, cmd.Description,
subCmdBar.Name, subCmdBar.Usage,
subCmdBaz.Name, subCmdBaz.Usage,
} {
if !strings.Contains(output, shouldContain) {
t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output)
} }
} subCmdBaz := &Command{
Name: "baz",
Usage: "does baz things",
}
cmd := &Command{
Name: "foo",
Description: "descriptive wall of text about how it does foo things",
Subcommands: []*Command{subCmdBar, subCmdBaz},
Action: func(c *Context) error { return nil },
}
app.Commands = []*Command{cmd}
err := app.Run(flagSet)
if err != nil {
t.Error(err)
}
output := buf.String()
if strings.Contains(output, "No help topic for") {
t.Errorf("expect a help topic, got none: \n%q", output)
}
for _, shouldContain := range []string{
cmd.Name, cmd.Description,
subCmdBar.Name, subCmdBar.Usage,
subCmdBaz.Name, subCmdBaz.Usage,
} {
if !strings.Contains(output, shouldContain) {
t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output)
}
}
})
} }
} }
@ -1610,31 +1594,31 @@ func TestApp_Run_Help(t *testing.T) {
var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}} var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}}
for _, args := range helpArguments { for _, args := range helpArguments {
buf := new(bytes.Buffer) t.Run(fmt.Sprintf("checking with arguments %v", args), func(t *testing.T) {
t.Logf("==> checking with arguments %v", args) buf := new(bytes.Buffer)
app := &App{ app := &App{
Name: "boom", Name: "boom",
Usage: "make an explosive entrance", Usage: "make an explosive entrance",
Writer: buf, Writer: buf,
Action: func(c *Context) error { Action: func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return nil return nil
}, },
} }
err := app.Run(args) err := app.Run(args)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
output := buf.String() output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if !strings.Contains(output, "boom - make an explosive entrance") { if !strings.Contains(output, "boom - make an explosive entrance") {
t.Errorf("want help to contain %q, did not: \n%q", "boom - make an explosive entrance", output) t.Errorf("want help to contain %q, did not: \n%q", "boom - make an explosive entrance", output)
} }
})
} }
} }
@ -1642,32 +1626,32 @@ func TestApp_Run_Version(t *testing.T) {
var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}} var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}}
for _, args := range versionArguments { for _, args := range versionArguments {
buf := new(bytes.Buffer) t.Run(fmt.Sprintf("checking with arguments %v", args), func(t *testing.T) {
t.Logf("==> checking with arguments %v", args) buf := new(bytes.Buffer)
app := &App{ app := &App{
Name: "boom", Name: "boom",
Usage: "make an explosive entrance", Usage: "make an explosive entrance",
Version: "0.1.0", Version: "0.1.0",
Writer: buf, Writer: buf,
Action: func(c *Context) error { Action: func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return nil return nil
}, },
} }
err := app.Run(args) err := app.Run(args)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
output := buf.String() output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if !strings.Contains(output, "0.1.0") { if !strings.Contains(output, "0.1.0") {
t.Errorf("want version to contain %q, did not: \n%q", "0.1.0", output) t.Errorf("want version to contain %q, did not: \n%q", "0.1.0", output)
} }
})
} }
} }
@ -1835,6 +1819,7 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
Action: func(c *Context) error { return nil }, Action: func(c *Context) error { return nil },
Before: func(c *Context) error { return fmt.Errorf("before error") }, Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) error { return fmt.Errorf("after error") }, After: func(c *Context) error { return fmt.Errorf("after error") },
Writer: ioutil.Discard,
} }
err := app.Run([]string{"foo"}) err := app.Run([]string{"foo"})
@ -1979,7 +1964,8 @@ func (c *customBoolFlag) IsSet() bool {
func TestCustomFlagsUnused(t *testing.T) { func TestCustomFlagsUnused(t *testing.T) {
app := &App{ app := &App{
Flags: []Flag{&customBoolFlag{"custom"}}, Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
} }
err := app.Run([]string{"foo"}) err := app.Run([]string{"foo"})
@ -1990,7 +1976,8 @@ func TestCustomFlagsUnused(t *testing.T) {
func TestCustomFlagsUsed(t *testing.T) { func TestCustomFlagsUsed(t *testing.T) {
app := &App{ app := &App{
Flags: []Flag{&customBoolFlag{"custom"}}, Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
} }
err := app.Run([]string{"foo", "--custom=bar"}) err := app.Run([]string{"foo", "--custom=bar"})
@ -2000,7 +1987,9 @@ func TestCustomFlagsUsed(t *testing.T) {
} }
func TestCustomHelpVersionFlags(t *testing.T) { func TestCustomHelpVersionFlags(t *testing.T) {
app := &App{} app := &App{
Writer: ioutil.Discard,
}
// Be sure to reset the global flags // Be sure to reset the global flags
defer func(helpFlag Flag, versionFlag Flag) { defer func(helpFlag Flag, versionFlag Flag) {
@ -2018,7 +2007,7 @@ func TestCustomHelpVersionFlags(t *testing.T) {
} }
func TestHandleExitCoder_Default(t *testing.T) { func TestHandleExitCoder_Default(t *testing.T) {
app := NewApp() app := newTestApp()
fs, err := flagSet(app.Name, app.Flags) fs, err := flagSet(app.Name, app.Flags)
if err != nil { if err != nil {
t.Errorf("error creating FlagSet: %s", err) t.Errorf("error creating FlagSet: %s", err)
@ -2034,7 +2023,7 @@ func TestHandleExitCoder_Default(t *testing.T) {
} }
func TestHandleExitCoder_Custom(t *testing.T) { func TestHandleExitCoder_Custom(t *testing.T) {
app := NewApp() app := newTestApp()
fs, err := flagSet(app.Name, app.Flags) fs, err := flagSet(app.Name, app.Flags)
if err != nil { if err != nil {
t.Errorf("error creating FlagSet: %s", err) t.Errorf("error creating FlagSet: %s", err)
@ -2091,6 +2080,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
Action: func(ctx *Context) error { Action: func(ctx *Context) error {
return fmt.Errorf("should not get here") return fmt.Errorf("should not get here")
}, },
Writer: ioutil.Discard,
} }
err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"}) err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"})
if err != nil { if err != nil {
@ -2101,7 +2091,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) { func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
testCode := 104 testCode := 104
app := NewApp() app := newTestApp()
app.Commands = []*Command{ app.Commands = []*Command{
{ {
Name: "cmd", Name: "cmd",
@ -2120,7 +2110,6 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
var exitCodeFromExitErrHandler int var exitCodeFromExitErrHandler int
app.ExitErrHandler = func(c *Context, err error) { app.ExitErrHandler = func(c *Context, err error) {
if exitErr, ok := err.(ExitCoder); ok { if exitErr, ok := err.(ExitCoder); ok {
t.Log(exitErr)
exitCodeFromExitErrHandler = exitErr.ExitCode() exitCodeFromExitErrHandler = exitErr.ExitCode()
} }
} }
@ -2151,3 +2140,9 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
t.Errorf("exitCodeFromOsExiter valeu should be %v, but its value is %v", testCode, exitCodeFromExitErrHandler) t.Errorf("exitCodeFromOsExiter valeu should be %v, but its value is %v", testCode, exitCodeFromExitErrHandler)
} }
} }
func newTestApp() *App {
a := NewApp()
a.Writer = ioutil.Discard
return a
}

View File

@ -166,7 +166,7 @@ func TocActionFunc(c *cli.Context) error {
filename = "README.md" filename = "README.md"
} }
err := runCmd("node_modules/.bin/markdown-toc", "-i", filename) err := runCmd("markdown-toc", "-i", filename)
if err != nil { if err != nil {
return err return err
} }

View File

@ -93,7 +93,7 @@ func TestParseAndRunShortOpts(t *testing.T) {
}, },
} }
app := NewApp() app := newTestApp()
app.Commands = []*Command{cmd} app.Commands = []*Command{cmd}
err := app.Run(c.testArgs) err := app.Run(c.testArgs)
@ -116,6 +116,7 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
}, },
}, },
}, },
Writer: ioutil.Discard,
} }
err := app.Run([]string{"foo", "bar"}) err := app.Run([]string{"foo", "bar"})
@ -317,12 +318,12 @@ func TestCommandSkipFlagParsing(t *testing.T) {
&StringFlag{Name: "flag"}, &StringFlag{Name: "flag"},
}, },
Action: func(c *Context) error { Action: func(c *Context) error {
fmt.Printf("%+v\n", c.String("flag"))
args = c.Args() args = c.Args()
return nil return nil
}, },
}, },
}, },
Writer: ioutil.Discard,
} }
err := app.Run(c.testArgs) err := app.Run(c.testArgs)

View File

@ -17,7 +17,6 @@ type Context struct {
App *App App *App
Command *Command Command *Command
shellComplete bool shellComplete bool
setFlags map[string]bool
flagSet *flag.FlagSet flagSet *flag.FlagSet
parentContext *Context parentContext *Context
} }

View File

@ -6,7 +6,7 @@ import (
) )
func testApp() *App { func testApp() *App {
app := NewApp() app := newTestApp()
app.Name = "greet" app.Name = "greet"
app.Flags = []Flag{ app.Flags = []Flag{
&StringFlag{ &StringFlag{

16
flag.go
View File

@ -203,12 +203,9 @@ func withEnvHint(envVars []string, str string) string {
return str + envText return str + envText
} }
func flagNames(f Flag) []string { func flagNames(name string, aliases []string) []string {
var ret []string var ret []string
name := flagStringField(f, "Name")
aliases := flagStringSliceField(f, "Aliases")
for _, part := range append([]string{name}, aliases...) { for _, part := range append([]string{name}, aliases...) {
// v1 -> v2 migration warning zone: // v1 -> v2 migration warning zone:
// Strip off anything after the first found comma or space, which // Strip off anything after the first found comma or space, which
@ -231,17 +228,6 @@ func flagStringSliceField(f Flag, name string) []string {
return []string{} return []string{}
} }
func flagStringField(f Flag, name string) string {
fv := flagValue(f)
field := fv.FieldByName(name)
if field.IsValid() {
return field.String()
}
return ""
}
func withFileHint(filePath, str string) string { func withFileHint(filePath, str string) string {
fileText := "" fileText := ""
if filePath != "" { if filePath != "" {

View File

@ -34,7 +34,7 @@ func (f *BoolFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *BoolFlag) Names() []string { func (f *BoolFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *DurationFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *DurationFlag) Names() []string { func (f *DurationFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Float64Flag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *Float64Flag) Names() []string { func (f *Float64Flag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -90,7 +90,7 @@ func (f *Float64SliceFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *Float64SliceFlag) Names() []string { func (f *Float64SliceFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -39,7 +39,7 @@ func (f *GenericFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *GenericFlag) Names() []string { func (f *GenericFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *IntFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *IntFlag) Names() []string { func (f *IntFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Int64Flag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *Int64Flag) Names() []string { func (f *Int64Flag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -91,7 +91,7 @@ func (f *Int64SliceFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *Int64SliceFlag) Names() []string { func (f *Int64SliceFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -102,7 +102,7 @@ func (f *IntSliceFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *IntSliceFlag) Names() []string { func (f *IntSliceFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -30,7 +30,7 @@ func (f *PathFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *PathFlag) Names() []string { func (f *PathFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -31,7 +31,7 @@ func (f *StringFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *StringFlag) Names() []string { func (f *StringFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -86,7 +86,7 @@ func (f *StringSliceFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *StringSliceFlag) Names() []string { func (f *StringSliceFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -1706,7 +1706,7 @@ func TestTimestamp_set(t *testing.T) {
} }
func TestTimestampFlagApply(t *testing.T) { func TestTimestampFlagApply(t *testing.T) {
expectedResult, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") expectedResult, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: time.RFC3339} fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: time.RFC3339}
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
_ = fl.Apply(set) _ = fl.Apply(set)
@ -1719,6 +1719,7 @@ func TestTimestampFlagApply(t *testing.T) {
func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) { func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "randomlayout"} fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "randomlayout"}
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.SetOutput(ioutil.Discard)
_ = fl.Apply(set) _ = fl.Apply(set)
err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"}) err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})
@ -1728,6 +1729,7 @@ func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) { func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) {
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "Jan 2, 2006 at 3:04pm (MST)"} fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "Jan 2, 2006 at 3:04pm (MST)"}
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.SetOutput(ioutil.Discard)
_ = fl.Apply(set) _ = fl.Apply(set)
err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"}) err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})

View File

@ -86,7 +86,7 @@ func (f *TimestampFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *TimestampFlag) Names() []string { func (f *TimestampFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *UintFlag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *UintFlag) Names() []string { func (f *UintFlag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Uint64Flag) String() string {
// Names returns the names of the flag // Names returns the names of the flag
func (f *Uint64Flag) Names() []string { func (f *Uint64Flag) Names() []string {
return flagNames(f) return flagNames(f.Name, f.Aliases)
} }
// IsRequired returns whether or not the flag is required // IsRequired returns whether or not the flag is required

View File

@ -24,9 +24,3 @@ func expect(t *testing.T, a interface{}, b interface{}) {
t.Errorf("(%s:%d) Expected %v (type %v) - Got %v (type %v)", fn, line, b, reflect.TypeOf(b), a, reflect.TypeOf(a)) t.Errorf("(%s:%d) Expected %v (type %v) - Got %v (type %v)", fn, line, b, reflect.TypeOf(b), a, reflect.TypeOf(a))
} }
} }
func refute(t *testing.T, a interface{}, b interface{}) {
if reflect.DeepEqual(a, b) {
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}