-
Notifications
You must be signed in to change notification settings - Fork 6
/
args.go
53 lines (44 loc) · 1.22 KB
/
args.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
package vaultcli
import (
"strings"
cst "github.com/DelineaXPM/dsv-cli/constants"
)
func ToFlagName(flag string) string {
return strings.ReplaceAll(flag, ".", "-")
}
func GetFlagVal(flag string, args []string) string {
shortFlag := cst.GetShortFlag(flag)
flagNames := []string{"--" + ToFlagName(flag)} // Long form of the flag. For example: `--config`
if shortFlag != "" {
flagNames = append(flagNames, `-`+shortFlag) // Add short form of the flag. For example: `-c`
}
for i, arg := range args {
for _, flagName := range flagNames {
if arg == flagName && len(args)-1 >= i+1 {
return args[i+1]
}
if strings.HasPrefix(arg, flagName+"=") {
return arg[len(flagName)+1:]
}
}
}
return ""
}
// GetFilenameFromArgs tries to extract a filename from args. If args has a --data or -d flag and
// its value starts with an '@' followed by a filename, the function tries to capture that filename.
func GetFilenameFromArgs(args []string) string {
var fileName string
for i := range args {
if args[i] == "--data" || args[i] == "-d" {
if i+1 == len(args) {
break
}
value := args[i+1]
if strings.HasPrefix(value, cst.CmdFilePrefix) {
fileName = value[1:]
}
break
}
}
return fileName
}