@@ -40,68 +40,77 @@ struct CommandConfig {
40
40
}
41
41
42
42
extension CommandConfig {
43
- static func helpRequested(
44
- argumentExtractor: inout ArgumentExtractor
45
- ) -> Bool {
46
- let help = argumentExtractor. extractFlag ( named: OptionsAndFlags . help. rawValue)
47
- return help != 0
48
- }
49
-
50
43
static func parse(
51
44
argumentExtractor argExtractor: inout ArgumentExtractor ,
52
45
pluginWorkDirectory: URL
53
- ) throws -> ( CommandConfig , [ String ] ) {
46
+ ) throws -> CommandConfig {
54
47
var config = CommandConfig . defaults
55
48
56
49
for flag in OptionsAndFlags . allCases {
57
50
switch flag {
58
51
case . accessLevel:
59
- let accessLevel = argExtractor. extractOption ( named: flag. rawValue)
60
- if let value = extractSingleValue ( flag, values: accessLevel) {
52
+ if let value = argExtractor. extractSingleOption ( named: flag. rawValue) {
61
53
if let accessLevel = GenerationConfig . AccessLevel ( rawValue: value) {
62
54
config. common. accessLevel = accessLevel
63
55
} else {
64
- Diagnostics . error ( " Unknown access level '-- \( flag . rawValue ) ' \ ( value) " )
56
+ throw CommandPluginError . unknownAccessLevel ( value)
65
57
}
66
58
}
67
59
68
- case . servers, . noServers:
69
- if flag == . noServers { continue } // only process this once
60
+ case . noServers:
61
+ // Handled by `.servers`
62
+ continue
63
+ case . servers:
70
64
let servers = argExtractor. extractFlag ( named: OptionsAndFlags . servers. rawValue)
71
65
let noServers = argExtractor. extractFlag ( named: OptionsAndFlags . noServers. rawValue)
72
- if noServers > servers {
66
+ if servers > 0 && noServers > 0 {
67
+ throw CommandPluginError . conflictingFlags ( OptionsAndFlags . servers. rawValue, OptionsAndFlags . noServers. rawValue)
68
+ } else if servers > 0 {
69
+ config. common. servers = true
70
+ } else if noServers > 0 {
73
71
config. common. servers = false
74
72
}
75
73
76
- case . clients, . noClients:
77
- if flag == . noClients { continue } // only process this once
74
+ case . noClients:
75
+ // Handled by `.clients`
76
+ continue
77
+ case . clients:
78
78
let clients = argExtractor. extractFlag ( named: OptionsAndFlags . clients. rawValue)
79
79
let noClients = argExtractor. extractFlag ( named: OptionsAndFlags . noClients. rawValue)
80
- if noClients > clients {
80
+ if clients > 0 && noClients > 0 {
81
+ throw CommandPluginError . conflictingFlags ( OptionsAndFlags . clients. rawValue, OptionsAndFlags . noClients. rawValue)
82
+ } else if clients > 0 {
83
+ config. common. clients = true
84
+ } else if noClients > 0 {
81
85
config. common. clients = false
82
86
}
83
87
84
- case . messages, . noMessages:
85
- if flag == . noMessages { continue } // only process this once
88
+ case . noMessages:
89
+ // Handled by `.messages`
90
+ continue
91
+ case . messages:
86
92
let messages = argExtractor. extractFlag ( named: OptionsAndFlags . messages. rawValue)
87
93
let noMessages = argExtractor. extractFlag ( named: OptionsAndFlags . noMessages. rawValue)
88
- if noMessages > messages {
94
+ if messages > 0 && noMessages > 0 {
95
+ throw CommandPluginError . conflictingFlags ( OptionsAndFlags . messages. rawValue, OptionsAndFlags . noMessages. rawValue)
96
+ } else if messages > 0 {
97
+ config. common. messages = true
98
+ } else if noMessages > 0 {
89
99
config. common. messages = false
90
100
}
91
101
92
102
case . fileNaming:
93
- let fileNaming = argExtractor . extractOption ( named : flag . rawValue )
94
- if let value = extractSingleValue ( flag , values : fileNaming ) {
103
+
104
+ if let value = argExtractor . extractSingleOption ( named : flag . rawValue ) {
95
105
if let fileNaming = GenerationConfig . FileNaming ( rawValue: value) {
96
106
config. common. fileNaming = fileNaming
97
107
} else {
98
- Diagnostics . error ( " Unknown file naming strategy '-- \( flag . rawValue ) ' \ ( value) " )
108
+ throw CommandPluginError . unknownFileNamingStrategy ( value)
99
109
}
100
110
}
101
111
102
112
case . accessLevelOnImports:
103
- let accessLevelOnImports = argExtractor. extractOption ( named: flag. rawValue)
104
- if let value = extractSingleValue ( flag, values: accessLevelOnImports) {
113
+ if let value = argExtractor. extractSingleOption ( named: flag. rawValue) {
105
114
guard let accessLevelOnImports = Bool ( value) else {
106
115
throw CommandPluginError . invalidArgumentValue ( name: flag. rawValue, value: value)
107
116
}
@@ -112,13 +121,10 @@ extension CommandConfig {
112
121
config. common. importPaths = argExtractor. extractOption ( named: flag. rawValue)
113
122
114
123
case . protocPath:
115
- let protocPath = argExtractor. extractOption ( named: flag. rawValue)
116
- config. common. protocPath = extractSingleValue ( flag, values: protocPath)
124
+ config. common. protocPath = argExtractor. extractSingleOption ( named: flag. rawValue)
117
125
118
- case . output:
119
- let output = argExtractor. extractOption ( named: flag. rawValue)
120
- config. common. outputPath =
121
- extractSingleValue ( flag, values: output) ?? pluginWorkDirectory. absoluteStringNoScheme
126
+ case . outputPath:
127
+ config. common. outputPath = argExtractor. extractSingleOption ( named: flag. rawValue) ?? pluginWorkDirectory. absoluteStringNoScheme
122
128
123
129
case . verbose:
124
130
let verbose = argExtractor. extractFlag ( named: flag. rawValue)
@@ -133,27 +139,24 @@ extension CommandConfig {
133
139
}
134
140
}
135
141
136
- if argExtractor. remainingArguments. isEmpty {
137
- throw CommandPluginError . missingInputFile
138
- }
139
-
140
- for argument in argExtractor. remainingArguments {
141
- if argument. hasPrefix ( " -- " ) {
142
- throw CommandPluginError . unknownOption ( argument)
143
- }
142
+ if let argument = argExtractor. remainingArguments. first {
143
+ throw CommandPluginError . unknownOption ( argument)
144
144
}
145
145
146
- return ( config, argExtractor . remainingArguments )
146
+ return config
147
147
}
148
148
}
149
149
150
- func extractSingleValue( _ flag: OptionsAndFlags , values: [ String ] ) -> String ? {
151
- if values. count > 1 {
152
- Stderr . print (
153
- " Warning: '-- \( flag. rawValue) ' was unexpectedly repeated, the first value will be used. "
154
- )
150
+ extension ArgumentExtractor {
151
+ mutating func extractSingleOption( named optionName: String ) -> String ? {
152
+ let values = self . extractOption ( named: optionName)
153
+ if values. count > 1 {
154
+ Diagnostics . warning (
155
+ " '-- \( optionName) ' was unexpectedly repeated, the first value will be used. "
156
+ )
157
+ }
158
+ return values. first
155
159
}
156
- return values. first
157
160
}
158
161
159
162
/// All valid input options/flags
@@ -169,7 +172,7 @@ enum OptionsAndFlags: String, CaseIterable {
169
172
case accessLevelOnImports = " access-level-on-imports "
170
173
case importPath = " import-path "
171
174
case protocPath = " protoc-path "
172
- case output
175
+ case outputPath = " output-path "
173
176
case verbose
174
177
case dryRun = " dry-run "
175
178
@@ -205,7 +208,7 @@ extension OptionsAndFlags {
205
208
return " The path to the protoc binary. "
206
209
case . dryRun:
207
210
return " Print but do not execute the protoc commands. "
208
- case . output :
211
+ case . outputPath :
209
212
return " The path into which the generated source files are created. "
210
213
case . verbose:
211
214
return " Emit verbose output. "
@@ -222,7 +225,7 @@ extension OptionsAndFlags {
222
225
printMessage = Stderr . print
223
226
}
224
227
225
- printMessage ( " Usage: swift package generate-grpc-code-from-protos [flags] [input files] " )
228
+ printMessage ( " Usage: swift package generate-grpc-code-from-protos [flags] [--] [ input files] " )
226
229
printMessage ( " " )
227
230
printMessage ( " Flags: " )
228
231
printMessage ( " " )
0 commit comments