Skip to content

Commit

Permalink
专用名字不转换 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong authored Apr 21, 2024
1 parent 8f69b80 commit ba58e71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions env_short_long_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions env_short_long_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit ba58e71

Please sign in to comment.