-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
188 lines (144 loc) · 3.17 KB
/
parser.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package program
import (
"maps"
"os"
"strings"
)
func (p *Program) parse() {
args := p.parseOptions(os.Args[1:], p.options)
if p.IsOptionSet("help") {
return
}
if p.command == nil {
args = p.parseArguments(args, p.arguments)
} else {
args = p.parseCommand(args)
options := make(map[string]*Option)
maps.Copy(options, p.options)
maps.Copy(options, p.selectedCommand.options)
args = p.parseOptions(args, options)
isHelpCommand := p.selectedCommand != nil &&
p.selectedCommand.FullName == "help"
if p.IsOptionSet("help") && !isHelpCommand {
return
}
args = p.parseArguments(args, p.selectedCommand.arguments)
}
}
func (p *Program) parseOptions(args []string, options map[string]*Option) []string {
for len(args) > 0 {
arg := args[0]
if arg == "--" || !isOption(arg) {
break
}
key := strings.TrimLeft(arg, "-")
opt, found := options[key]
if !found {
p.Fatal("unknown option %q", key)
}
opt.Set = true
if opt.ValueName == "" {
args = args[1:]
} else {
if len(args) < 2 {
p.Fatal("missing value for option %q", key)
}
opt.Value = args[1]
args = args[2:]
}
}
return args
}
func (p *Program) parseCommand(args []string) []string {
p.selectedCommand = p.command
if len(args) == 0 {
p.Fatal("missing command")
}
cmd := p.command
names := []string{}
for len(args) > 0 {
arg := args[0]
if arg == "--" || isOption(arg) {
break
}
names = append(names, arg)
cmd2 := cmd.subcommands[arg]
if cmd2 == nil {
break
}
cmd = cmd2
args = args[1:]
}
fullName := strings.Join(names, " ")
if cmd == p.command {
p.Fatal("unknown command %q", fullName)
}
if cmd.Main == nil {
if len(args) == 0 {
p.Fatal("missing subcommand(s) for command %q", cmd.FullName)
} else {
p.Fatal("unknown command %q", strings.Join(names, " "))
}
}
p.selectedCommand = cmd
return args
}
func (p *Program) parseArguments(args []string, arguments []*Argument) []string {
if len(arguments) > 0 {
// Mandatory arguments
min := 0
for _, argument := range arguments {
if argument.Optional || argument.Trailing {
break
}
min++
}
if len(args) < min {
p.Fatal("missing argument(s)")
}
for i := 0; i < min; i++ {
argument := arguments[i]
argument.Set = true
argument.Value = args[i]
}
args = args[min:]
arguments = arguments[min:]
// Optional arguments
var trailingArgument *Argument
for _, argument := range arguments {
if len(args) == 0 {
break
}
if argument.Trailing {
trailingArgument = argument
break
}
argument.Set = true
argument.Value = args[0]
args = args[1:]
}
// Trailing argument
if trailingArgument != nil {
trailingArgument.TrailingValues = args
args = args[len(args):]
} else {
if len(args) > 0 {
p.Fatal("too many arguments")
}
}
} else {
if len(args) > 0 {
p.Fatal("unexpected arguments")
}
}
return args
}
func isOption(arg string) bool {
return isShortOption(arg) || isLongOption(arg)
}
func isShortOption(arg string) bool {
return len(arg) == 2 && arg[0] == '-' && arg[1] != '-'
}
func isLongOption(arg string) bool {
return len(arg) > 2 && arg[0] == '-' && arg[1] == '-'
}