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

altsrc requires flag names #2

Open
eyudkin opened this issue Jan 23, 2020 · 5 comments
Open

altsrc requires flag names #2

eyudkin opened this issue Jan 23, 2020 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@eyudkin
Copy link

eyudkin commented Jan 23, 2020

What problem does this solve?

For now altsrc requires to name my flags using dots to find them into multi-level config files.
I.e. to find a flag "level" in the following config:

{
  "infrastructure": {
    "logger": {
      "level": 10
    }
  }
}

..i need to name my flag "infrastructure.logger.level" without exceptions.
But what if I don't need it? For my cli interface I want its name to be "--log-level", because I don't need any nested things there.
So, I want to be able name flags as I want and configure paths as I want too, separately.

Solution description

Something like:

altsrc.NewIntFlag(&cli.IntFlag{
	Name: "log-level",
	Usage: "Logger level (0 - debug, 1 - info, ...)",
	Value: 1,
}, "my.path.to.flag.in.config"), // added customizable parameter here

Describe alternatives you've considered

Using aliases is not an option there because this ugly long "infrastructure.logger.level" name will be displayed at help screen anyway.

@coilysiren
Copy link
Member

Heya! We are planning on completely rewriting altsrc for v3, so its unlikely that a maintainer will get to this issue! I'm marking it as help wanted for any other contributor that wants to help, though 🙏

@coilysiren
Copy link
Member

Ah, I just re-read the issue. It looks like this is a feature request? Since altsrc is about to be rewritten, we aren't currently accepting feature requests for it.

@stale
Copy link

stale bot commented Jun 28, 2020

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

@stale
Copy link

stale bot commented Jul 29, 2020

Closing this as it has become stale.

@stale stale bot closed this as completed Jul 29, 2020
@meatballhat meatballhat reopened this Apr 22, 2022
@meatballhat meatballhat added the help wanted Extra attention is needed label May 5, 2022
@dearchap dearchap transferred this issue from urfave/cli Nov 28, 2022
@abitrolly
Copy link
Contributor

Looks like it is fixed new, and it is possible to choose any key name in a config file.

configFiles := []string{
	filepath.Join(testdataDir, "config.yaml"),
	filepath.Join(testdataDir, "alt-config.yaml"),
}

app := &cli.Command{
	Name: "greet",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "name",
			Aliases: []string{"n"},
			Sources: altsrc.YAML("greet.name", configFiles...),
		},
		&cli.IntFlag{
			Name:    "enthusiasm",
			Aliases: []string{"!"},
			Sources: altsrc.YAML("greet.enthusiasm", configFiles...),
		},
	},
	Action: func(cCtx *cli.Context) error {
		punct := ""
		if cCtx.Int("enthusiasm") > 9000 {
			punct = "!"
		}

		fmt.Fprintf(os.Stdout, "Hello, %[1]v%[2]v\n", cCtx.String("name"), punct)

		return nil
	},
}

// Simulating os.Args
os.Args = []string{"greet"}

if err := app.Run(context.Background(), os.Args); err != nil {
	fmt.Fprintf(os.Stdout, "OH NO: %[1]v\n", err)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants