From 271781c7bbd85edbeda8c16a225e5b6a20f69bfb Mon Sep 17 00:00:00 2001 From: guonaihong Date: Sun, 21 Apr 2024 14:01:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=93=E7=94=A8=E5=90=8D=E5=AD=97=E4=B8=8D?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env_short_long_name.go | 18 ++++++++++++++++++ env_short_long_name_test.go | 2 ++ 2 files changed, 20 insertions(+) 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)