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

YAML with a sequence containing varying types causes parse failure, but does not return error #710

Closed
tonglil opened this issue Feb 8, 2018 · 3 comments
Assignees
Labels
help wanted please help if you can! kind/bug describes or fixes a bug

Comments

@tonglil
Copy link

tonglil commented Feb 8, 2018

For some odd reason, this code fails depending on the content of the yaml file.

Code:

package main

import (
	"fmt"
	"os"

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

var build = "0" // build number set at compile-time

func main() {
	app := cli.NewApp()
	app.Name = "fails"
	app.Version = fmt.Sprintf("0.1+%s", build)
	app.Action = run
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name: "file",
		},
		altsrc.NewStringSliceFlag(cli.StringSliceFlag{
			Name: "surrogate-keys",
		}),
	}

	app.Before = altsrc.InitInputSourceWithContext(app.Flags, altsrc.NewYamlSourceFromFlagFunc("file"))

	app.Run(os.Args)
}

func run(c *cli.Context) {
	fmt.Println("test")
	fmt.Println(c.StringSlice("surrogate-keys"))
}

yaml file that breaks the app:

---
surrogate-keys:
  - x
  - y
  - z

yaml file that works:

---
surrogate-keys:
  - xx
  - yy
  - zz
@jszwedko
Copy link
Contributor

Hi @tonglil,

This one surprised me. Apparently an unquoted y is a boolean value in YAML 1.1 (see go-yaml/yaml#214 for more discussion), so, in this case, the YAML is parsed as map[surrogate-keys:[x true z]] which doesn't fit in a StringSlice.

This should fail with a better error during parsing though, so I'll update the title and mark as a bug.

@jszwedko jszwedko changed the title Sourcing from yaml introduces strange behaviorx YAML with a sequence containing varying types causes parse failure, but does not return error Feb 12, 2018
@jszwedko jszwedko added kind/bug describes or fixes a bug help wanted please help if you can! labels Feb 12, 2018
@tonglil
Copy link
Author

tonglil commented Feb 22, 2018

Thanks.
Yeah that was really strange to me while I was trying to debug why my app wasn't taking flags.

@meatballhat meatballhat self-assigned this Feb 25, 2018
@asahasrabuddhe asahasrabuddhe self-assigned this Aug 5, 2019
@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
help wanted please help if you can! kind/bug describes or fixes a bug
Projects
None yet
Development

No branches or pull requests

5 participants