larksuite/cli · error

bind flag aliases: command is nil

Error message

bind flag aliases: command is nil

What it means

Programmer-error guard in Bind: invoked with a nil cobra command. Flag alias binding requires a live command to look up flag sets and command paths; there is no recovery path other than passing a valid command.

Solutions

  1. Construct the cobra command before calling Bind
  2. Audit MustBind call sites for nil-unsafe initialization order
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/flagalias/flagalias.go:50 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/8f35d27fc582a35d. Report an issue: GitHub.

Appendix: source

Thrown at internal/flagalias/flagalias.go:50

	Aliases   []string
}

// Bind installs exact-name aliases on cmd and records them on their canonical
// pflags for manifest/tooling introspection. Existing pflag normalization is
// composed first; alias resolution is then applied to the normalized spelling.
// The canonical pflag also remembers the spelling that supplied its most
// recently applied value so validation errors can point back to the caller's
// actual input without changing native repeated-flag semantics.
//
// Bind is intentionally the only production owner of SetNormalizeFunc. It
// validates the complete accepted-name set before installing alias metadata or
// a normalizer, so a configuration error cannot leave aliases partially bound.
func Bind(cmd *cobra.Command, specs []Spec) error {
	if len(specs) == 0 {
		return nil
	}
	if cmd == nil {
		return fmt.Errorf("bind flag aliases: command is nil")
	}

	cmd.InitDefaultHelpFlag()
	flagSet := cmd.Flags()
	previous := flagSet.GetNormalizeFunc()
	normalize := func(name string) string {
		if previous == nil {
			return name
		}
		return string(previous(flagSet, name))
	}

	registered := make(map[string]string)
	collectRegistered(registered, flagSet)
	collectRegistered(registered, cmd.InheritedFlags())

	// Existing annotations matter when Bind is composed by multiple adapters:
	// aliases are not independent pflags, so VisitAll alone cannot see them.

View on GitHub (pinned to 7fd6ef3c07)