Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package flag |
| 18 | |
| 19 | import ( |
| 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 |
| 28 | func 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 |
| 36 | func 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 |
| 47 | func 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 | } |