Merge remote-tracking branch 'origin/master' into paths-take-files

This commit is contained in:
Erin Call 2020-10-13 11:33:31 -07:00
commit 0d11bd55f5
No known key found for this signature in database
GPG Key ID: 4071FF6C15B8DAD1
7 changed files with 79 additions and 5 deletions

2
app.go
View File

@ -64,7 +64,7 @@ type App struct {
Action ActionFunc
// Execute this function if the proper command cannot be found
CommandNotFound CommandNotFoundFunc
// Execute this function if an usage error occurs
// Execute this function if a usage error occurs
OnUsageError OnUsageErrorFunc
// Compilation date
Compiled time.Time

View File

@ -241,7 +241,7 @@ func (c *Command) startApp(ctx *Context) error {
app.HideHelpCommand = c.HideHelpCommand
app.Version = ctx.App.Version
app.HideVersion = ctx.App.HideVersion
app.HideVersion = true
app.Compiled = ctx.App.Compiled
app.Writer = ctx.App.Writer
app.ErrWriter = ctx.App.ErrWriter

View File

@ -377,3 +377,48 @@ func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) {
}
}
func TestCommand_NoVersionFlagOnCommands(t *testing.T) {
app := &App{
Version: "some version",
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
HideHelp: true,
Action: func(c *Context) error {
if len(c.App.VisibleFlags()) != 0 {
t.Fatal("unexpected flag on command")
}
return nil
},
},
},
}
err := app.Run([]string{"foo", "bar"})
expect(t, err, nil)
}
func TestCommand_CanAddVFlagOnCommands(t *testing.T) {
app := &App{
Version: "some version",
Writer: ioutil.Discard,
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
Flags: []Flag{
&BoolFlag{
Name: "v",
},
},
},
},
}
err := app.Run([]string{"foo", "bar"})
expect(t, err, nil)
}

View File

@ -661,7 +661,7 @@ func main() {
You can make a flag required by setting the `Required` field to `true`. If a user
does not provide a required flag, they will be shown an error message.
Take for example this app that reqiures the `lang` flag:
Take for example this app that requires the `lang` flag:
<!-- {
"error": "Required flag \"lang\" not set"

View File

@ -124,7 +124,9 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
}
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
f.Value = &StringSlice{}
if f.Value == nil {
f.Value = &StringSlice{}
}
destination := f.Value
if f.Destination != nil {
destination = f.Destination

View File

@ -386,6 +386,20 @@ func TestStringSliceFlagApply_SetsAllNames(t *testing.T) {
expect(t, err, nil)
}
func TestStringSliceFlagApply_UsesEnvValues(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()
_ = os.Setenv("MY_GOAT", "vincent van goat,scape goat")
var val StringSlice
fl := StringSliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: &val}
set := flag.NewFlagSet("test", 0)
_ = fl.Apply(set)
err := set.Parse(nil)
expect(t, err, nil)
expect(t, val.Value(), NewStringSlice("vincent van goat", "scape goat").Value())
}
func TestStringSliceFlagApply_DefaultValueWithDestination(t *testing.T) {
defValue := []string{"UA", "US"}
@ -1836,6 +1850,17 @@ func TestTimestampFlagApply(t *testing.T) {
expect(t, *fl.Value.timestamp, expectedResult)
}
func TestTimestampFlagApplyValue(t *testing.T) {
expectedResult, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: time.RFC3339, Value: NewTimestamp(expectedResult)}
set := flag.NewFlagSet("test", 0)
_ = fl.Apply(set)
err := set.Parse([]string{""})
expect(t, err, nil)
expect(t, *fl.Value.timestamp, expectedResult)
}
func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "randomlayout"}
set := flag.NewFlagSet("test", 0)

View File

@ -118,7 +118,9 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error {
if f.Layout == "" {
return fmt.Errorf("timestamp Layout is required")
}
f.Value = &Timestamp{}
if f.Value == nil {
f.Value = &Timestamp{}
}
f.Value.SetLayout(f.Layout)
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {