forked from johnkerl/miller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getoptify.go
43 lines (41 loc) · 1.36 KB
/
getoptify.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
package lib
import (
"regexp"
"strings"
)
// Getoptify expands "-xyz" into "-x -y -z" while leaving "--xyz" intact. This
// is a keystroke-saver for the user.
//
// This is OK to do here globally since Miller is quite consistent (in main,
// verbs, auxents, and terminals) that multi-character options start with two
// dashes, e.g. "--csv". (The sole exception is the sort verb's -nf/-nr which
// are handled specially there.)
//
// Additionally, we split "--foo=bar" into "--foo" and "bar".
func Getoptify(inargs []string) []string {
expandRegex := regexp.MustCompile("^-[a-zA-Z0-9]+$")
splitRegex := regexp.MustCompile("^--[^=]+=.+$")
numberRegex := regexp.MustCompile("^-[0-9]+$")
outargs := make([]string, 0)
for _, inarg := range inargs {
if expandRegex.MatchString(inarg) {
if numberRegex.MatchString(inarg) {
// Don't expand things like '-12345' which are (likely!) numeric arguments to verbs.
// Example: 'mlr unsparsify --fill-with -99999'.
outargs = append(outargs, inarg)
} else {
for _, c := range inarg[1:] {
outargs = append(outargs, "-"+string(c))
}
}
} else if splitRegex.MatchString(inarg) {
pair := strings.SplitN(inarg, "=", 2)
InternalCodingErrorIf(len(pair) != 2)
outargs = append(outargs, pair[0])
outargs = append(outargs, pair[1])
} else {
outargs = append(outargs, inarg)
}
}
return outargs
}