-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargvalue.go
90 lines (72 loc) · 1.63 KB
/
argvalue.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
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
package arg
import (
"strings"
)
func newPosixArg(value string, cmd *Cmd) *posixArg {
return &posixArg{runes: getRunes(value), cmd: cmd}
}
type posixArg struct {
cmd *Cmd
runes []rune
}
func (as *posixArg) getOptions() ([]option, error) {
if len(as.runes) < 2 || as.runes[0] != '-' {
return nil, nil
}
if as.runes[0] == '-' && as.runes[1] == '-' {
return as.getLongOption()
}
return as.getShortOptions()
}
func (as *posixArg) getLongOption() ([]option, error) {
var opt option
var opts []option
rhs := string(as.runes[2:])
tokens := strings.Split(rhs, "=")
cmdOpt, exist := as.cmd.longOption(tokens[0])
if !exist {
return nil, ErrInvalidArgs
}
opt.long = tokens[0]
opt.short = cmdOpt.short
opt.dataType = cmdOpt.dataType
opt.required = cmdOpt.required
//Check for argument
if len(tokens) > 1 {
opt.arg = tokens[1]
}
opts = append(opts, opt)
return opts, nil
}
func (as *posixArg) getShortOptions() ([]option, error) {
var opts []option
rhs := as.runes[1:]
//Process first option
cmdOpt, exist := as.cmd.shortOption(rhs[0])
if !exist {
return nil, ErrInvalidArgs
}
opts = append(opts, option{
dataType: cmdOpt.dataType,
required: cmdOpt.required,
long: cmdOpt.long,
short: rhs[0],
})
//Start processing subsequent options
for i := 1; i < len(rhs); i++ {
//previous option has this argument
cmdOpt, exist := as.cmd.shortOption(rhs[i])
if !exist {
opts[i-1].arg = string(rhs[i:])
break
}
//another good option
opts = append(opts, option{
dataType: cmdOpt.dataType,
required: cmdOpt.required,
long: cmdOpt.long,
short: rhs[i],
})
}
return opts, nil
}