Skip to content

Commit 4485c84

Browse files
authored
Merge pull request #804 from allevato/allow-stdin
Allow `swift-parser-test` to take input from stdin.
2 parents 0522c83 + eb6f64c commit 4485c84

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Sources/swift-parser-test/swift-parser-test.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ private func withTemporaryFile<T>(contents: [UInt8], body: (URL) throws -> T) th
3535
return try body(tempFileURL)
3636
}
3737

38-
private func getContentsOfSourceFile(at path: String) throws -> [UInt8] {
39-
let sourceURL = URL(fileURLWithPath: path)
40-
let source = try Data(contentsOf: sourceURL)
38+
private func getContentsOfSourceFile(at path: String?) throws -> [UInt8] {
39+
let source: Data
40+
if let path = path {
41+
let sourceURL = URL(fileURLWithPath: path)
42+
source = try Data(contentsOf: sourceURL)
43+
} else {
44+
source = FileHandle.standardInput.readDataToEndOfFile()
45+
}
4146
return [UInt8](source)
4247
}
4348

@@ -54,14 +59,14 @@ class SwiftParserTest: ParsableCommand {
5459
class VerifyRoundTrip: ParsableCommand {
5560
required init() {}
5661

57-
init(sourceFile: String, swiftVersion: String?, enableBareSlashRegex: Bool?) {
62+
init(sourceFile: String?, swiftVersion: String?, enableBareSlashRegex: Bool?) {
5863
self.sourceFile = sourceFile
5964
self.swiftVersion = swiftVersion
6065
self.enableBareSlashRegex = enableBareSlashRegex
6166
}
6267

63-
@Argument(help: "The source file that should be parsed")
64-
var sourceFile: String
68+
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
69+
var sourceFile: String?
6570

6671
@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
6772
var swiftVersion: String?
@@ -109,8 +114,8 @@ class VerifyRoundTrip: ParsableCommand {
109114
class PrintDiags: ParsableCommand {
110115
required init() {}
111116

112-
@Argument(help: "The source file that should be parsed")
113-
var sourceFile: String
117+
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
118+
var sourceFile: String?
114119

115120
@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
116121
var swiftVersion: String?
@@ -141,8 +146,8 @@ class PrintDiags: ParsableCommand {
141146
class DumpTree: ParsableCommand {
142147
required init() {}
143148

144-
@Argument(help: "The source file that should be parsed")
145-
var sourceFile: String
149+
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
150+
var sourceFile: String?
146151

147152
@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
148153
var swiftVersion: String?
@@ -167,8 +172,8 @@ class DumpTree: ParsableCommand {
167172
class Reduce: ParsableCommand {
168173
required init() {}
169174

170-
@Argument(help: "The test case that should be reduced")
171-
var sourceFile: String
175+
@Argument(help: "The test case that should be reduced; if omitted, use stdin")
176+
var sourceFile: String?
172177

173178
@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
174179
var swiftVersion: String?

0 commit comments

Comments
 (0)