Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

专用名字不转换 #108

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading