-
Notifications
You must be signed in to change notification settings - Fork 99
/
flag.go
48 lines (40 loc) · 960 Bytes
/
flag.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package exec
import (
"fmt"
"regexp"
)
var (
// Be very strict about what kind of format is allowed for flags
fullFlagRegex = regexp.MustCompile(`\A(\-\-[a-z]+(\-[a-z]+)*)=(.+)\z`)
)
type Flag struct {
Name string // e.g. --name
Value string
}
func NewFlagFromString(str string) (Flag, error) {
match := fullFlagRegex.FindStringSubmatch(str)
if len(match) != 4 {
return Flag{}, fmt.Errorf("Expected flag '%s' to be in '--name=val' format", str)
}
name := match[1]
val := match[3]
return Flag{Name: name, Value: val}, nil
}
type FlagSet struct {
included map[string]struct{}
}
func NewFlagSet(optss ...[]string) FlagSet {
result := map[string]struct{}{}
for _, opts := range optss {
for _, opt := range opts {
result[opt] = struct{}{}
}
}
return FlagSet{result}
}
func (s FlagSet) Includes(name string) bool {
_, found := s.included[name]
return found
}