Documentation
¶
Overview ¶
Package flag provides helpers for defining and parsing command-line flags in go-service.
This package is a small wrapper around the standard library flag package that standardizes a few flag conventions used across go-service, especially configuration source selection.
Configuration flag (-config / -c) ¶
Many go-service applications accept "-config" and "-c" flags that select where configuration should be loaded from. The conventional value format is:
"kind:location"
Common examples:
- "file:/path/to/config.yaml" to read configuration from a file (decoder selected by extension)
- "env:MY_CONFIG" to read configuration from an environment variable (typically "<kind>:<base64-content>")
The config package treats unsupported explicit "kind:location" values as invalid.
The FlagSet type in this package supports installing this convention via FlagSet.AddConfig and retrieving it via FlagSet.GetConfig. The config subsystem ([config.NewDecoder]) consumes this value to route to the appropriate decoder.
Start with NewFlagSet, FlagSet.AddConfig, and FlagSet.GetConfig.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FlagSet ¶
FlagSet represents a set of defined flags.
It wraps flag.FlagSet and provides optional support for the conventional go-service configuration flag ("-config" / "-c") used by the config subsystem to route decoding.
This type is intentionally minimal: you can still use the embedded *flag.FlagSet to define and parse arbitrary flags.
func NewFlagSet ¶
NewFlagSet creates a new FlagSet with the given name.
The returned FlagSet wraps the standard library flag.FlagSet and uses flag.ContinueOnError so callers can handle parse errors explicitly (instead of terminating the process).
func (*FlagSet) AddConfig ¶ added in v2.500.0
AddConfig adds the conventional configuration flag ("-config" / "-c") to the flag set.
The value is treated as an opaque "kind:location" string that the config package interprets. Common examples include:
- "file:/path/to/config.yaml" (decode kind inferred from file extension)
- "env:MY_CONFIG" (read config payload from environment variable MY_CONFIG)
The config package treats unsupported explicit "kind:location" values as invalid.
The provided value is used as the default. AddConfig registers both flag names against the same backing value, so if both aliases are supplied during parsing, the later parsed flag wins.
AddConfig must be called before [FlagSet.Parse]. Like the embedded standard library flag.FlagSet, it panics if either "config" or "c" has already been registered.
func (*FlagSet) GetConfig ¶ added in v2.500.0
GetConfig returns the configured config flag ("-config" / "-c") value.
After FlagSet.AddConfig, GetConfig returns the default value until [FlagSet.Parse] updates it. GetConfig is safe to call before AddConfig; in that case it returns an empty string.