peterbourgon / ff

Flags-first package for configuration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Selectively inheriting from parent flags

mfridman opened this issue · comments

I suppose this is more a question/clarification on the intended behavior of .GetFlag and .AddFlag in v4.0.0-alpha.4.

I passed root flags down to a subcommand, so it has access to *ff.FlagSet and then constructed a new flagset, and afaics the only option is to call

ff.NewFlagSet("status").SetParent(root.flags)

But, in some commands I want to selectively add parent flags without duplicating all the bits. So I was expecting something like this to work in the subcommand:

fs := ff.NewFlagSet("status")
THIS, _ := root.flags.GetFlag("dir")

# Add the inherited dir flag to the status flagset
fs.AddFlag(THIS)

But, .AddFlag takes an ff.FlagConfig and .GetFlag returns an ff.Flag. Curious if this is working as intended and .SetParent is all-or-nothing, or there could/should be support for selectively inheriting flags?

I got around this by defining a "global" flag config, e.g.,

func newDirFlag(s *string) ff.FlagConfig {
	return ff.FlagConfig{
		ShortName: 'd',
		LongName:  "dir",
		Usage:     "dir to read migrations from",
		NoDefault: true,
		Value:     ffval.NewValue(s),
	}
}

And then just re-using that wherever I needed that specific flag to be available.

fs := ff.NewFlagSet("status")
var dir string
fs.AddFlag(newDirFlag(&dir))

Going to close this, as it appears to be working as intended.