Merge pull request #6144 from andylibrian/5036-incorrect-subcommand-options

implemented options command, fixes #5036
pull/6175/head
Medya Ghazizadeh 2019-12-24 01:57:07 -08:00 committed by GitHub
commit 6b0e2ca121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 1 deletions

View File

@ -0,0 +1,68 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"bytes"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/minikube/pkg/minikube/out"
)
// optionsCmd represents the options command
var optionsCmd = &cobra.Command{
Use: "options",
Short: "Show a list of global command-line options (applies to all commands).",
Long: "Show a list of global command-line options (applies to all commands).",
Run: runOptions,
}
// runOptions handles the executes the flow of "minikube options"
func runOptions(cmd *cobra.Command, args []string) {
out.String("The following options can be passed to any command:\n\n")
for _, flagName := range viperWhiteList {
f := pflag.Lookup(flagName)
out.String(flagUsage(f))
}
}
func flagUsage(flag *pflag.Flag) string {
x := new(bytes.Buffer)
if flag.Hidden {
return ""
}
format := "--%s=%s: %s\n"
if flag.Value.Type() == "string" {
format = "--%s='%s': %s\n"
}
if len(flag.Shorthand) > 0 {
format = " -%s, " + format
} else {
format = " %s " + format
}
fmt.Fprintf(x, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
return x.String()
}

View File

@ -54,9 +54,9 @@ var dirs = [...]string{
}
var viperWhiteList = []string{
"v",
"alsologtostderr",
"log_dir",
"v",
}
// RootCmd represents the base command when called without any subcommands
@ -214,6 +214,7 @@ func init() {
logsCmd,
updateCheckCmd,
versionCmd,
optionsCmd,
},
},
}