This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparser_impl.go
160 lines (128 loc) · 3.57 KB
/
argparser_impl.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
package argparser
import (
"github.com/chardon55/go-argparser/argshifter"
"github.com/chardon55/go-argparser/exceptions"
)
type switchName struct {
Short string
Long string
}
// ArgParser implementation
type argParser struct {
ops map[string]*operation
commands map[string]*operation
}
func (parser *argParser) newOperation() *operation {
return &operation{
parent: parser,
booleanSwitches: make(map[string]bool),
incrementSwitches: make(map[string]uint),
dataSwitches: make(map[string]string),
data: []string{},
switchLongShortMap: make(map[string]rune),
switchShortLongMap: make(map[rune]string),
}
}
func (parser *argParser) AddCommand(name string) Operation {
op := parser.newOperation()
parser.commands[name] = op
return op
}
func (parser *argParser) AddOperation(short rune, long string) Operation {
op := parser.newOperation()
parser.ops[string(short)] = op
parser.ops[long] = op
return op
}
func (parser *argParser) Parse(args []string) error {
shifter := argshifter.NewArgShifter(args)
_, _, prs := shifter.Shift()
if !prs {
return exceptions.NewEmptyArgumentError()
}
// Get operation
operationString, argType, prs := shifter.Shift()
if !prs {
return exceptions.NewNoOperationError()
}
var op *operation
switch argType {
case argshifter.Command:
op, prs = parser.commands[operationString]
if !prs {
return exceptions.NewInvalidOperationError(operationString, exceptions.COMMAND)
}
case argshifter.ShortOption, argshifter.LongOption:
op, prs = parser.ops[operationString]
if !prs {
return exceptions.NewInvalidOperationError(operationString, exceptions.OPERATION)
}
default:
return exceptions.NewNoOperationError()
}
var dataSwitchNamePtr *switchName
value, argType, valPrs := shifter.Shift()
for valPrs {
switch argType {
case argshifter.Data:
if dataSwitchNamePtr != nil {
if len(dataSwitchNamePtr.Short) > 0 {
op.DataSwitches()[dataSwitchNamePtr.Short] = value
}
op.dataSwitches[dataSwitchNamePtr.Long] = value
dataSwitchNamePtr = nil
} else {
op.data = append(op.data, value)
}
case argshifter.ShortOption:
longOp := op.switchShortLongMap[[]rune(value)[0]]
if _, prs := op.booleanSwitches[value]; prs {
op.booleanSwitches[value] = true
op.booleanSwitches[longOp] = op.booleanSwitches[value]
} else if _, prs := op.incrementSwitches[value]; prs {
op.incrementSwitches[value]++
op.incrementSwitches[longOp] = op.incrementSwitches[value]
} else if _, prs := op.dataSwitches[value]; prs {
dataSwitchNamePtr = &switchName{
Short: value,
Long: longOp,
}
}
case argshifter.LongOption:
shortOpRune, opPrs := op.switchLongShortMap[value]
shortOp := string(shortOpRune)
if _, prs := op.booleanSwitches[value]; prs {
op.booleanSwitches[value] = true
if opPrs {
op.booleanSwitches[shortOp] = op.booleanSwitches[value]
}
} else if _, prs := op.incrementSwitches[value]; prs {
op.incrementSwitches[value]++
if opPrs {
op.incrementSwitches[shortOp] = op.incrementSwitches[value]
}
} else if _, prs := op.dataSwitches[value]; prs {
var finalShort string
if opPrs {
finalShort = shortOp
} else {
finalShort = ""
}
dataSwitchNamePtr = &switchName{
Short: finalShort,
Long: value,
}
}
default:
// Nothing to do
}
value, argType, valPrs = shifter.Shift()
}
return op.execute(args)
}
func NewArgParser() ArgParser {
return &argParser{
ops: make(map[string]*operation),
commands: make(map[string]*operation),
}
}