-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.go
48 lines (43 loc) · 1023 Bytes
/
match.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
package clo
import (
"fmt"
)
func matchArg(
token string,
cmd string,
validCmdArg func(string, string) (string, error)) (string, error) {
if !isArg(token) {
return "", nil
}
arg, err := validCmdArg(cmd, trimArgPrefix(token))
return arg, err
}
// match a token to a given token type, in the context of existing
// command, argument (applicable to tokens.argument and tokens.value, tokens.valueFixed, tokens.valueDefault)
func match(
token string,
tokenType int,
cmdCtx, argCtx string,
def *definitions) (string, error) {
if def == nil {
return "", fmt.Errorf("cannot match token with nil definitions")
}
switch tokenType {
case command:
return def.definedCmd(token)
case argument:
return matchArg(token, cmdCtx, def.definedArg)
case value:
if isArg(token) {
break
}
val, err := def.definedVal(cmdCtx, argCtx, token)
return val, err
default:
return "", fmt.Errorf(
"cannot confirm match for a token '%v' of type '%v'",
token,
tokenString(tokenType))
}
return "", nil
}