-
Notifications
You must be signed in to change notification settings - Fork 3
/
flags.go
63 lines (53 loc) · 1.46 KB
/
flags.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
package flags
import (
"fmt"
"regexp"
)
var switchFlagRx = regexp.MustCompile("^--([a-z][-a-zA-z_0-9]*)$")
var valueFlagRx = regexp.MustCompile("^--([a-z][-a-zA-z_0-9]*)=\"?([^\"]*)\"?$")
var maybeFlagRx = regexp.MustCompile("^--.*")
const TrueValue = "true"
type ErrRepeatedFlag struct {
Arg string
}
func (err ErrRepeatedFlag) Error() string {
return fmt.Sprintf("Flag %q specified more than once", err.Arg)
}
type ErrInvalidFlag struct {
Arg string
}
func (err ErrInvalidFlag) Error() string {
return fmt.Sprintf("Flag syntax invalid: %q", err.Arg)
}
func Parse(args []string) ([]string, map[string]string, error) {
positionalArgs := make([]string, 0, len(args))
flags := make(map[string]string)
for _, arg := range args {
flag, value, ok, err := parseFlag(arg)
if err != nil {
return nil, nil, err
}
if ok {
if _, exists := flags[flag]; exists {
return nil, nil, ErrRepeatedFlag{
Arg: arg,
}
}
flags[flag] = value
} else {
positionalArgs = append(positionalArgs, arg)
}
}
return positionalArgs, flags, nil
}
func parseFlag(arg string) (string, string, bool, error) {
if maybeFlagRx.MatchString(arg) {
if matches := switchFlagRx.FindStringSubmatch(arg); len(matches) == 2 {
return matches[1], TrueValue, true, nil
} else if matches := valueFlagRx.FindStringSubmatch(arg); len(matches) == 3 {
return matches[1], matches[2], true, nil
}
return "", "", false, ErrInvalidFlag{Arg: arg}
}
return arg, arg, false, nil
}