Skip to content

Commit 59ec2a1

Browse files
authored
Merge pull request #1262 from dearchap/issue_1199
Allow -ve values for int, float & duration
2 parents c864c24 + d198aed commit 59ec2a1

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

altsrc/flag.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContex
197197
if err != nil {
198198
return err
199199
}
200-
if value > 0 {
201-
for _, name := range f.Names() {
202-
_ = f.set.Set(name, strconv.FormatInt(int64(value), 10))
203-
}
200+
for _, name := range f.Names() {
201+
_ = f.set.Set(name, strconv.FormatInt(int64(value), 10))
204202
}
205203
}
206204
}
@@ -215,10 +213,8 @@ func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
215213
if err != nil {
216214
return err
217215
}
218-
if value > 0 {
219-
for _, name := range f.Names() {
220-
_ = f.set.Set(name, value.String())
221-
}
216+
for _, name := range f.Names() {
217+
_ = f.set.Set(name, value.String())
222218
}
223219
}
224220
}
@@ -233,11 +229,9 @@ func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCo
233229
if err != nil {
234230
return err
235231
}
236-
if value > 0 {
237-
floatStr := float64ToString(value)
238-
for _, name := range f.Names() {
239-
_ = f.set.Set(name, floatStr)
240-
}
232+
floatStr := float64ToString(value)
233+
for _, name := range f.Names() {
234+
_ = f.set.Set(name, floatStr)
241235
}
242236
}
243237
}

altsrc/flag_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ func TestIntApplyInputSourceMethodSet(t *testing.T) {
234234
expect(t, 15, c.Int("test"))
235235
}
236236

237+
func TestIntApplyInputSourceMethodSetNegativeValue(t *testing.T) {
238+
c := runTest(t, testApplyInputSource{
239+
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
240+
FlagName: "test",
241+
MapValue: -1,
242+
})
243+
expect(t, -1, c.Int("test"))
244+
}
245+
237246
func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
238247
c := runTest(t, testApplyInputSource{
239248
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
@@ -264,6 +273,15 @@ func TestDurationApplyInputSourceMethodSet(t *testing.T) {
264273
expect(t, 30*time.Second, c.Duration("test"))
265274
}
266275

276+
func TestDurationApplyInputSourceMethodSetNegativeValue(t *testing.T) {
277+
c := runTest(t, testApplyInputSource{
278+
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
279+
FlagName: "test",
280+
MapValue: -30 * time.Second,
281+
})
282+
expect(t, -30*time.Second, c.Duration("test"))
283+
}
284+
267285
func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
268286
c := runTest(t, testApplyInputSource{
269287
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
@@ -294,6 +312,24 @@ func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
294312
expect(t, 1.3, c.Float64("test"))
295313
}
296314

315+
func TestFloat64ApplyInputSourceMethodSetNegativeValue(t *testing.T) {
316+
c := runTest(t, testApplyInputSource{
317+
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
318+
FlagName: "test",
319+
MapValue: -1.3,
320+
})
321+
expect(t, -1.3, c.Float64("test"))
322+
}
323+
324+
func TestFloat64ApplyInputSourceMethodSetNegativeValueNotSet(t *testing.T) {
325+
c := runTest(t, testApplyInputSource{
326+
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test1"}),
327+
FlagName: "test1",
328+
// dont set map value
329+
})
330+
expect(t, 0.0, c.Float64("test1"))
331+
}
332+
297333
func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
298334
c := runTest(t, testApplyInputSource{
299335
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),

0 commit comments

Comments
 (0)