kubernetes/kops · error

cannot specify cluster via --name and positional argument

Error message

cannot specify cluster via --name and positional argument

What it means

RootCmd.ProcessArgs parses positional args under the rule: zero args OK, or exactly one arg used as the cluster name when --name was not set. If exactly one arg was given but --name is already set, the cluster is specified twice — ProcessArgs returns "cannot specify cluster via --name and positional argument" after printing a diagnostic listing both values and hinting at the boolean-flag pitfall.

Source

Thrown at cmd/kops/root.go:303

		if c.clusterName == "" {
			c.clusterName = args[0]
			return nil
		}
	}

	fmt.Printf("\nFound multiple arguments which look like a cluster name\n")
	if c.clusterName != "" {
		fmt.Printf("\t%q (via flag)\n", c.clusterName)
	}
	for _, arg := range args {
		fmt.Printf("\t%q (as argument)\n", arg)
	}
	fmt.Printf("\n")
	fmt.Printf("This often happens if you specify an argument to a boolean flag without using =\n")
	fmt.Printf("For example: use `--bastion=true` or `--bastion`, not `--bastion true`\n\n")

	if len(args) == 1 {
		return fmt.Errorf("cannot specify cluster via --name and positional argument")
	}
	return fmt.Errorf("expected a single <clustername> to be passed as an argument")
}

func (c *RootCmd) ClusterName(verbose bool) string {
	if c.clusterName != "" {
		return c.clusterName
	}

	// Read from kubeconfig
	pathOptions := clientcmd.NewDefaultPathOptions()

	config, err := pathOptions.GetStartingConfig()
	if err != nil {
		klog.Warningf("error reading kubecfg: %v", err)
	} else if config.CurrentContext == "" {
		klog.Warningf("no context set in kubecfg")
	} else {

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Remove either --name or the positional cluster argument — specify the cluster only once
  2. If a boolean flag was involved, use `--bastion=true` / `--bastion` syntax instead of `--bastion true`
  3. Quote/skip an unintended extra argument in the script (check for accidental variables expanding to a string)
  4. If you intended to change the cluster, drop the positional arg and only use --name

Example fix

// before
kops get cluster --name a.k8s.local b.k8s.local
// error: cannot specify cluster via --name and positional argument
// after
kops get cluster --name a.k8s.local
Defensive patterns

Strategy: validation

Validate before calling

// precheck: never combine --name with a positional cluster
if clusterFlag != "" && len(positionalArgs) >= 1 {
    return errors.New("specify the cluster once: use --name OR positional argument, not both")
}

Prevention

When it happens

Trigger: A command receives len(args)==1 while c.clusterName is non-empty (i.e. --name was also provided). E.g. `kops get cluster --name a.k8s.local b.k8s.local`. Also triggered indirectly when a boolean flag was given a bare value (e.g. `--bastion true`), making `true` the positional arg.

Common situations: `--flag value` instead of `--flag=value` for booleans, so "true" becomes the cluster name; combining --name with a positional cluster name in scripts; passing a leftover token from shell word-splitting.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/eaab3977ce08d264. Report an issue: GitHub.