Getting Started With Multi File Command Line Swift
Filed under: swift
I wanted to write a post on commandline swift projects that span more than one file. This post covers a hello world type example that spans three files and prompts for input. Also covered are a file wrapper for multi file swift scripts and compiled command line executables.
Also see the post on Getting Started With Command Line Swift.
Software Versions
$ date
February 9, 2016 at 02:08:36 AM JST
$ uname -mv
Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
$ swift --version # swiftc is the same
Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
$ xcrun --version
xcrun version 28.Instructions
First, we will create a multi file project.
Add the following to main.swift.
func main() {
let name = inputPrompt("What is your name? ")
sayHello(name)
}
main()Add the following to input_prompt.swift.
import Foundation
func inputPrompt(message: String = "> ") -> String {
print(message, terminator: "")
fflush(__stdoutp)
let keyboard = NSFileHandle.fileHandleWithStandardInput()
let inputData = keyboard.availableData
let result = NSString(data: inputData, encoding: NSUTF8StringEncoding) as! String
return result.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet()
)
}Add the following to hello.swift.
func sayHello(name: String) {
print("Hello, " + name + "!")
}To execute main.swift and the support files, create a wrapper script. Add the following to hello_user.sh.
#!/bin/sh
TMPFILE=`mktemp /tmp/Project.swift.XXXXXX` || exit 1
trap "rm -f $TMPFILE" EXIT
cat *.swift > $TMPFILE
swift $TMPFILEMake the wrapper executable.
chmod +x hello_user.shThe wrapper script can be run from the command line.
./hello_user.shAlternatively, compile the files into an executable with swiftc.
swiftc file_a.swift file_b.swift file_c.swift -o executable_nameOne of the files in this project imports foundation, so xcrun -sdk macosx swiftc needs to be used instead of swiftc.
xcrun -sdk macosx swiftc main.swift input_prompt.swift hello.swift -o hello_userThe executable can be run from the command line.
./hello_userReferences
Primary Sources, Non-Peer-Reviewed
- “Minimal Swift Command Line Hello World.” n.d. Gist. Accessed October 25, 2026. https://gist.github.com/kavu/79f05be2383e97843867
- “Swift, Functions.” n.d. developer.apple.com. Accessed October 25, 2026. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html
General Sources
- “Command Line: Is There a Way to Have a Swift Script Use Multiple Files.” n.d. Accessed October 25, 2026. http://stackoverflow.com/questions/28069043/is-there-a-way-to-have-a-swift-script-use-multiple-files
- “How to Include .Swift File from Other .Swift File in an Immediate Mode?” n.d. Accessed October 25, 2026. http://stackoverflow.com/questions/25342940/how-to-include-swift-file-from-other-swift-file-in-an-immediate-mode
- “Ios: ‘Use of Unresolved Identifier’ in Swift.” n.d. Accessed October 25, 2026. http://stackoverflow.com/questions/28996730/use-of-unresolved-identifier-in-swift
- “Objective c: Update Current Line with Command Line Tool in Swift.” n.d. Accessed October 25, 2026. http://stackoverflow.com/questions/25483292/update-current-line-with-command-line-tool-in-swift
- “Osx: Input from the Keyboard in Command Line Application.” n.d. Accessed October 25, 2026. http://stackoverflow.com/questions/24004776/input-from-the-keyboard-in-command-line-application
- “Scripting in Swift Is Pretty Awesome.” n.d. KrakenDev. Accessed October 25, 2026. http://krakendev.io/blog/scripting-in-swift
- “Swift for CLI Tools.” n.d. Speaker Deck. Accessed October 25, 2026. https://speakerdeck.com/supermarin/swift-for-cli-tools
- “Swift Scripts: How to Write Small Command Line Scripts in Swift.” 2014. practicalswift.com, June 7. http://practicalswift.com/2014/06/07/swift-scripts-how-to-write-small-command-line-scripts-in-swift/
- “Swift, Does swift has trim method on String?” n.d. Accessed October 25, 2026. http://stackoverflow.com/questions/26797739/does-swift-has-trim-method-on-string
- “Swift, print without newline in swift 2.0.” n.d. Accessed October 25, 2026. http://stackoverflow.com/questions/30865233/print-without-newline-in-swift-2-0
Related Posts
- Sechter, Brendan. 2016. “Getting Started With Command Line Swift.” Brendan A R Sechter’s Development Blog, January 18. https://sgeos.github.io/swift/2016/01/18/getting-started-with-command-line-swift.html