Command Line Tool using Swift Package Manager and Utility package

Dmytro Anokhin
Oct 5, 2018 · 2 min read

Recently I had to build a Command Line Tool second time in one month, so I decided to document the setup I use.

The simplest way to create a Command Line Tool is to use Xcode template. But it lacks a crucial part — argument parsing. Swift Package Manager contains Utility package that has exactly what we need.

Without further do, here are the steps:

1. Create and navigate to a directory for the command line tool

mkdir commandlinetool
cd commandlinetool

By default, the name of the directory will be the name of Xcode target and the tool.

2. Create Swift package with type executable

swift package init --type executable

This will create initial files and folders including hidden .gitignore and .build/.

3. Add dependencies in Package.swift

open Package.swift

We need to add "https://github.com/apple/swift-package-manager.git" to the package dependencies and "Utility" to the target.

The latest release can be found here: https://github.com/apple/swift-package-manager/releases/latest

4. Generate Xcode project

swift package generate-xcodeproj

Xcode project is generated and excluded from git by default.

You can also use your xcconfig file.

swift package generate-xcodeproj --xcconfig-overrides Config.xcconfig

Here is an example with the latest macOS deployment target and Swift version.

Enjoy

That’s pretty much it. Now you can start adding code in main.swift

I did not found official documentation for ArgumentParser. But it is open source and not hard to start using it.


Hope you find this useful. If you like the article, please follow me and share.

Show authors more ❤️ with 👏’s

And hope to see you next time 😊

Dmytro Anokhin

Written by

iOS Developer, here to share best practices learned through my experience. You can find me on Twitter: https://twitter.com/dmytroanokhin

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade