diff --git a/env_short_long_name.go b/env_short_long_name.go index 5194606..35539c0 100644 --- a/env_short_long_name.go +++ b/env_short_long_name.go @@ -6,15 +6,29 @@ import "strings" // LongOpt -> short-opt // long_opt -> long-opt +// 专有名字不转换 +// 专有名字词 +var specialNames = map[string]bool{ + "JSON": true, + "XML": true, + "YAML": true, + // Add more special names here +} + func wordStart(b byte) bool { return b >= 'A' && b <= 'Z' || b == '_' } +// gnuOptionName 转换为gnu风格的名字 func gnuOptionName(opt string) (string, error) { var name strings.Builder + if specialNames[opt] { + return opt, nil + } + for i, b := range []byte(opt) { if wordStart(b) { @@ -37,10 +51,14 @@ func gnuOptionName(opt string) (string, error) { return name.String(), nil } +// 环境变量名字是大写,下划线(蛇形)风格 func envOptionName(opt string) (string, error) { var name strings.Builder + if specialNames[opt] { + return opt, nil + } for i, b := range []byte(opt) { if wordStart(b) { diff --git a/env_short_long_name_test.go b/env_short_long_name_test.go index 62a84e0..c1b583f 100644 --- a/env_short_long_name_test.go +++ b/env_short_long_name_test.go @@ -18,6 +18,7 @@ func Test_ShortLongName(t *testing.T) { {"almost_all", "almost-all"}, {"_almost_all", "almost-all"}, {"LongOpt_all", "long-opt-all"}, + {"JSON", "JSON"}, } { got, err := gnuOptionName(tcase.in) assert.NoError(t, err) @@ -32,6 +33,7 @@ func Test_EnvName(t *testing.T) { {"almost_all", "ALMOST_ALL"}, {"_almost_all", "ALMOST_ALL"}, {"envOpt_all", "ENV_OPT_ALL"}, + {"JSON", "JSON"}, } { got, err := envOptionName(tcase.in) assert.NoError(t, err)