Creating a Swift CLI Tool

Passionate about: Defensive Programming ⢠Effective Engineering Documentation ⢠Emotion-Centered Design and Microcopy
Recently, I had the interesting experience of writing a CLI (Command Line Interface) tool in Swift. My objective was to build an executable that could be run via a command in the Terminal and perform some operations. Specifically, I wanted the executable to check whether a specified projectâs files were alphabetically sorted.
In this post, Iâll share the basics of how to get up and running with a very simple (think Hello World) CLI tool.
Getting started was easier than I expected.
In Xcode, go to File > New > Project, select the Command Line Tool template, and name your project
In your newly created project, open the main.swift file. You should see a
"Hello, World!"print statement.Try running this simple program in Xcode. In the debug console, you should see
"Hello, World!"
Wait, but since this is supposed to be a CLI tool, shouldnât we be running it from Terminal? Thatâs right, we'll do that next:
In Xcode, go to Product > Show Build Folder in Finder.
In Finder, follow the file path Build > Products > Debug. Inside the Debug folder, you should see your projectâs executable.
Right click the executable and select Get Info.
Copy the file path to the executable for your reference, for example:
/Users/chrchang/Library/Developer/Xcode/DerivedData/CLITest-gnkyrvsvnodazpcfygpunwnqgqkz/Build/Products/DebugIn Terminal, navigate to this Debug folder, for example by running
cd ~/Library/Developer/Xcode/DerivedData/CLITest-gnkyrvsvnodazpcfygpunwnqgqkz/Build/Products/DebugNow that youâre in the Debug folder where the executable is stored, you can run the executable using the dot-slash syntax, for example:
./CLITestYou should see
"Hello, World!"printed in your Terminal.
Note: Whenever you make changes to main.swift, youâll need to build the project before running the executable in Terminal in order for your changes to be applied when executing in Terminal.
Now comes the hard (but fun!) part of figuring out what you actually want to write in your CLI program. If youâre feeling disoriented, try adding this line to the bottom of your main.swift: print(CommandLine.arguments.count)
When you build and run your program, you should see a value of 1 printed to the console, representing a count of one argument. This makes sense, because in order to run the program, you only needed to enter one argument: ./CLITest from step 6 in the instructions above.
Depending on what you want your tool to achieve, youâll likely want the user to enter multiple space-separated arguments as inputs to your program. As a simple example, imagine that we want the user to input their name when running the program. Try adding the following code to the bottom of your program:
if CommandLine.arguments.count > 1 {
print("My name is \(CommandLine.arguments[1])")
}
Build the project in Xcode. Then back in Terminal, run the executable with the same command as before, followed by a space and the second argumentâyour name. For example:./CLITest Christine
You should see something like this:

Hopefully this gives you a basic idea of how to:
Run a CLI tool from Terminal,
Enter user inputs (aka arguments) when running the program
Read those inputs in the program.
Now you're ready to start designing and building a CLI tool of your own!



