Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit ad3b12d

Browse files
authored
Use #file to get Swift script location. (#20)
Using `CommandLine.arguments[0]` to get the Swift script location is unrobust. `arguments[0]` differs when running `swift` directly vs. invoking `swiftc` then running the compiled binary.
1 parent 40c239b commit ad3b12d

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

Autoencoder/Autoencoder.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ let outputFolder = "/tmp/mnist-test/"
2121

2222
func readDataset() -> (images: Tensor<Float>, labels: Tensor<Int32>)? {
2323
print("Reading the data.")
24-
guard let swiftFile = CommandLine.arguments.first else { return nil }
25-
let swiftFileURL = URL(fileURLWithPath: swiftFile)
24+
let swiftFileURL = URL(fileURLWithPath: #file)
2625
var imageFolderURL = swiftFileURL.deletingLastPathComponent()
2726
var labelFolderURL = swiftFileURL.deletingLastPathComponent()
2827
imageFolderURL.appendPathComponent("Resources/train-images-idx3-ubyte")

MNIST/MNIST.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ struct MNISTParameters : ParameterAggregate {
4141

4242
/// Train a MNIST classifier for the specified number of iterations.
4343
func train(_ parameters: inout MNISTParameters, iterationCount: Int) {
44-
// Get script directory. This is necessary for MNIST.swift to work when
45-
// invoked from any directory.
46-
let currentDirectory = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
47-
let currentScriptPath = URL(fileURLWithPath: CommandLine.arguments[0],
48-
relativeTo: currentDirectory)
44+
// Get script path. This is necessary for MNIST.swift to work when invoked
45+
// from any directory.
46+
let currentScriptPath = URL(fileURLWithPath: #file)
4947
let scriptDirectory = currentScriptPath.deletingLastPathComponent()
5048

5149
// Get training data.

0 commit comments

Comments
 (0)