-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFlag.swift
More file actions
374 lines (341 loc) · 14.9 KB
/
Flag.swift
File metadata and controls
374 lines (341 loc) · 14.9 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//
// Flag.swift
// CommandLineKit
//
// Created by Matthias Zenger on 25/03/2017.
// Copyright © 2018-2019 Google LLC
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
import Foundation
///
/// A `Flag` object describes a command-line flag in terms of a short name, a long name, and
/// a description. A short name is a character excluding '-', '=', ' ', '0', '1', '2', '3', '4',
/// '5', '6', '7', '8', '9'. A long name is a non-empty string which does not contain whitespace
/// and '=' characters. Also a single dash is not supported as a long name.
///
/// There are two different types of flags: options and arguments. An option is a boolean flag:
/// it is either set or not set. An argument is a flag which comes with at least one argument.
/// Method `isOption` can be used to distinguish between options and arguments.
///
public class Flag {
/// The short name of the flag.
public let shortName: Character?
/// The long name of the flag.
public let longName: String?
/// The description of the flag.
public let helpDescription: String
/// Is set to true if the flag was set on the command-line.
public fileprivate(set) var wasSet: Bool
/// Initializes a flag
fileprivate init(shortName: Character?, longName: String?, description: String) {
assert(shortName != nil || longName != nil, "declared flag without short and long name")
if let name = shortName {
assert(name != "0" && name != "1" && name != "2" && name != "3" && name != "4" &&
name != "5" && name != "6" && name != "7" && name != "8" && name != "9" &&
name != "-" && name != "=" && name != " ",
"short flag name consists of illegal character")
}
if let name = longName {
assert(name.count != 0, "long flag name is empty for flag -\(shortName ?? "?")")
assert(name.contains { $0 == " " || $0 == "=" } == false,
"long flag name --\(name) contains illegal characters")
assert(name != "-", "long flag name --\(name) is a dash")
}
self.shortName = shortName
self.longName = longName
self.helpDescription = description
self.wasSet = false
}
/// Returns true if this flag descriptor represents an option; i.e. a flag without argument.
public var isOption: Bool {
return false
}
/// Parses the arguments of the flag starting `index` in argument array `args`.
public func parse(_ args: [String], at index: Int) throws -> Int {
self.wasSet = true
return index
}
}
///
/// An `Option` is a flag that is either present or absent. If an option is present, the field
/// `wasSet` is set to true. It is also possible to register a callback which gets invoked when
/// the option was found during command-line parsing.
///
public final class Option: Flag {
private let notify: () throws -> Void
/// Initializes an option with a parsing callback.
public init(shortName: Character?,
longName: String?,
description: String,
notify: @escaping () throws -> Void) {
self.notify = notify
super.init(shortName: shortName, longName: longName, description: description)
}
/// Initializes an option without parsing callback.
public override init(shortName: Character?,
longName: String?,
description: String) {
self.notify = {}
super.init(shortName: shortName, longName: longName, description: description)
}
/// Returns true
public override var isOption: Bool {
return true
}
/// Parses an option.
public override func parse(_ args: [String], at index: Int) throws -> Int {
try self.notify()
self.wasSet = true
return index
}
}
///
/// An `Argument` is a flag with parameters. Flags which allow multiple parameters need to
/// get initialized such that `repeated` is set to true. For being able to handle
/// arguments, the argument object needs to be provided a `handler` function which is
/// responsible for parsing the string parameters and persisting the result.
///
public class Argument: Flag {
/// Is this argument repeated?
public let repeated: Bool
/// Handler for parsing and persisting argument values.
internal private(set) var handler: (String) throws -> Void
/// A readable parameter name, used for documentation purposes.
public let paramIdent: String
/// Initializes an argument from the given short name, long name, and description. `repeated`
/// needs to be set to true if the argument accepts multiple parameters. `handler` is used
/// for parsing and persisting parameters.
public init(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
repeated: Bool = false,
handler: @escaping (String) throws -> Void) {
self.repeated = repeated
self.handler = handler
self.paramIdent = paramIdent == nil ? (repeated ? "<value> ..." : "<value>") : paramIdent!
super.init(shortName: shortName, longName: longName, description: description)
}
/// Initializes an argument of type `T` from the given short name, long name, and description.
/// `repeated` needs to be set to true if the argument accepts multiple parameters. `parse` is
/// used to parse the string parameter into a value of type `T`. `set` persists values of
/// type `T`.
public convenience init<T>(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
repeated: Bool = false,
parse: @escaping (String) -> T?,
set: @escaping (T) throws -> Void) {
self.init(shortName: shortName,
longName: longName,
paramIdent: paramIdent,
description: description,
repeated: repeated,
handler: { x in })
self.setHandler(parse: parse, set: set)
}
/// Initializes an argument of type `T` from the given short name, long name, and description.
/// `repeated` needs to be set to true if the argument accepts multiple parameters. This
/// initializer handles subtypes of `ConvertibleFromString` which come with a default parsing
/// method. `set` persists values of type `T`.
public convenience init<T: ConvertibleFromString>(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
repeated: Bool = false,
set: @escaping (T) throws -> Void) {
self.init(shortName: shortName,
longName: longName,
paramIdent: paramIdent,
description: description,
repeated: repeated,
handler: { x in })
self.setHandler(parse: T.from, set: set)
}
/// Initializes an argument of type `T` from the given short name, long name, and description.
/// `repeated` needs to be set to true if the argument accepts multiple parameters. This
/// initializer handles subtypes of `ConvertibleFromString` which come with a default parsing
/// method. `set` persists values of type `T`.
public convenience init<T: RawRepresentable>(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
repeated: Bool = false,
set: @escaping (T) throws -> Void)
where T.RawValue: ConvertibleFromString {
self.init(shortName: shortName,
longName: longName,
paramIdent: paramIdent,
description: description,
repeated: repeated,
handler: { x in })
self.setHandler(parse: T.from, set: set)
}
/// Used internally to construct a handler from a `parse` and `set` function.
fileprivate func setHandler<T>(parse: @escaping (String) -> T?,
set: @escaping (T) throws -> Void) {
self.handler = { [unowned self] arg in
guard let value = parse(arg) else {
throw FlagError(.malformedValue(arg), self)
}
try set(value)
}
}
/// Parses an option.
public override func parse(_ args: [String], at index: Int) throws -> Int {
var i = index
while i < args.count {
let arg = args[i]
if arg == Flags.terminator || arg.hasPrefix(Flags.longNamePrefix) {
guard self.repeated else {
throw FlagError(.missingValue, self)
}
return i
} else if arg.hasPrefix("-0") || arg.hasPrefix("-1") || arg.hasPrefix("-2") ||
arg.hasPrefix("-3") || arg.hasPrefix("-4") || arg.hasPrefix("-5") ||
arg.hasPrefix("-6") || arg.hasPrefix("-7") || arg.hasPrefix("-8") ||
arg.hasPrefix("-9") {
try self.handler(arg)
self.wasSet = true
i += 1
guard self.repeated else {
return i
}
} else if arg.hasPrefix("-") {
guard self.repeated else {
throw FlagError(.missingValue, self)
}
return i
} else {
try self.handler(arg)
self.wasSet = true
i += 1
guard self.repeated else {
return i
}
}
}
guard self.repeated else {
throw FlagError(.missingValue, self)
}
return i
}
}
///
/// A `SingletonArgument` encapsulates a single, optional parameter value of type `T`. This
/// class is most commonly used in command-line applications for handling arguments with a
/// single parameter where the parameter extracted from the command-line is stored in the
/// flag object itself.
///
public final class SingletonArgument<T>: Argument {
public var value: T?
/// Initializes a singleton argument from the given short name, long name, and description.
/// `value` is a default value for the parameter. `parse` is used to parse strings into
/// values of type `T`.
public init(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
value: T? = nil,
parse: @escaping (String) -> T?) {
self.value = value
super.init(shortName: shortName,
longName: longName,
paramIdent: paramIdent,
description: description,
handler: { x in })
self.setHandler(parse: parse, set: { [unowned self] value in self.value = value })
}
}
extension SingletonArgument where T: ConvertibleFromString {
/// Initializes a singleton argument from the given short name, long name, and description.
/// `value` is a default value for the parameter. The default parsing function is used.
public convenience init(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
value: T? = nil) {
self.init(shortName: shortName,
longName: longName,
paramIdent: paramIdent,
description: description,
value: value,
parse: T.from)
}
}
///
/// A `RepeatedArgument` encapsulates a sequence of parameter values of type `T`. This
/// class is most commonly used in command-line applications for handling arguments with
/// potentially multiple parameter values where the parameters extracted from the command-line
/// are stored in the flag object itself.
///
public final class RepeatedArgument<T>: Argument {
private let maxCount: Int
public var value: [T]
/// Initializes a repeated argument from the given short name, long name, and description.
/// `maxCount` determines how many parameters are acceptable. `parse` is used to parse strings
/// into values of type `T`.
public init(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
maxCount: Int = Int.max,
parse: @escaping (String) -> T?) {
self.maxCount = maxCount
self.value = []
super.init(shortName: shortName,
longName: longName,
paramIdent: paramIdent,
description: description,
repeated: true,
handler: { x in })
self.setHandler(parse: parse, set: { [unowned self] value in
guard self.value.count < self.maxCount else {
throw FlagError(.tooManyValues(String(describing: self.value)), self)
}
self.value.append(value)
})
}
}
extension RepeatedArgument where T: ConvertibleFromString {
/// Initializes a repeated argument from the given short name, long name, and description.
/// `maxCount` determines how many parameters are acceptable. The default parsing function
/// is used.
public convenience init(shortName: Character?,
longName: String?,
paramIdent: String? = nil,
description: String,
maxCount: Int = Int.max) {
self.init(shortName: shortName,
longName: longName,
paramIdent: paramIdent,
description: description,
maxCount: maxCount,
parse: T.from)
}
}