-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
91 lines (77 loc) · 1.99 KB
/
Copy pathoptions.go
File metadata and controls
91 lines (77 loc) · 1.99 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package commandergo
import (
"fmt"
"regexp"
)
type Options []*Option
func (o Options) has(name string) bool {
for _, opt := range o {
if opt.name == name {
return true
}
}
return false
}
func (o Options) get(name string) (*Option, bool) {
for _, opt := range o {
if opt.name == name {
return opt, true
}
}
return nil, false
}
type Option struct {
name string
alias string
desc string
defaultValue Varaint
valueRequired bool
valueName string
}
var OPTION_FLAG_PATTERN = regexp.MustCompile(`^\s*(?:(?:-([a-zA-Z])(?:(?:\s+)|(?:\s*,\s*))\-\-([a-zA-Z-]+)\s+(?:\[\s*([a-zA-Z]+)\s*\]|<\s*([a-zA-Z]+)\s*>))|(?:-([a-zA-Z])(?:(?:\s+)|(?:\s*,\s*))\-\-([a-zA-Z-]+))|(?:\-\-([a-zA-Z-]+)\s+(?:(?:\[\s*([a-zA-Z]+)\s*\])|(?:\<\s*([a-zA-Z]+)\s*\>)))|(?:\-\-([a-zA-Z-]+)))\s*$`)
/**
* ps: --a | -a --abc | --a [valueName...] | -a --abc <valueName...>
*/
func NewOption(flag string) (*Option, error) {
if !OPTION_FLAG_PATTERN.MatchString(flag) {
return nil, fmt.Errorf("invalid option flag :%v", flag)
}
group := OPTION_FLAG_PATTERN.FindStringSubmatch(flag)
tryGroup := func(indexs []int) string {
for _, index := range indexs {
if group[index] != "" {
return group[index]
}
}
return ""
}
name := func() string {
n := tryGroup([]int{2, 6, 7, 10})
if n != "" {
return n
}
panic(fmt.Errorf("invalid option flag :%v", flag))
}()
alias := tryGroup([]int{1, 5})
valueName := tryGroup([]int{3, 4, 8, 9})
valueRequired := group[4] != "" || group[9] != ""
return &Option{
name: name,
alias: alias,
valueName: valueName,
valueRequired: valueRequired,
}, nil
}
func (c *Command) options(flag, desc string, defaultValue any) *Command {
option, err := NewOption(flag)
if err != nil {
panic(err)
}
if c._options.has(option.name) {
panic(fmt.Errorf("option %s already exists", option.name))
}
option.desc = desc
option.defaultValue = Varaint{value: defaultValue}
c._options = append(c._options, option)
return c
}