Merge remote-tracking branch 'origin/master' into paths-take-files
This commit is contained in:
commit
0d11bd55f5
2
app.go
2
app.go
@ -64,7 +64,7 @@ type App struct {
|
|||||||
Action ActionFunc
|
Action ActionFunc
|
||||||
// Execute this function if the proper command cannot be found
|
// Execute this function if the proper command cannot be found
|
||||||
CommandNotFound CommandNotFoundFunc
|
CommandNotFound CommandNotFoundFunc
|
||||||
// Execute this function if an usage error occurs
|
// Execute this function if a usage error occurs
|
||||||
OnUsageError OnUsageErrorFunc
|
OnUsageError OnUsageErrorFunc
|
||||||
// Compilation date
|
// Compilation date
|
||||||
Compiled time.Time
|
Compiled time.Time
|
||||||
|
@ -241,7 +241,7 @@ func (c *Command) startApp(ctx *Context) error {
|
|||||||
app.HideHelpCommand = c.HideHelpCommand
|
app.HideHelpCommand = c.HideHelpCommand
|
||||||
|
|
||||||
app.Version = ctx.App.Version
|
app.Version = ctx.App.Version
|
||||||
app.HideVersion = ctx.App.HideVersion
|
app.HideVersion = true
|
||||||
app.Compiled = ctx.App.Compiled
|
app.Compiled = ctx.App.Compiled
|
||||||
app.Writer = ctx.App.Writer
|
app.Writer = ctx.App.Writer
|
||||||
app.ErrWriter = ctx.App.ErrWriter
|
app.ErrWriter = ctx.App.ErrWriter
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
@ -661,7 +661,7 @@ func main() {
|
|||||||
You can make a flag required by setting the `Required` field to `true`. If a user
|
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.
|
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"
|
"error": "Required flag \"lang\" not set"
|
||||||
|
@ -124,7 +124,9 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
if f.Value == nil {
|
||||||
f.Value = &StringSlice{}
|
f.Value = &StringSlice{}
|
||||||
|
}
|
||||||
destination := f.Value
|
destination := f.Value
|
||||||
if f.Destination != nil {
|
if f.Destination != nil {
|
||||||
destination = f.Destination
|
destination = f.Destination
|
||||||
|
25
flag_test.go
25
flag_test.go
@ -386,6 +386,20 @@ func TestStringSliceFlagApply_SetsAllNames(t *testing.T) {
|
|||||||
expect(t, err, nil)
|
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) {
|
func TestStringSliceFlagApply_DefaultValueWithDestination(t *testing.T) {
|
||||||
defValue := []string{"UA", "US"}
|
defValue := []string{"UA", "US"}
|
||||||
|
|
||||||
@ -1836,6 +1850,17 @@ func TestTimestampFlagApply(t *testing.T) {
|
|||||||
expect(t, *fl.Value.timestamp, expectedResult)
|
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) {
|
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)
|
||||||
|
@ -118,7 +118,9 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error {
|
|||||||
if f.Layout == "" {
|
if f.Layout == "" {
|
||||||
return fmt.Errorf("timestamp Layout is required")
|
return fmt.Errorf("timestamp Layout is required")
|
||||||
}
|
}
|
||||||
|
if f.Value == nil {
|
||||||
f.Value = &Timestamp{}
|
f.Value = &Timestamp{}
|
||||||
|
}
|
||||||
f.Value.SetLayout(f.Layout)
|
f.Value.SetLayout(f.Layout)
|
||||||
|
|
||||||
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
Loading…
Reference in New Issue
Block a user