Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,14 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context
}

for _, flag := range cmd.allFlags() {
isSet := flag.IsSet()
if err := flag.PostParse(); err != nil {
return ctx, err
}
// add env set flags here
if !isSet && flag.IsSet() {
cmd.setFlags[flag] = struct{}{}
}
}

if cmd.After != nil && !cmd.Root().shellCompletion {
Expand Down
22 changes: 22 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,28 @@ func TestParseGenericFromEnv(t *testing.T) {
assert.NoError(t, cmd.Run(buildTestContext(t), []string{"run"}))
}

func TestFlagActionFromEnv(t *testing.T) {
t.Setenv("X", "42")
x := 0

cmd := &Command{
Flags: []Flag{
&IntFlag{
Name: "x",
Sources: EnvVars("X"),
Action: func(ctx context.Context, cmd *Command, v int) error {
x = v
return nil
},
},
},
}

assert.NoError(t, cmd.Run(buildTestContext(t), []string{"run"}))
assert.Equal(t, cmd.Int("x"), 42)
assert.Equal(t, x, 42)
}

func TestParseMultiString(t *testing.T) {
_ = (&Command{
Flags: []Flag{
Expand Down