peterbourgon / ff

Flags-first package for configuration
Apache License 2.0
1.37k stars 59 forks source link

Selectively inheriting from parent flags #133

Closed mfridman closed 6 months ago

mfridman commented 6 months ago

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?

mfridman commented 6 months ago

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.