Skip to content

source/flag: command-line flags from a descriptor #1

Description

@tdakkota

Goal

A source/flag adapter, so a descriptor also produces command-line flags:

cfg, report, err := ConfigDescriptor.Resolve(
    yaml.File("config.yaml"),
    env.Current(env.Prefix("APP_")),
    flag.CommandLine(),          // highest precedence
)

Why it is not just another source

The three existing sources answer "what did this input say about field X". Flags
also have to answer the reverse — "what flags exist, and what does --help look
like" — which means registering a flag per field before parsing. The
figureout.Source interface (Load(ctx, *Model) (*Layer, error)) covers the
first half only, so this needs either a pre-parse step or a wider interface.

Scope

  • flag.Name, flag.Short, flag.Skip, flag.Hidden, flag.Deprecated
    field options, with names relative to the declaring object as env and
    the tree sources already are (--server.listen-port, not
    --listen-port)
  • Derived names from the canonical path: server.listenPort
    --server-listen-port
  • Help text from Doc, defaults from Default, allowed values from an
    enum constraint, and the value placeholder from the semantic type
  • Booleans as presence-only flags (--debug, not --debug=true)
  • Lists as repeated flags, delimited values, or both
  • Unions: the discriminator is a flag, variant fields are flags alongside it
    (the same inline layout the other sources use)
  • Origin reporting flag --server-listen-port so a bad value points at the
    flag, not at a file

Open questions

  • Adapter target. The design lists flag.FlagSet, Cobra and Kong. Standard
    library first, or an interface with adapters from the start?
  • Pre-parse registration. Add a Register(*Model) error step to the flag
    source specifically, or widen figureout.Source with an optional interface
    the resolver type-asserts?
  • Required fields. A required field with no default currently errors when
    missing. With flags in the mix, does --help still work when required flags
    are absent? It has to.
  • Merging. Repeated flags across one command line — MergeAppend within a
    single layer, or does the flag source flatten before producing its layer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions