/
usage.swift
299 lines (258 loc) · 9.13 KB
/
usage.swift
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
This source file is part of the Swift.org open source project
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import enum Build.Configuration
import Multitool
func usage(print: (String) -> Void = { print($0) }) {
//.........10.........20.........30.........40.........50.........60.........70..
print("OVERVIEW: Build sources into binary products")
print("")
print("USAGE: swift build [options]")
print("")
print("MODES:")
print(" --configuration <value> Build with configuration (debug|release) [-c]")
print(" --clean[=<mode>] Delete artefacts (build|dist) [-k]")
print(" --init Creates a new Swift project")
print(" --fetch Fetch package dependencies")
print("")
print("OPTIONS:")
print(" --chdir <value> Change working directory before any other operation [-C]")
print(" -v[v] Increase verbosity of informational output")
print(" -Xcc <flag> Pass flag through to all C compiler instantiations")
print(" -Xlinker <flag> Pass flag through to all linker instantiations")
print(" -Xswiftc <flag> Pass flag through to all Swift compiler instantiations")
}
enum CleanMode: String {
case Build = "build"
case Dist = "dist"
}
enum Mode {
case Build(Configuration)
case Clean(CleanMode)
case Fetch
case Init
case Usage
case Version
}
struct Options {
var chdir: String? = nil
var verbosity: Int = 0
var Xcc: [String] = []
var Xld: [String] = []
var Xswiftc: [String] = []
}
func parse(commandLineArguments args: [String]) throws -> (Mode, Options) {
var opts = Options()
var mode: Mode?
//TODO refactor
var skipNext = false
var cruncher = Cruncher(args: args.flatMap { arg -> [String] in
if skipNext {
skipNext = false
return [arg]
}
if "-Xcc" == arg || "-Xlinker" == arg || "-Xswiftc" == arg {
skipNext = true
return [arg]
}
// split short form arguments so Cruncher can work with them,
// eg. -vv is split into -v -v
if arg.hasPrefix("-") && !arg.hasPrefix("--") {
return arg.characters.dropFirst().map{ "-" + String($0) }
}
// split applicative arguments so Cruncher can work with them,
// eg. --mode=value splits into --mode =value
let argParts = arg.characters.split{ $0 == "=" }.map{ String($0) }
if argParts.count > 1 {
return argParts
}
return [arg]
})
while cruncher.shouldContinue {
switch try cruncher.pop() {
case .Mode(let newMode):
switch (mode, newMode) {
case (let a?, let b) where a == b:
break
case (.Usage?, let ignoredArgument):
throw CommandLineError.InvalidUsage("Both --help and \(ignoredArgument) specified", .Print)
case (let ignoredArgument?, .Usage):
throw CommandLineError.InvalidUsage("Both --help and \(ignoredArgument) specified", .Print)
case (let oldMode?, let newMode):
throw CommandLineError.InvalidUsage("Multiple modes specified: \(oldMode), \(newMode)", .Imply)
case (nil, .Build):
switch try cruncher.peek() {
case .Name("debug")?:
mode = .Build(.Debug)
cruncher.postPeekPop()
case .Name("release")?:
mode = .Build(.Release)
cruncher.postPeekPop()
case .Name(let name)?:
throw CommandLineError.InvalidUsage("Unknown build configuration: \(name)", .Imply)
default:
break
}
case (nil, .Usage):
mode = .Usage
case (nil, .Init):
mode = .Init
case (nil, .Clean):
mode = .Clean(.Build)
switch try cruncher.peek() {
case .Name("build")?:
cruncher.postPeekPop()
case .Name("dist")?:
mode = .Clean(.Dist)
cruncher.postPeekPop()
case .Name(let name)?:
throw CommandLineError.InvalidUsage("Unknown clean mode: \(name)", .Imply)
default:
break
}
case (nil, .Version):
mode = .Version
case (nil, .Fetch):
mode = .Fetch
}
case .Switch(.Chdir):
switch try cruncher.peek() {
case .Name(let name)?:
cruncher.postPeekPop()
opts.chdir = name
default:
throw CommandLineError.InvalidUsage("Option `--chdir' requires subsequent directory argument", .Imply)
}
case .Switch(.Verbose):
opts.verbosity += 1
case .Name(let name):
throw CommandLineError.InvalidUsage("Unknown argument: \(name)", .Imply)
case .Switch(.Xcc):
opts.Xcc.append(try cruncher.rawPop())
case .Switch(.Xlinker):
opts.Xld.append(try cruncher.rawPop())
case .Switch(.Xswiftc):
opts.Xswiftc.append(try cruncher.rawPop())
}
}
return (mode ?? .Build(.Debug), opts)
}
extension CleanMode: CustomStringConvertible {
var description: String {
return "=\(self.rawValue)"
}
}
extension Mode: CustomStringConvertible {
var description: String { //FIXME shouldn't be necessary!
switch self {
case .Build(let conf): return "--build \(conf)"
case .Clean(let cleanMode): return "--clean=\(cleanMode)"
case .Fetch: return "--fetch"
case .Init: return "--init"
case .Usage: return "--help"
case .Version: return "--version"
}
}
}
private struct Cruncher {
enum Crunch {
enum TheMode: String {
case Build = "--configuration"
case Clean = "--clean"
case Fetch = "--fetch"
case Init = "--init"
case Usage = "--help"
case Version = "--version"
init?(rawValue: String) {
switch rawValue {
case Build.rawValue, "-c":
self = .Build
case Clean.rawValue, "-k":
self = .Clean
case Fetch.rawValue:
self = .Fetch
case Init.rawValue:
self = .Init
case Usage.rawValue:
self = .Usage
case Version.rawValue:
self = .Version
default:
return nil
}
}
}
enum TheSwitch: String {
case Chdir = "--chdir"
case Verbose = "--verbose"
case Xcc = "-Xcc"
case Xlinker = "-Xlinker"
case Xswiftc = "-Xswiftc"
init?(rawValue: String) {
switch rawValue {
case Chdir.rawValue, "-C":
self = .Chdir
case Verbose.rawValue, "-v":
self = .Verbose
case Xcc.rawValue:
self = .Xcc
case Xlinker.rawValue:
self = .Xlinker
case Xswiftc.rawValue:
self = .Xswiftc
default:
return nil
}
}
}
case Mode(TheMode)
case Switch(TheSwitch)
case Name(String)
}
var args: [String]
var shouldContinue: Bool {
return !args.isEmpty
}
func parse(arg: String) throws -> Crunch {
if let mode = Crunch.TheMode(rawValue: arg) {
return .Mode(mode)
}
if let theSwitch = Crunch.TheSwitch(rawValue: arg) {
return .Switch(theSwitch)
}
guard !arg.hasPrefix("-") else {
throw CommandLineError.InvalidUsage("unknown argument: \(arg)", .Imply)
}
return .Name(arg)
}
mutating func rawPop() throws -> String {
guard args.count > 0 else { throw CommandLineError.InvalidUsage("expected argument", .Imply) }
return args.removeFirst()
}
mutating func pop() throws -> Crunch {
return try parse(args.removeFirst())
}
mutating func postPeekPop() {
args.removeFirst()
}
func peek() throws -> Crunch? {
guard let arg = args.first else {
return nil
}
return try parse(arg)
}
}
private func ==(lhs: Mode, rhs: Cruncher.Crunch.TheMode) -> Bool {
switch lhs {
case .Build: return rhs == .Build
case .Clean: return rhs == .Clean
case .Fetch: return rhs == .Fetch
case .Init: return rhs == .Init
case .Version: return rhs == .Version
case .Usage: return rhs == .Usage
}
}