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

Alternate input sources YAML doesn't work when flag includes alternate name #711

Closed
wcwynn opened this issue Feb 9, 2018 · 6 comments
Closed
Labels
help wanted please help if you can! kind/bug describes or fixes a bug

Comments

@wcwynn
Copy link

wcwynn commented Feb 9, 2018

Alternate names are described here: https://github.com/urfave/cli#alternate-names
Support for YAML is described here: https://github.com/urfave/cli#values-from-alternate-input-sources-yaml-toml-and-others

When any alternate source flag includes an alternate name, the alternate source input doesn't function as it should (i.e. it doesn't use the alternate input source data).

Code to reproduce:

package main

import (
    "fmt"
    "os"

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

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

    flags := []cli.Flag{
        /*  
            // OK
            altsrc.NewIntFlag(cli.IntFlag{Name: "test", Value: -1}),
            altsrc.NewStringFlag(cli.StringFlag{Name: "flag", Value: "default"}),
        */

        // NG #1
        altsrc.NewIntFlag(cli.IntFlag{Name: "test, t", Value: -1}),
        altsrc.NewStringFlag(cli.StringFlag{Name: "flag", Value: "default"}),

        /*  
            // NG #2
            altsrc.NewIntFlag(cli.IntFlag{Name: "test", Value: -1}),
            altsrc.NewStringFlag(cli.StringFlag{Name: "flag, f", Value: "default"}),

            // NG #3
            altsrc.NewIntFlag(cli.IntFlag{Name: "test, t", Value: -1}),
            altsrc.NewStringFlag(cli.StringFlag{Name: "flag, f", Value: "default"}),
        */

        cli.StringFlag{Name: "load, l"},
    }   

    app.Action = func(c *cli.Context) error {
        fmt.Println("yaml ist rad")
        fmt.Printf("load: %s\n", c.String("load"))
        fmt.Printf("test: %d\n", c.Int("test"))
        fmt.Printf("flag: %s\n", c.String("flag"))
        return nil 
    }   

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

    app.Run(os.Args)
}

Test file contents:

iMac:buggy chris$ cat buggy.txt
test: 123
flag: excellent!
iMac:buggy chris$

Output:

iMac:buggy chris$ go run buggy.go --load buggy.txt
yaml ist rad
load: buggy.txt
test: -1
flag: excellent!
iMac:buggy chris$

Have not had a chance to debug this as of yet, but documenting it for now in case others encounter this problem.

@jszwedko
Copy link
Contributor

Thanks for the detailed bug report @wcwynn ! It does look like there are a few places where altsrc assumes that the defined flags only have one "name" specified.

@jszwedko jszwedko added kind/bug describes or fixes a bug help wanted please help if you can! labels Feb 12, 2018
@meatballhat meatballhat self-assigned this Feb 25, 2018
@rmoriz
Copy link

rmoriz commented Apr 19, 2018

FYI: this also affects TOML.

@pysqz
Copy link

pysqz commented Jan 8, 2019

@wcwynn PR #791 can fix this bug. @jszwedko @meatballhat please review this commits

@mdbraber
Copy link

Also mentioned this in #791, this doesn't seem to fix problems with GlobalStringSliceFlag

@pysqz
Copy link

pysqz commented Jan 15, 2019

please review #792. it fix problems with *SliceFlag. @mdbraber

@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

8 participants