Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into paths-take-files
Browse files Browse the repository at this point in the history
  • Loading branch information
ErinCall committed Oct 13, 2020
2 parents 5bb54ac + cd82784 commit 0d11bd5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion docs/v2/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion flag_string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion flag_timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0d11bd5

Please sign in to comment.