blob: 55a3ed34a8e701e94fb9fcc459667b5c62139e76 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package flag
18
19import (
20 goflag "flag"
21 "strings"
22
23 "github.com/golang/glog"
24 "github.com/spf13/pflag"
25)
26
27// WordSepNormalizeFunc changes all flags that contain "_" separators
28func WordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
29 if strings.Contains(name, "_") {
30 return pflag.NormalizedName(strings.Replace(name, "_", "-", -1))
31 }
32 return pflag.NormalizedName(name)
33}
34
35// WarnWordSepNormalizeFunc changes and warns for flags that contain "_" separators
36func WarnWordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
37 if strings.Contains(name, "_") {
38 nname := strings.Replace(name, "_", "-", -1)
39 glog.Warningf("%s is DEPRECATED and will be removed in a future version. Use %s instead.", name, nname)
40
41 return pflag.NormalizedName(nname)
42 }
43 return pflag.NormalizedName(name)
44}
45
46// InitFlags normalizes, parses, then logs the command line flags
47func InitFlags() {
48 pflag.CommandLine.SetNormalizeFunc(WordSepNormalizeFunc)
49 pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
50 pflag.Parse()
51 pflag.VisitAll(func(flag *pflag.Flag) {
52 glog.V(2).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
53 })
54}