{"record":{"id":"39e9cd2941ed5183","repo":"kubernetes/kops","slug":"cannot-use-both-all-flag-and-positional-argument","errorCode":null,"errorMessage":"cannot use both --all flag and positional arguments","messagePattern":"cannot use both --all flag and positional arguments","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/export_kubeconfig.go","lineNumber":78,"sourceCode":"\tkubeconfig.CreateKubecfgOptions\n}\n\nfunc NewCmdExportKubeconfig(f *util.Factory, out io.Writer) *cobra.Command {\n\toptions := &ExportKubeconfigOptions{}\n\n\tcmd := &cobra.Command{\n\t\tUse:     \"kubeconfig [CLUSTER | --all]\",\n\t\tAliases: []string{\"kubecfg\"},\n\t\tShort:   exportKubeconfigShort,\n\t\tLong:    exportKubeconfigLong,\n\t\tExample: exportKubeconfigExample,\n\t\tArgs: func(cmd *cobra.Command, args []string) error {\n\t\t\tif options.Admin != 0 && options.User != \"\" {\n\t\t\t\treturn fmt.Errorf(\"cannot use both --admin and --user\")\n\t\t\t}\n\t\t\tif options.all {\n\t\t\t\tif len(args) != 0 {\n\t\t\t\t\treturn fmt.Errorf(\"cannot use both --all flag and positional arguments\")\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t} else {\n\t\t\t\treturn rootCommand.clusterNameArgs(&options.ClusterName)(cmd, args)\n\t\t\t}\n\t\t},\n\t\tValidArgsFunction: commandutils.CompleteClusterName(f, true, false),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn RunExportKubeconfig(cmd.Context(), f, out, options, args)\n\t\t},\n\t}\n\n\tcmd.Flags().StringVar(&options.KubeConfigPath, \"kubeconfig\", options.KubeConfigPath, \"Filename of the kubeconfig to create\")\n\tcmd.Flags().BoolVar(&options.all, \"all\", options.all, \"Export all clusters from the kOps state store\")\n\tcmd.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\")\n\tcmd.Flags().Lookup(\"admin\").NoOptDefVal = kubeconfig.DefaultKubecfgAdminLifetime.String()\n\tcmd.Flags().StringVar(&options.User, \"user\", options.User, \"Existing user in kubeconfig file to use\")\n\tcmd.RegisterFlagCompletionFunc(\"user\", completeKubecfgUser)","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/export_kubeconfig.go#L60-L96","documentation":"`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.","triggerScenarios":"Running `kops export kubeconfig --all mycluster` — the positional cluster name conflicts with --all's all-clusters semantics.","commonSituations":"Shell completion or habit adding the cluster name; scripts parameterized as `kops export kubeconfig --all $CLUSTER`.","solutions":["Remove the positional argument: kops export kubeconfig --all","Remove --all and target one cluster: kops export kubeconfig mycluster"],"exampleFix":"// before\nkops export kubeconfig --all mycluster\n// after\nkops export kubeconfig --all","handlingStrategy":"validation","validationCode":"if flags.Changed(\"all\") && len(args) > 0 {\n    return errors.New(\"cannot use both --all flag and positional arguments\")\n}","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"kops\", args...).CombinedOutput()\nif err != nil && strings.Contains(string(out), \"--all flag and positional arguments\") {\n    // drop the positional cluster name or the --all flag and retry\n}","preventionTips":["Remember --all means every cluster: no positional name allowed","Parameterize scripts to build either `--all` or `<cluster>` invocation, not both","Use `kops get clusters` to decide whether a targeted or --all call is needed"],"tags":["cli","flags","kubeconfig"],"backgroundTag":"mutually-exclusive-flags","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}