Skip to content

required parameters don't work with altsrc #849

@costela

Description

@costela

Hi,

Unless I'm somehow overlooking something obvious, it seems the new Required support introduced in #819 doesn't work with altsrc.
The following code (based on the example for altsrc) complains that someflag wasn't supported if Required: true, but works if you comment that line out.

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/urfave/cli"
	"github.com/urfave/cli/altsrc"
)

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

	flags := []cli.Flag{
		altsrc.NewIntFlag(cli.IntFlag{
			Name:     "someflag",
			Required: true,
		}),
		cli.StringFlag{
			Name:  "file",
			Value: "test_cli_yaml.yaml",
		},
	}

	app.Action = func(c *cli.Context) error {
		fmt.Printf("test: %s\n", c.String("someflag"))
		return nil
	}

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

	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
}

And the accompanying YAML file (test_cli_yaml.yaml):

---

someflag: 1

@lynncyrin, I suspect your question in #825 might be related to this, right?

Metadata

Metadata

Assignees

Labels

kind/bugdescribes or fixes a bugstatus/confirmedconfirmed to be valid, but work has yet to start

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions