-
Notifications
You must be signed in to change notification settings - Fork 10
/
maybe_opt.go
39 lines (31 loc) · 702 Bytes
/
maybe_opt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package clop
import (
"fmt"
"github.com/antlabs/strsim"
)
func (c *Clop) maybeOpt(optionName string) string {
opts := make([]string, len(c.shortAndLong))
index := 0
for k := range c.shortAndLong {
opts[index] = k
index++
}
// 没有长短命令的直接返回
if len(opts) == 0 {
return ""
}
m := strsim.FindBestMatchOne(optionName, opts)
if m.Score > 0.0 {
return m.S
}
return ""
}
func (c *Clop) genMaybeHelpMsg(optionName string) string {
if s := c.maybeOpt(optionName); len(s) > 0 {
return fmt.Sprintf("\n Did you mean --%s?\n", s)
}
if _, ok := c.subcommand[optionName]; ok {
return fmt.Sprintf("\n Did you mean '%s' subcommand?\n", optionName)
}
return ""
}