Use pflags word separators to make - and _ equiv.

This commit is contained in:
Tim Hockin 2015-04-23 20:50:35 -07:00
parent 22db9fb176
commit 6694eff020
6 changed files with 12 additions and 13 deletions

View File

@ -70,6 +70,7 @@ func (hk *HyperKube) Flags() *pflag.FlagSet {
if hk.baseFlags == nil { if hk.baseFlags == nil {
hk.baseFlags = pflag.NewFlagSet(hk.Name, pflag.ContinueOnError) hk.baseFlags = pflag.NewFlagSet(hk.Name, pflag.ContinueOnError)
hk.baseFlags.SetOutput(ioutil.Discard) hk.baseFlags.SetOutput(ioutil.Discard)
hk.baseFlags.SetWordSeparators([]string{"-", "_"})
hk.baseFlags.BoolVarP(&hk.helpFlagVal, "help", "h", false, "help for "+hk.Name) hk.baseFlags.BoolVarP(&hk.helpFlagVal, "help", "h", false, "help for "+hk.Name)
// These will add all of the "global" flags (defined with both the // These will add all of the "global" flags (defined with both the

View File

@ -68,6 +68,7 @@ func (s *Server) Flags() *pflag.FlagSet {
if s.flags == nil { if s.flags == nil {
s.flags = pflag.NewFlagSet(s.Name(), pflag.ContinueOnError) s.flags = pflag.NewFlagSet(s.Name(), pflag.ContinueOnError)
s.flags.SetOutput(ioutil.Discard) s.flags.SetOutput(ioutil.Discard)
s.flags.SetWordSeparators([]string{"-", "_"})
} }
return s.flags return s.flags
} }

View File

@ -55,6 +55,7 @@ func isYAML(data []byte) bool {
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
flag.Commandline.SetWordSeparators([]string{"-", "_"})
flag.Parse() flag.Parse()
if *rewrite != "" { if *rewrite != "" {

View File

@ -17,6 +17,7 @@ limitations under the License.
package util package util
import ( import (
"flag"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -85,6 +86,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
mapper := kubectl.ShortcutExpander{latest.RESTMapper} mapper := kubectl.ShortcutExpander{latest.RESTMapper}
flags := pflag.NewFlagSet("", pflag.ContinueOnError) flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SetWordSeparators([]string{"-", "_"})
clientConfig := optionalClientConfig clientConfig := optionalClientConfig
if optionalClientConfig == nil { if optionalClientConfig == nil {
@ -225,7 +227,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
// BindFlags adds any flags that are common to all kubectl sub commands. // BindFlags adds any flags that are common to all kubectl sub commands.
func (f *Factory) BindFlags(flags *pflag.FlagSet) { func (f *Factory) BindFlags(flags *pflag.FlagSet) {
// any flags defined by external projects (not part of pflags) // any flags defined by external projects (not part of pflags)
util.AddAllFlagsToPFlagSet(flags) util.AddFlagSetToPFlagSet(flag.CommandLine, flags)
// This is necessary as github.com/spf13/cobra doesn't support "global" // This is necessary as github.com/spf13/cobra doesn't support "global"
// pflags currently. See https://github.com/spf13/cobra/issues/44. // pflags currently. See https://github.com/spf13/cobra/issues/44.

View File

@ -16,12 +16,11 @@ limitations under the License.
package util package util
import ( import "github.com/spf13/pflag"
flag "github.com/spf13/pflag"
)
// InitFlags normalizes and parses the command line flags // InitFlags normalizes and parses the command line flags
func InitFlags() { func InitFlags() {
pflag.CommandLine.SetWordSeparators([]string{"-", "_"})
AddAllFlagsToPFlags() AddAllFlagsToPFlags()
flag.Parse() pflag.Parse()
} }

View File

@ -79,7 +79,7 @@ func (v *flagValueWrapper) IsBoolFlag() bool {
// Imports a 'flag.Flag' into a 'pflag.FlagSet'. The "short" option is unset // Imports a 'flag.Flag' into a 'pflag.FlagSet'. The "short" option is unset
// and the type is inferred using reflection. // and the type is inferred using reflection.
func AddFlagToPFlagSet(f *flag.Flag, fs *pflag.FlagSet) { func addFlagToPFlagSet(f *flag.Flag, fs *pflag.FlagSet) {
if fs.Lookup(f.Name) == nil { if fs.Lookup(f.Name) == nil {
fs.Var(wrapFlagValue(f.Value), f.Name, f.Usage) fs.Var(wrapFlagValue(f.Value), f.Name, f.Usage)
} }
@ -88,16 +88,11 @@ func AddFlagToPFlagSet(f *flag.Flag, fs *pflag.FlagSet) {
// Adds all of the flags in a 'flag.FlagSet' package flags to a 'pflag.FlagSet'. // Adds all of the flags in a 'flag.FlagSet' package flags to a 'pflag.FlagSet'.
func AddFlagSetToPFlagSet(fsIn *flag.FlagSet, fsOut *pflag.FlagSet) { func AddFlagSetToPFlagSet(fsIn *flag.FlagSet, fsOut *pflag.FlagSet) {
fsIn.VisitAll(func(f *flag.Flag) { fsIn.VisitAll(func(f *flag.Flag) {
AddFlagToPFlagSet(f, fsOut) addFlagToPFlagSet(f, fsOut)
}) })
} }
// Adds all of the top level 'flag' package flags to a 'pflag.FlagSet'. // Add all of the top level 'flag' package flags to the top level 'pflag' flags.
func AddAllFlagsToPFlagSet(fs *pflag.FlagSet) {
AddFlagSetToPFlagSet(flag.CommandLine, fs)
}
// Add al of the top level 'flag' package flags to the top level 'pflag' flags.
func AddAllFlagsToPFlags() { func AddAllFlagsToPFlags() {
AddFlagSetToPFlagSet(flag.CommandLine, pflag.CommandLine) AddFlagSetToPFlagSet(flag.CommandLine, pflag.CommandLine)
} }