Skip to content

Commit ec570e7

Browse files
Fix typo in error message and enhance flag parsing logic for command line arguments (#1233)
Fix typo in error message and enhance flag parsing logic for command line arguments #1232 #1083
1 parent 0349962 commit ec570e7

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

cli/parser.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,20 @@ func (p *Parser) parseCommandArg(s string) (flag *Flag, value string, err error)
143143
value = v
144144
}
145145

146-
if strings.HasPrefix(prefix, "--") {
147-
if len(prefix) > 2 {
148-
flag, err = p.detector.detectFlag(prefix[2:])
149-
} else {
150-
err = fmt.Errorf("not support '--' in command line")
151-
}
152-
} else if strings.HasPrefix(prefix, "-") {
153-
if len(prefix) == 2 {
146+
if strings.HasPrefix(prefix, "-") {
147+
// 特殊处理 SSL 证书等以多个连字符开头的情况
148+
if strings.HasPrefix(prefix, "---") {
149+
// 对于以三个或更多连字符开头的参数,视为值而非 flag
150+
value = s
151+
} else if strings.HasPrefix(prefix, "--") {
152+
// 恰好以两个连字符开头的处理为长格式 flag
153+
if len(prefix) > 2 {
154+
flag, err = p.detector.detectFlag(prefix[2:])
155+
} else {
156+
err = fmt.Errorf("not support '--' in command line")
157+
}
158+
} else if len(prefix) == 2 {
159+
// 对于 -x 格式的短 flag
154160
flag, err = p.detector.detectFlagByShorthand(rune(prefix[1]))
155161
} else {
156162
err = fmt.Errorf("not support flag form %s", prefix)

cli/parser_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ func TestParser1(t *testing.T) {
101101
_, _, err = parser.parseCommandArg("-")
102102
assert.NotNil(t, err)
103103
assert.Equal(t, "not support flag form -", err.Error())
104+
105+
// more than two dashes, treat as value
106+
_, v, err = parser.parseCommandArg("---a")
107+
assert.Nil(t, err)
108+
assert.Equal(t, "---a", v)
109+
110+
// contain ==
111+
_, v, err = parser.parseCommandArg("----a==2")
112+
assert.Nil(t, err)
113+
assert.Equal(t, "----a==2", v)
104114
}
105115

106116
// 2. can parse args and flags

openapi/commando.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func (c *Commando) createInvoker(ctx *cli.Context, productCode string, apiOrMeth
304304
style, _ := ctx.Flags().Get("style").GetValue()
305305
if style == "" {
306306
return nil, cli.NewErrorWithTip(fmt.Errorf("uncheked version %s", version),
307-
"Please use --style to speicify API sytle, rpc or restful.")
307+
"Please use --style to specify API style, rpc or restful.")
308308
}
309309
product.ApiStyle = style
310310
}

0 commit comments

Comments
 (0)