-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompression.diff
More file actions
390 lines (383 loc) · 17.3 KB
/
Copy pathcompression.diff
File metadata and controls
390 lines (383 loc) · 17.3 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
--- input/input.swift
+++ output/output.swift
@@ -71,51 +71,14 @@
static func flag(key: InputKey, name: NameSpecification, default initialValue: Bool?, help: ArgumentHelp?) -> ArgumentSet {
// The flag is required if initialValue is `nil`, otherwise it's optional
let helpOptions: ArgumentDefinition.Help.Options = initialValue != nil ? .isOptional : []
- let defaultValueString = initialValue == true ? "true" : nil
-
- let help = ArgumentDefinition.Help(
- allValueStrings: [],
- options: helpOptions,
- help: help,
- defaultValue: defaultValueString,
- key: key,
- isComposite: false)
- let arg = ArgumentDefinition(kind: .name(key: key, specification: name), help: help, completion: .default, update: .nullary({ (origin, name, values) in
- values.set(true, forKey: key, inputOrigin: origin)
- }), initial: { origin, values in
- if let initialValue = initialValue {
- values.set(initialValue, forKey: key, inputOrigin: origin)
- }
- })
+ { … 16 line(s) … ⟦tj:44fef8d9ac46cbe62f407eac91ece81f⟧ }
return ArgumentSet(arg)
- }
static func updateFlag<Value: Equatable>(key: InputKey, value: Value, origin: InputOrigin, values: inout ParsedValues, exclusivity: FlagExclusivity) throws {
let hasUpdated: Bool
if let previous = values.element(forKey: key) {
- hasUpdated = !previous.inputOrigin.elements.isEmpty
- } else {
- hasUpdated = false
+ { … 21 line(s) … ⟦tj:9e0949946576091866cb97f0057a2742⟧ }
}
-
- switch (hasUpdated, exclusivity.base) {
- case (true, .exclusive):
- // This value has already been set.
- if let previous = values.element(forKey: key) {
- if (previous.value as? Value) == value {
- // setting the value again will consume the argument
- values.set(value, forKey: key, inputOrigin: origin)
- }
- else {
- throw ParserError.duplicateExclusiveValues(previous: previous.inputOrigin, duplicate: origin, originalInput: values.originalInput)
- }
- }
- case (true, .chooseFirst):
- values.update(forKey: key, inputOrigin: origin, initial: value, closure: { _ in })
- case (false, _), (_, .chooseLast):
- values.set(value, forKey: key, inputOrigin: origin)
- }
- }
/// Creates an argument set for a pair of inverted Boolean flags.
static func flag(
@@ -126,44 +89,17 @@
inversion: FlagInversion,
exclusivity: FlagExclusivity,
help: ArgumentHelp?) -> ArgumentSet
- {
let helpOptions: ArgumentDefinition.Help.Options = required ? [] : .isOptional
- let (enableNames, disableNames) = inversion.enableDisableNamePair(for: key, name: name)
- let initialValueNames = initialValue.map {
- $0 ? enableNames : disableNames
- }
-
- let enableHelp = ArgumentDefinition.Help(allValueStrings: [], options: helpOptions, help: help, defaultValue: initialValueNames?.first?.synopsisString, key: key, isComposite: true)
- let disableHelp = ArgumentDefinition.Help(allValueStrings: [], options: [.isOptional], help: help, defaultValue: nil, key: key, isComposite: false)
-
- let enableArg = ArgumentDefinition(kind: .named(enableNames), help: enableHelp, completion: .default, update: .nullary({ (origin, name, values) in
- try ArgumentSet.updateFlag(key: key, value: true, origin: origin, values: &values, exclusivity: exclusivity)
- }), initial: { origin, values in
- if let initialValue = initialValue {
- values.set(initialValue, forKey: key, inputOrigin: origin)
- }
- })
- let disableArg = ArgumentDefinition(kind: .named(disableNames), help: disableHelp, completion: .default, update: .nullary({ (origin, name, values) in
- try ArgumentSet.updateFlag(key: key, value: false, origin: origin, values: &values, exclusivity: exclusivity)
- }), initial: { _, _ in })
+ { … 18 line(s) … ⟦tj:e7e0b88e148a406b0bc91f900bdab6cc⟧ }
return ArgumentSet([enableArg, disableArg])
- }
/// Creates an argument set for an incrementing integer flag.
static func counter(key: InputKey, name: NameSpecification, help: ArgumentHelp?) -> ArgumentSet {
let help = ArgumentDefinition.Help(allValueStrings: [], options: [.isOptional, .isRepeating], help: help, defaultValue: nil, key: key, isComposite: false)
let arg = ArgumentDefinition(kind: .name(key: key, specification: name), help: help, completion: .default, update: .nullary({ (origin, name, values) in
guard let a = values.element(forKey: key)?.value, let b = a as? Int else {
- throw ParserError.invalidState
- }
- values.set(b + 1, forKey: key, inputOrigin: origin)
- }), initial: { origin, values in
- values.set(0, forKey: key, inputOrigin: origin)
- })
- return ArgumentSet(arg)
- }
-}
+ { … 9 line(s) … ⟦tj:1c6306bf0877824552769b4ee5b2fbec⟧ }
extension ArgumentSet {
/// Fills the given `ParsedValues` instance with initial values from this
@@ -234,57 +170,12 @@
) throws {
let origin = InputOrigin(elements: [originElement])
switch argument.parsingStrategy {
- case .default:
- // We need a value for this option.
- if let value = parsed.value {
- // This was `--foo=bar` style:
- try update(origin, parsed.name, value, &result)
- usedOrigins.formUnion(origin)
- } else if argument.allowsJoinedValue,
- let (origin2, value) = inputArguments.extractJoinedElement(at: originElement)
- {
- // Found a joined argument
- let origins = origin.inserting(origin2)
- try update(origins, parsed.name, String(value), &result)
- usedOrigins.formUnion(origins)
- } else if let (origin2, value) = inputArguments.popNextElementIfValue(after: originElement) {
- // Use `popNextElementIfValue(after:)` to handle cases where short option
- // labels are combined
- let origins = origin.inserting(origin2)
- try update(origins, parsed.name, value, &result)
- usedOrigins.formUnion(origins)
- } else {
- throw ParserError.missingValueForOption(origin, parsed.name)
- }
-
- case .scanningForValue:
- // We need a value for this option.
- if let value = parsed.value {
- // This was `--foo=bar` style:
- try update(origin, parsed.name, value, &result)
- usedOrigins.formUnion(origin)
+ { … 29 line(s) … ⟦tj:3447b7b9d4b8fcda5c477eae86c2ce1d⟧ }
} else if argument.allowsJoinedValue,
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement) {
// Found a joined argument
let origins = origin.inserting(origin2)
- try update(origins, parsed.name, String(value), &result)
- usedOrigins.formUnion(origins)
- } else if let (origin2, value) = inputArguments.popNextValue(after: originElement) {
- // Use `popNext(after:)` to handle cases where short option
- // labels are combined
- let origins = origin.inserting(origin2)
- try update(origins, parsed.name, value, &result)
- usedOrigins.formUnion(origins)
- } else {
- throw ParserError.missingValueForOption(origin, parsed.name)
- }
-
- case .unconditional:
- // Use an attached value if it exists...
- if let value = parsed.value {
- // This was `--foo=bar` style:
- try update(origin, parsed.name, value, &result)
- usedOrigins.formUnion(origin)
+ { … 18 line(s) … ⟦tj:922e1197fe71d675300aedd6e78f9afb⟧ }
} else if argument.allowsJoinedValue,
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement) {
// Found a joined argument
@@ -295,42 +186,12 @@
guard let (origin2, value) = inputArguments.popNextElementAsValue(after: originElement) else {
throw ParserError.missingValueForOption(origin, parsed.name)
}
- let origins = origin.inserting(origin2)
- try update(origins, parsed.name, value, &result)
- usedOrigins.formUnion(origins)
- }
-
- case .allRemainingInput:
- // Reset initial value with the found input origins:
- try argument.initial(origin, &result)
-
- // Use an attached value if it exists...
- if let value = parsed.value {
- // This was `--foo=bar` style:
- try update(origin, parsed.name, value, &result)
- usedOrigins.formUnion(origin)
+ { … 14 line(s) … ⟦tj:150af35f19050ead2b90ec483faf1a4a⟧ }
} else if argument.allowsJoinedValue,
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement) {
// Found a joined argument
let origins = origin.inserting(origin2)
- try update(origins, parsed.name, String(value), &result)
- usedOrigins.formUnion(origins)
- inputArguments.removeAll(in: usedOrigins)
- }
-
- // ...and then consume the rest of the arguments
- while let (origin2, value) = inputArguments.popNextElementAsValue(after: originElement) {
- let origins = origin.inserting(origin2)
- try update(origins, parsed.name, value, &result)
- usedOrigins.formUnion(origins)
- }
-
- case .upToNextOption:
- // Use an attached value if it exists...
- if let value = parsed.value {
- // This was `--foo=bar` style:
- try update(origin, parsed.name, value, &result)
- usedOrigins.formUnion(origin)
+ { … 18 line(s) … ⟦tj:9b3633e5e5628df62eddec9016aaf54b⟧ }
} else if argument.allowsJoinedValue,
let (origin2, value) = inputArguments.extractJoinedElement(at: originElement) {
// Found a joined argument
@@ -372,55 +233,13 @@
) throws {
var endOfInput = unusedInput.elements.endIndex
- // If this argument set includes a definition that should collect all the
- // post-terminator inputs, capture them before trying to fill other
- // `@Argument` definitions.
- if let postTerminatorArg = argumentSet.first(where: { def in
- def.isRepeatingPositional && def.parsingStrategy == .postTerminator
- }),
- case let .unary(update) = postTerminatorArg.update,
- let terminatorIndex = unusedInput.elements.firstIndex(where: \.isTerminator)
- {
- for input in unusedInput.elements[(terminatorIndex + 1)...] {
- // Everything post-terminator is a value, force-unwrapping here is safe:
- let value = input.value.valueString!
- try update([.argumentIndex(input.index)], nil, value, &result)
- }
-
- endOfInput = terminatorIndex
- }
-
- // Create a stack out of the remaining unused inputs that aren't "partial"
- // arguments (i.e. the individual components of a `-vix` grouped short
- // option input).
- var argumentStack = unusedInput.elements[..<endOfInput].filter {
- $0.index.subIndex == .complete
- }[...]
- guard !argumentStack.isEmpty else { return }
-
- /// Pops arguments off the stack until the next valid value. Skips over
+ { … 27 line(s) … ⟦tj:95ee305c7ee5f3ae0ae6e059081b9e87⟧ }
/// dash-prefixed inputs unless `unconditional` is `true`.
func next(unconditional: Bool) -> SplitArguments.Element? {
while let arg = argumentStack.popFirst() {
if arg.isValue || unconditional {
- return arg
- }
- }
-
- return nil
- }
-
- // For all positional arguments, consume one or more inputs.
- var usedOrigins = InputOrigin()
- ArgumentLoop:
- for argumentDefinition in argumentSet {
- guard case .positional = argumentDefinition.kind else { continue }
- switch argumentDefinition.parsingStrategy {
- case .default, .allRemainingInput:
- break
- default:
+ { … 16 line(s) … ⟦tj:6785c755abc427832175b54103619012⟧ }
continue ArgumentLoop
- }
guard case let .unary(update) = argumentDefinition.update else {
preconditionFailure("Shouldn't see a nullary positional argument.")
}
@@ -430,117 +249,14 @@
guard let arg = next(unconditional: allowOptionsAsInput) else {
break ArgumentLoop
}
- let origin: InputOrigin.Element = .argumentIndex(arg.index)
- let value = unusedInput.originalInput(at: origin)!
- try update([origin], nil, value, &result)
- usedOrigins.insert(origin)
- } while argumentDefinition.isRepeatingPositional
+ { … 21 line(s) … ⟦tj:462ea207eb0a00daf635fcd760c93567⟧ }
}
-
- // If there's an `.allUnrecognized` argument array, collect leftover args.
- if let allUnrecognizedArg = argumentSet.first(where: { def in
- def.isRepeatingPositional && def.parsingStrategy == .allUnrecognized
- }),
- case let .unary(update) = allUnrecognizedArg.update
- {
- result.capturedUnrecognizedArguments = SplitArguments(
- _elements: Array(argumentStack),
- originalInput: [])
- while let arg = argumentStack.popFirst() {
- let origin: InputOrigin.Element = .argumentIndex(arg.index)
- let value = unusedInput.originalInput(at: origin)!
- try update([origin], nil, value, &result)
- }
- }
- }
mutating func parse() throws -> ParsedValues {
let originalInput = inputArguments
defer { inputArguments = originalInput }
-
- // If this argument set includes a positional argument that unconditionally
- // captures all remaining input, we use a different behavior, where we
- // shortcut out at the first sign of a positional argument or unrecognized
- // option/flag label.
- let capturesForPassthrough = defaultCapturesForPassthrough || argumentSet.contains(where: { arg in
- arg.isRepeatingPositional && arg.parsingStrategy == .allRemainingInput
- })
-
- var result = ParsedValues(elements: [:], originalInput: inputArguments.originalInput)
- var allUsedOrigins = InputOrigin()
-
- try argumentSet.setInitialValues(into: &result)
-
- // Loop over all arguments:
- ArgumentLoop:
- while let (origin, next) = inputArguments.popNext() {
- var usedOrigins = InputOrigin()
- defer {
- inputArguments.removeAll(in: usedOrigins)
- allUsedOrigins.formUnion(usedOrigins)
- }
-
- switch next.value {
- case .value(let argument):
- // Special handling for matching subcommand names. We generally want
- // parsing to skip over unrecognized input, but if the current
- // command or the matched subcommand captures all remaining input,
- // then we want to break out of parsing at this point.
- if let matchedSubcommand = subcommands.first(where: { $0._commandName == argument }) {
- if !matchedSubcommand.includesPassthroughArguments && defaultCapturesForPassthrough {
- continue ArgumentLoop
- } else if matchedSubcommand.includesPassthroughArguments {
- break ArgumentLoop
- }
- }
-
- // If we're capturing all, the first positional value represents the
- // start of positional input.
- if capturesForPassthrough { break ArgumentLoop }
- // We'll parse positional values later.
- break
- case let .option(parsed):
- // Look for an argument that matches this `--option` or `-o`-style
- // input. If we can't find one, just move on to the next input. We
- // defer catching leftover arguments until we've fully extracted all
- // the information for the selected command.
- guard let argument = argumentSet.first(matching: parsed) else
- {
- // If we're capturing all, an unrecognized option/flag is the start
- // of positional input. However, the first time we see an option
- // pack (like `-fi`) it looks like a long name with a single-dash
- // prefix, which may not match an argument even if its subcomponents
- // will match.
- if capturesForPassthrough && parsed.subarguments.isEmpty { break ArgumentLoop }
-
- // Otherwise, continue parsing. This option/flag may get picked up
- // by a child command.
- continue
- }
-
- switch argument.update {
- case let .nullary(update):
- // We don’t expect a value for this option.
- guard parsed.value == nil else {
- throw ParserError.unexpectedValueForOption(origin, parsed.name, parsed.value!)
- }
- _ = try update([origin], parsed.name, &result)
- usedOrigins.insert(origin)
- case let .unary(update):
- try parseValue(argument, parsed, origin, update, &result, &usedOrigins)
- }
- case .terminator:
- // Ignore the terminator, it might get picked up as a positional value later.
- break
- }
- }
-
- // We have parsed all non-positional values at this point.
- // Next: parse / consume the positional values.
- var unusedArguments = originalInput
- unusedArguments.removeAll(in: allUsedOrigins)
- try parsePositionalValues(from: unusedArguments, into: &result)
-
- return result
+ { … 85 line(s) … ⟦tj:9ef90e1d76b3637a4c65b89126975e70⟧ }
}
-}
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (21727 bytes): call tinyjuice_retrieve with token "eb7a0b429ceb76710ec8596d16de2c48"]
\ No newline at end of file