Skip to content

Commit b816af8

Browse files
committed
Return an error when parsing environment variables for values fails
Currently cli silently (aside from IntSlice and Int64Slice which oddly printed directly to the error stream) ignores failures that occur when parsing environment variables for their value for flags that define environment variables. Instead, we should propogate up the error to the user. This is accomplished in a backwards compatible manner by adding a new, internal, interface which defines an applyWithError function that all flags here define. In v2, when we can modify the interface, we can drop this secondary interface and modify `Apply` to return an error.
1 parent 60cf8c7 commit b816af8

7 files changed

Lines changed: 328 additions & 64 deletions

File tree

‎app.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ func (a *App) Run(arguments []string) (err error) {
173173
a.Setup()
174174

175175
// parse flags
176-
set := flagSet(a.Name, a.Flags)
176+
set, err := flagSet(a.Name, a.Flags)
177+
if err != nil {
178+
return err
179+
}
180+
177181
set.SetOutput(ioutil.Discard)
178182
err = set.Parse(arguments[1:])
179183
nerr := normalizeFlags(a.Flags, set)
@@ -288,7 +292,11 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
288292
}
289293

290294
// parse flags
291-
set := flagSet(a.Name, a.Flags)
295+
set, err := flagSet(a.Name, a.Flags)
296+
if err != nil {
297+
return err
298+
}
299+
292300
set.SetOutput(ioutil.Discard)
293301
err = set.Parse(ctx.Args().Tail())
294302
nerr := normalizeFlags(a.Flags, set)

‎app_test.go‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,11 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
14021402
func TestHandleAction_WithNonFuncAction(t *testing.T) {
14031403
app := NewApp()
14041404
app.Action = 42
1405-
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
1405+
fs, err := flagSet(app.Name, app.Flags)
1406+
if err != nil {
1407+
t.Errorf("error creating FlagSet: %s", err)
1408+
}
1409+
err = HandleAction(app.Action, NewContext(app, fs, nil))
14061410

14071411
if err == nil {
14081412
t.Fatalf("expected to receive error from Run, got none")
@@ -1426,7 +1430,11 @@ func TestHandleAction_WithNonFuncAction(t *testing.T) {
14261430
func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
14271431
app := NewApp()
14281432
app.Action = func() string { return "" }
1429-
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
1433+
fs, err := flagSet(app.Name, app.Flags)
1434+
if err != nil {
1435+
t.Errorf("error creating FlagSet: %s", err)
1436+
}
1437+
err = HandleAction(app.Action, NewContext(app, fs, nil))
14301438

14311439
if err == nil {
14321440
t.Fatalf("expected to receive error from Run, got none")
@@ -1450,7 +1458,11 @@ func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
14501458
func TestHandleAction_WithInvalidFuncReturnSignature(t *testing.T) {
14511459
app := NewApp()
14521460
app.Action = func(_ *Context) (int, error) { return 0, nil }
1453-
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
1461+
fs, err := flagSet(app.Name, app.Flags)
1462+
if err != nil {
1463+
t.Errorf("error creating FlagSet: %s", err)
1464+
}
1465+
err = HandleAction(app.Action, NewContext(app, fs, nil))
14541466

14551467
if err == nil {
14561468
t.Fatalf("expected to receive error from Run, got none")
@@ -1481,5 +1493,9 @@ func TestHandleAction_WithUnknownPanic(t *testing.T) {
14811493
fn(ctx)
14821494
return nil
14831495
}
1484-
HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
1496+
fs, err := flagSet(app.Name, app.Flags)
1497+
if err != nil {
1498+
t.Errorf("error creating FlagSet: %s", err)
1499+
}
1500+
HandleAction(app.Action, NewContext(app, fs, nil))
14851501
}

‎command.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ func (c Command) Run(ctx *Context) (err error) {
9191
c.Flags = append(c.Flags, BashCompletionFlag)
9292
}
9393

94-
set := flagSet(c.Name, c.Flags)
94+
set, err := flagSet(c.Name, c.Flags)
95+
if err != nil {
96+
return err
97+
}
9598
set.SetOutput(ioutil.Discard)
9699

97100
if c.SkipFlagParsing {

‎context.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ func (c *Context) Parent() *Context {
147147
return c.parentContext
148148
}
149149

150+
// value returns the value of the flag coressponding to `name`
151+
func (c *Context) value(name string) interface{} {
152+
return c.flagSet.Lookup(name).Value.(flag.Getter).Get()
153+
}
154+
150155
// Args contains apps console arguments
151156
type Args []string
152157

‎context_test.go‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,30 @@ func TestContext_IsSet(t *testing.T) {
184184
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
185185
// Should be moved to `flag_test` in v2
186186
func TestContext_IsSet_fromEnv(t *testing.T) {
187-
var timeoutIsSet, tIsSet, noEnvVarIsSet, nIsSet, passwordIsSet, pIsSet bool
187+
var (
188+
timeoutIsSet, tIsSet bool
189+
noEnvVarIsSet, nIsSet bool
190+
passwordIsSet, pIsSet bool
191+
unparsableIsSet, uIsSet bool
192+
)
188193

189194
os.Clearenv()
190195
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
191196
os.Setenv("APP_PASSWORD", "")
192197
a := App{
193198
Flags: []Flag{
194199
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
195-
Float64Flag{Name: "password, p", EnvVar: "APP_PASSWORD"},
200+
StringFlag{Name: "password, p", EnvVar: "APP_PASSWORD"},
201+
Float64Flag{Name: "unparsable, u", EnvVar: "APP_UNPARSABLE"},
196202
Float64Flag{Name: "no-env-var, n"},
197203
},
198204
Action: func(ctx *Context) error {
199205
timeoutIsSet = ctx.IsSet("timeout")
200206
tIsSet = ctx.IsSet("t")
201207
passwordIsSet = ctx.IsSet("password")
202208
pIsSet = ctx.IsSet("p")
209+
unparsableIsSet = ctx.IsSet("unparsable")
210+
uIsSet = ctx.IsSet("u")
203211
noEnvVarIsSet = ctx.IsSet("no-env-var")
204212
nIsSet = ctx.IsSet("n")
205213
return nil
@@ -212,6 +220,11 @@ func TestContext_IsSet_fromEnv(t *testing.T) {
212220
expect(t, pIsSet, true)
213221
expect(t, noEnvVarIsSet, false)
214222
expect(t, nIsSet, false)
223+
224+
os.Setenv("APP_UNPARSABLE", "foobar")
225+
a.Run([]string{"run"})
226+
expect(t, unparsableIsSet, false)
227+
expect(t, uIsSet, false)
215228
}
216229

217230
func TestContext_GlobalIsSet(t *testing.T) {
@@ -236,21 +249,33 @@ func TestContext_GlobalIsSet(t *testing.T) {
236249
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
237250
// Should be moved to `flag_test` in v2
238251
func TestContext_GlobalIsSet_fromEnv(t *testing.T) {
239-
var timeoutIsSet, tIsSet, noEnvVarIsSet, nIsSet bool
252+
var (
253+
timeoutIsSet, tIsSet bool
254+
noEnvVarIsSet, nIsSet bool
255+
passwordIsSet, pIsSet bool
256+
unparsableIsSet, uIsSet bool
257+
)
240258

241259
os.Clearenv()
242260
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
261+
os.Setenv("APP_PASSWORD", "")
243262
a := App{
244263
Flags: []Flag{
245264
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
265+
StringFlag{Name: "password, p", EnvVar: "APP_PASSWORD"},
246266
Float64Flag{Name: "no-env-var, n"},
267+
Float64Flag{Name: "unparsable, u", EnvVar: "APP_UNPARSABLE"},
247268
},
248269
Commands: []Command{
249270
{
250271
Name: "hello",
251272
Action: func(ctx *Context) error {
252273
timeoutIsSet = ctx.GlobalIsSet("timeout")
253274
tIsSet = ctx.GlobalIsSet("t")
275+
passwordIsSet = ctx.GlobalIsSet("password")
276+
pIsSet = ctx.GlobalIsSet("p")
277+
unparsableIsSet = ctx.GlobalIsSet("unparsable")
278+
uIsSet = ctx.GlobalIsSet("u")
254279
noEnvVarIsSet = ctx.GlobalIsSet("no-env-var")
255280
nIsSet = ctx.GlobalIsSet("n")
256281
return nil
@@ -261,8 +286,15 @@ func TestContext_GlobalIsSet_fromEnv(t *testing.T) {
261286
a.Run([]string{"run", "hello"})
262287
expect(t, timeoutIsSet, true)
263288
expect(t, tIsSet, true)
289+
expect(t, passwordIsSet, true)
290+
expect(t, pIsSet, true)
264291
expect(t, noEnvVarIsSet, false)
265292
expect(t, nIsSet, false)
293+
294+
os.Setenv("APP_UNPARSABLE", "foobar")
295+
a.Run([]string{"run"})
296+
expect(t, unparsableIsSet, false)
297+
expect(t, uIsSet, false)
266298
}
267299

268300
func TestContext_NumFlags(t *testing.T) {

0 commit comments

Comments
 (0)