flag

package
v2.693.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 2 Imported by: 0

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

type FlagSet struct {
	*flag.FlagSet
	// contains filtered or unexported fields
}

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

func NewFlagSet(name string) *FlagSet

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

func (f *FlagSet) AddConfig(value string)

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

func (f *FlagSet) GetConfig() string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL