micro/go-micro · error

flags not parsed

Error message

flags not parsed

What it means

The flag config source reads values from the standard library flag package, but it can only do so after flag.Parse() has been called. Read() throws this error when the flags have not yet been parsed, because there are no values to expose.

Source

Thrown at config/source/flag/flag.go:19

package flag

import (
	"errors"
	"flag"
	"strings"
	"time"

	"dario.cat/mergo"
	"go-micro.dev/v6/config/source"
)

type flagsrc struct {
	opts source.Options
}

func (fs *flagsrc) Read() (*source.ChangeSet, error) {
	if !flag.Parsed() {
		return nil, errors.New("flags not parsed")
	}

	var changes map[string]interface{}

	visitFn := func(f *flag.Flag) {
		n := strings.ToLower(f.Name)
		keys := strings.FieldsFunc(n, split)
		reverse(keys)

		tmp := make(map[string]interface{})
		for i, k := range keys {
			if i == 0 {
				tmp[k] = f.Value
				continue
			}

			tmp = map[string]interface{}{k: tmp}
		}

View on GitHub (pinned to 24529f1404)

Solutions

  1. Call flag.Parse() in main before initializing the config stack that includes the flag source.
  2. Reorder startup: parse flags first, then load config so Read() sees parsed values.
  3. If flags are managed by another package, invoke that package's Parse entrypoint before reading config.
  4. In tests, call flag.Parse() with a prepared flag.CommandLine set or use flag.CommandLine.Parse(args).

Example fix

// before
func main() {
    cfg, _ := config.Load() // flag source errors: flags not parsed
    flag.Parse()
}
// after
func main() {
    flag.Parse()
    cfg, _ := config.Load()
}
Defensive patterns

Strategy: validation

Validate before calling

if !flag.Parsed() {
    flag.Parse()
}

Prevention

When it happens

Trigger: Building a config source from flags and calling Read() (directly or via config.Load) before flag.Parse() ran in main; skipping flag.Parse entirely in a service that loads config early.

Common situations: Config loading hoisted before argument parsing in an init/main ordering refactor; tests constructing the flag source without simulating parsed flags; using a library that defers parsing while your code reads config first.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/fd3ba285878e4b6c. Report an issue: GitHub.