flag

package
v2.511.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 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 "<ext>:<base64-content>")

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 provided value is used as the default.

func (*FlagSet) GetConfig added in v2.500.0

func (f *FlagSet) GetConfig() string

GetConfig returns the configured config flag ("-config" / "-c") value.

This method is nil-safe with respect to AddConfig: if AddConfig was not called, GetConfig returns an empty string. This allows callers to depend on GetConfig unconditionally.

Jump to

Keyboard shortcuts

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