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
/
operation.go
138 lines (100 loc) · 3.08 KB
/
operation.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
package argparser
import "fmt"
type Operation interface {
// Add a boolean switch
AddBooleanSwitch(short rune, long string) Operation
// Add a boolean switch with only a long name
AddLongBooleanSwitch(long string) Operation
// Add an increment switch
AddIncrementSwitch(short rune, long string) Operation
// Add an increment switch with only a long name
AddLongIncrementSwitch(long string) Operation
// Add an data switch
AddDataSwitch(short rune, long string) Operation
// Add an data switch with only a long name
AddLongDataSwitch(long string) Operation
// Set the executor
//
// NOTE: There may be only one executor.
// If you set the executor multiple times, only the latest update will be preserved.
SetExecutor(e func(op Operation, args []string) error) Operation
// Complete the configuration and go back to its parent
Complete() ArgParser
// Get regular boolean switches
BooleanSwitches() map[string]bool
// Get increment switches
IncrementSwitches() map[string]uint
// Get data switches
DataSwitches() map[string]string
// Get data that do not belong to any data switches
Data() []string
}
// Operation implementation
type operation struct {
parent ArgParser
executor func(op Operation, args []string) error
data []string
booleanSwitches map[string]bool
incrementSwitches map[string]uint
dataSwitches map[string]string
switchLongShortMap map[string]rune
switchShortLongMap map[rune]string
}
func (op *operation) AddBooleanSwitch(short rune, long string) Operation {
op.booleanSwitches[string(short)] = false
op.booleanSwitches[long] = false
op.switchLongShortMap[long] = short
op.switchShortLongMap[short] = long
return op
}
func (op *operation) AddLongBooleanSwitch(long string) Operation {
op.booleanSwitches[long] = false
return op
}
func (op *operation) AddIncrementSwitch(short rune, long string) Operation {
op.incrementSwitches[string(short)] = 0
op.incrementSwitches[long] = 0
op.switchLongShortMap[long] = short
op.switchShortLongMap[short] = long
return op
}
func (op *operation) AddLongIncrementSwitch(long string) Operation {
op.incrementSwitches[long] = 0
return op
}
func (op *operation) AddDataSwitch(short rune, long string) Operation {
op.dataSwitches[string(short)] = ""
op.dataSwitches[long] = ""
op.switchLongShortMap[long] = short
op.switchShortLongMap[short] = long
return op
}
func (op *operation) AddLongDataSwitch(long string) Operation {
op.dataSwitches[long] = ""
return op
}
func (op *operation) SetExecutor(e func(Operation, []string) error) Operation {
op.executor = e
return op
}
func (op *operation) Complete() ArgParser {
return op.parent
}
func (op *operation) execute(args []string) error {
if op.executor == nil {
return fmt.Errorf("no executors")
}
return op.executor(op, args)
}
func (op *operation) BooleanSwitches() map[string]bool {
return op.booleanSwitches
}
func (op *operation) IncrementSwitches() map[string]uint {
return op.incrementSwitches
}
func (op *operation) DataSwitches() map[string]string {
return op.dataSwitches
}
func (op *operation) Data() []string {
return op.data
}