derailed/k9s · warning

invalid command use `context xxx`

Error message

invalid command use `context xxx`

What it means

The `context` command was issued but cmd.Interpreter.ContextArg() could not extract a context argument. The command grammar is `context <name>`: with a name it switches kube context immediately; with a valid-but-empty arg the code falls through to a context picker. This error means the parser got neither a name nor a valid empty-arg form.

Source

Thrown at internal/view/command.go:105

}

var allowedCmds = sets.New[*client.GVR](
	client.PodGVR,
	client.SvcGVR,
	client.DpGVR,
	client.DsGVR,
	client.StsGVR,
	client.RsGVR,
)

func allowedXRay(gvr *client.GVR) bool {
	return allowedCmds.Has(gvr)
}

func (c *Command) contextCmd(p *cmd.Interpreter, pushCmd bool) error {
	ct, ok := p.ContextArg()
	if !ok {
		return fmt.Errorf("invalid command use `context xxx`")
	}

	if ct != "" {
		return useContext(c.app, ct)
	}

	gvr, v, comd, err := c.viewMetaFor(p)
	if err != nil {
		return err
	}
	if comd != nil {
		p = comd
	}

	return c.exec(p, gvr, c.componentFor(gvr, ct, v), true, pushCmd)
}

func (*Command) namespaceCmd(p *cmd.Interpreter) bool {

View on GitHub (pinned to 2d3ccc6ba2)

Solutions

  1. Type the full form: context <context-name> (names from kubectl config get-contexts).
  2. To open the picker, use the documented no-arg path (e.g. :ctx alias) that satisfies the parser's empty-arg form.
  3. If scripting, build the command via cmd.NewInterpreter with the accepted grammar instead of raw strings.
  4. Check for stray characters/whitespace after context in the command line.

Example fix

# before
:context

# after
:context minikube
Defensive patterns

Strategy: validation

Validate before calling

if _, ok := p.ContextArg(); !ok {
    return errors.New("usage: context <name> (see kubectl config get-contexts)")
}

Prevention

When it happens

Trigger: Typing bare `context` when the interpreter's grammar expects an argument form, or malformed input like `context ` with odd spacing/quotes that the arg parser rejects, or programmatic invocation of contextCmd with a non-conforming interpreter line.

Common situations: Users accustomed to other tools where bare `context` opens the picker; typos/invisible whitespace; custom keybindings or scripts replaying a stale command line after a grammar change between versions.

Related errors


AI-assisted analysis of derailed/k9s@2d3ccc6ba2 (2026-08-15). Data as JSON: /api/errors/8c086b7472e50ebd. Report an issue: GitHub.