kubernetes/kops · error

cannot use both --all flag and positional arguments

Error message

cannot use both --all flag and positional arguments

What it means

`kops export kubeconfig --all` exports kubeconfigs for all clusters known to the state store, so it cannot be combined with a positional cluster-name argument. The Args validator returns this error when --all is set and len(args) != 0.

Source

Thrown at cmd/kops/export_kubeconfig.go:78

	kubeconfig.CreateKubecfgOptions
}

func NewCmdExportKubeconfig(f *util.Factory, out io.Writer) *cobra.Command {
	options := &ExportKubeconfigOptions{}

	cmd := &cobra.Command{
		Use:     "kubeconfig [CLUSTER | --all]",
		Aliases: []string{"kubecfg"},
		Short:   exportKubeconfigShort,
		Long:    exportKubeconfigLong,
		Example: exportKubeconfigExample,
		Args: func(cmd *cobra.Command, args []string) error {
			if options.Admin != 0 && options.User != "" {
				return fmt.Errorf("cannot use both --admin and --user")
			}
			if options.all {
				if len(args) != 0 {
					return fmt.Errorf("cannot use both --all flag and positional arguments")
				}
				return nil
			} else {
				return rootCommand.clusterNameArgs(&options.ClusterName)(cmd, args)
			}
		},
		ValidArgsFunction: commandutils.CompleteClusterName(f, true, false),
		RunE: func(cmd *cobra.Command, args []string) error {
			return RunExportKubeconfig(cmd.Context(), f, out, options, args)
		},
	}

	cmd.Flags().StringVar(&options.KubeConfigPath, "kubeconfig", options.KubeConfigPath, "Filename of the kubeconfig to create")
	cmd.Flags().BoolVar(&options.all, "all", options.all, "Export all clusters from the kOps state store")
	cmd.Flags().DurationVar(&options.Admin, "admin", options.Admin, "Also export a cluster admin user credential with the specified lifetime and add it to the cluster context")
	cmd.Flags().Lookup("admin").NoOptDefVal = kubeconfig.DefaultKubecfgAdminLifetime.String()
	cmd.Flags().StringVar(&options.User, "user", options.User, "Existing user in kubeconfig file to use")
	cmd.RegisterFlagCompletionFunc("user", completeKubecfgUser)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Remove the positional argument: kops export kubeconfig --all
  2. Remove --all and target one cluster: kops export kubeconfig mycluster

Example fix

// before
kops export kubeconfig --all mycluster
// after
kops export kubeconfig --all
Defensive patterns

Strategy: validation

Validate before calling

if flags.Changed("all") && len(args) > 0 {
    return errors.New("cannot use both --all flag and positional arguments")
}

Try / catch

out, err := exec.Command("kops", args...).CombinedOutput()
if err != nil && strings.Contains(string(out), "--all flag and positional arguments") {
    // drop the positional cluster name or the --all flag and retry
}

Prevention

When it happens

Trigger: Running `kops export kubeconfig --all mycluster` — the positional cluster name conflicts with --all's all-clusters semantics.

Common situations: Shell completion or habit adding the cluster name; scripts parameterized as `kops export kubeconfig --all $CLUSTER`.

Understand the failure class

Background: "mutually exclusive" flag errors: what "can't supply both nx and xx", "--raw is not compatible with -i" and "cannot be used with" mean, and how to fix them — this error's family across 29 libraries.

Related errors


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