ahmetb/kubectx · warning

unsupported option %q

Error message

unsupported option %q

What it means

getSwitchOp checks the target namespace string before building a SwitchOp. If the target starts with "-" (and is not the bare "-"), it is treated as an unknown option rather than a namespace, and UnsupportedOp with "unsupported option %q" is returned. This prevents flags from being silently interpreted as namespace names.

Source

Thrown at cmd/kubens/flags.go:83

		if !force {
			if !slices.Contains([]string{"-f", "--force"}, argv[0]) {
				return UnsupportedOp{Err: fmt.Errorf("unsupported arguments %q", argv)}
			}

			// -f|--force {namespace}
			force = true
			name = argv[1]
		}

		return getSwitchOp(name, force)
	}

	return UnsupportedOp{Err: fmt.Errorf("too many arguments")}
}

func getSwitchOp(v string, force bool) Op {
	if strings.HasPrefix(v, "-") && v != "-" {
		return UnsupportedOp{Err: fmt.Errorf("unsupported option %q", v)}
	}
	return SwitchOp{Target: v, Force: force}
}

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Provide the namespace as a bare positional argument: `kubens <namespace>`
  2. Remove kubectl-only flags like -n/--namespace; kubens switches namespace directly
  3. Use only the supported flags -f/--force, always together with a namespace
  4. Run `kubens --help` for the accepted syntax

Example fix

// before
kubens -n kube-system   # -n unsupported
// after
kubens kube-system
Defensive patterns

Strategy: validation

Validate before calling

target := args[len(args)-1]
if strings.HasPrefix(target, "-") {
    return fmt.Errorf("namespace cannot start with '-': %q", target)
}

Try / catch

op := kubens.ParseArgs(argv)
if so, ok := op.(kubens.SwitchOp); ok {
    if strings.HasPrefix(so.Target, "-") {
        // shouldn't happen (getSwitchOp rejects), but guard anyway
    }
} else if uo, ok := op.(kubens.UnsupportedOp); ok {
    fmt.Fprintf(stderr, "unsupported option: %v\n", uo.Err)
    os.Exit(2)
}

Prevention

When it happens

Trigger: Calling kubens with an unrecognized flag in the namespace position, e.g. `kubens --force` (missing the actual namespace) or `kubens -n foo`.

Common situations: Users carrying over kubectl-style flags (-n, --namespace) that kubens does not support; forgetting the namespace after -f; copy-pasted commands from other tools.

Related errors


AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02). Data as JSON: /api/errors/9cb7d0d06b0effe1. Report an issue: GitHub.