-
Notifications
You must be signed in to change notification settings - Fork 14
/
flag.go
46 lines (40 loc) · 831 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
package command
import (
"flag"
"strings"
)
type Args []string
func NewArgs(args []string) Args {
return args[1:]
}
// Filter returns all command line arguments which will match the given flag.FlagSet.
func (a Args) Filter(set *flag.FlagSet) Args {
if set == nil {
return a
}
var args Args
for i, arg := range a {
name := arg[1:]
if idx := strings.Index(name, "="); idx > -1 {
name = name[:idx]
}
if f := set.Lookup(name); f != nil {
if name == arg[1:] {
if iFn, ok := f.Value.(interface{ IsBoolFlag() bool }); ok {
if iFn.IsBoolFlag() {
args = append(args, a[i:i+1]...)
continue
}
}
if len(a[i:]) > 1 {
args = append(args, a[i:i+2]...)
} else {
args = append(args, a[i:i+1]...)
}
continue
}
args = append(args, a[i])
}
}
return args
}