Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOML/YAML Parsing int64, uint64, uint, time.Duration #599

Closed
timjchin opened this issue Feb 15, 2017 · 1 comment
Closed

TOML/YAML Parsing int64, uint64, uint, time.Duration #599

timjchin opened this issue Feb 15, 2017 · 1 comment
Labels
kind/bug describes or fixes a bug

Comments

@timjchin
Copy link

Love the library, thank you all for working on it.

Ran into an issue when using toml/yaml imports, int64, uint64, uint and time.Duration are not parsed.

For example:

test.toml

test-int=5
test-int-64=9223372036854775807
test-uint-64=100
test-uint=50
# test-duration="4m49s"

test.yaml

test-int: 5
test-int-64: 9223372036854775807
test-uint-64: 100
test-uint: 50
# test-duration: "4m49s"

main.go:

package main

import (
	"fmt"
	"os"
	"time"

	"gopkg.in/urfave/cli.v1"
	"gopkg.in/urfave/cli.v1/altsrc"
)

func main() {
	app := cli.NewApp()

	var (
		testInt64    int64
		testUint64   uint64
		testUint     uint
		testInt      int
		testDuration time.Duration
	)

	app.Flags = []cli.Flag{
		altsrc.NewInt64Flag(cli.Int64Flag{
			Name:        "test-int-64",
			Destination: &testInt64,
		}),
		altsrc.NewUint64Flag(cli.Uint64Flag{
			Name:        "test-uint-64",
			Destination: &testUint64,
		}),
		altsrc.NewUintFlag(cli.UintFlag{
			Name:        "test-uint",
			Destination: &testUint,
		}),
		altsrc.NewIntFlag(cli.IntFlag{
			Name:        "test-int",
			Destination: &testInt,
		}),
		altsrc.NewDurationFlag(cli.DurationFlag{
			Name:        "test-duration",
			Destination: &testDuration,
		}),
		cli.StringFlag{
			Name: "config, c",
		},
	}

	app.Action = func(c *cli.Context) error {
		fmt.Println(testInt)
		fmt.Println(testInt64)
		fmt.Println(testUint64)
		fmt.Println(testUint)
		fmt.Println(testDuration)
		return nil
	}

	// TOML:
	app.Before = altsrc.InitInputSourceWithContext(app.Flags, altsrc.NewTomlSourceFromFlagFunc("config"))

	// YAML:
	// app.Before = altsrc.InitInputSourceWithContext(app.Flags, altsrc.NewYamlSourceFromFlagFunc("config"))
	app.Run(os.Args)
}

Running this with YAML or TOML results in the same outputs.

Results from running with test-duration comented out:

go run main.go -c test.toml
go run main.go -c test.yaml

5
0
0
0
0s

Running with test-duration:

Mismatched type for flag 'test-duration'. Expected 'duration' but actual is 'string'
exit status 1
@coilysiren
Copy link
Member

Hiya! There's a known issue with the current implementation of the CLI, where altsrc is generally clunky and poorly documented. My current idea is that I'm going to move all of the altsrc functionality into the main package in some way, and that's how all of the "altsrc is weird and different???" issues people are having will get resolved. I'm tracking that work here => #833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug describes or fixes a bug
Projects
None yet
Development

No branches or pull requests

3 participants