-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
163 lines (134 loc) · 3.59 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"errors"
"fmt"
"slices"
"strings"
)
var (
ErrArgParse = errors.New("error parsing arguments")
ErrHelp = errors.New("help requested")
)
type ParsedArgs struct {
Options map[string]string
Destination string
CommandWithArgs []string
SSHArgs []string
}
func parseLongOption(args []string, idx int, parsedArgs *ParsedArgs) (int, error) {
consumedLongOptionsWithArguments := []string{
"--list-columns",
"--region",
"--profile",
"--destination-type",
"--address-type",
"--eice-id",
}
consumedLongOptionsWithoutArguments := []string{
"--list",
"--no-send-keys",
"--use-eice",
"--debug",
}
arg := args[idx]
if arg == "--help" {
return idx, ErrHelp
}
if slices.Contains(consumedLongOptionsWithoutArguments, arg) {
if _, ok := parsedArgs.Options[arg]; !ok {
parsedArgs.Options[arg] = "true"
}
return idx, nil
}
option, value, includesValue := strings.Cut(arg, "=")
if slices.Contains(consumedLongOptionsWithArguments, option) {
if !includesValue {
/* value is in the next argument */
if idx+1 >= len(args) {
return idx, fmt.Errorf("%w: missing value for %s", ErrArgParse, arg)
}
value = args[idx+1]
idx++
}
if _, ok := parsedArgs.Options[arg]; !ok {
parsedArgs.Options[option] = value
}
return idx, nil
}
/* SSH doesn't support long options, so we error out here */
return idx, fmt.Errorf("%w: unknown option %s", ErrArgParse, arg)
}
func parseShortOption(args []string, idx int, parsedArgs *ParsedArgs) (int, error) {
consumedOptionsWithArguments := "lpi"
unconsumedOptionsWithArguments := "BbcDEeFIiJLlmOoPpRSWw"
optionsWithArguments := consumedOptionsWithArguments + unconsumedOptionsWithArguments
flags := args[idx][1:]
for flagIdx := 0; flagIdx < len(flags); flagIdx++ {
flag := string(flags[flagIdx])
if flag == "h" {
return idx, ErrHelp
}
/* we don't consume any options without arguments */
if !strings.Contains(optionsWithArguments, flag) {
parsedArgs.SSHArgs = append(parsedArgs.SSHArgs, "-"+flag)
continue
}
var value string
/* value is attached to the current flag */
if flagIdx+1 < len(flags) {
value = flags[flagIdx+1:]
flagIdx = len(flags) // Stop iterating over current argument
} else {
/* value is in the next argument */
if idx+1 >= len(args) {
return idx, fmt.Errorf("%w: missing value for %s", ErrArgParse, args[idx])
}
value = args[idx+1]
idx++
}
if strings.Contains(consumedOptionsWithArguments, flag) {
if _, ok := parsedArgs.Options["-"+flag]; !ok {
parsedArgs.Options["-"+flag] = value
}
} else {
parsedArgs.SSHArgs = append(parsedArgs.SSHArgs, "-"+flag+value)
}
}
return idx, nil
}
func ParseArgs(args []string) (ParsedArgs, error) {
var err error
parsedArgs := ParsedArgs{
Options: make(map[string]string),
Destination: "",
CommandWithArgs: []string{},
SSHArgs: []string{},
}
loop:
for idx := 0; idx < len(args); idx++ {
arg := args[idx]
switch {
case arg == "--":
parsedArgs.CommandWithArgs = args[idx+1:]
break loop
case strings.HasPrefix(arg, "--"): /* long option */
idx, err = parseLongOption(args, idx, &parsedArgs)
if err != nil {
return ParsedArgs{}, err
}
case strings.HasPrefix(arg, "-") && len(arg) > 1: /* short option */
idx, err = parseShortOption(args, idx, &parsedArgs)
if err != nil {
return ParsedArgs{}, err
}
default: /* non-option argument */
if parsedArgs.Destination == "" {
parsedArgs.Destination = arg
} else {
parsedArgs.CommandWithArgs = args[idx:]
break loop
}
}
}
return parsedArgs, nil
}