Lets build a Command line app in Swift

Sandeep Joshi
Jan 11, 2018 · 6 min read

With Swift being open source and server side swift gaining more and more traction, command line apps built with swift can be extemely powerful for server side applications. There have been some libraries which helps to build useful command line apps and today we’ll explore few libraries and build a simple command line app.

We’ll build a command line tool to group files of a particular type and move them into a sub directory. For example, taking all png files in a folder and moving them to a sub directory within that folder. We’ll explore three packages from awesomeiOS list on GitHub.

Setting things up…

  1. Make sure you have the latest version of Swift(4) installed and you can check it using the command swift --version as shown below.

2. Now we need to setup a basic Package structure using the command swift package init --type executable. Note that the command will create a package with the same name as the present working directory. As you can see in Fig 1, the command creates a directory structure.

Fig 1. Creating a new Package

One of the file created is Package.swift which contains 3 important aspects of the package

  • Products: The executables and libraries which are the end products of the package
  • Dependencies: All the other libraries which we’ll use in this package
  • Targets: These are basic blocks of the package like test suite or the main module, and can be interdependent. Like the test suite refers to the main source module.

Moving on to the source file, Swift has a simple hello world boilerplate code in the source file. A look at the main.swift is as below

We can now build the package and make sure there are no issues. swift build command will compile all the modules and handle the dependencies in the Package.swift file.

Now running the executable FileOrganizer in the debug directory should print Hello World

Now that we have basic package setup, lets use some libraries and build the tool.

Adding dependencies

For this project we’ll use three command line libraries/packages from awesomeiOS list on GitHub. To add a package, we need to edit the manifest file Package.swift as in Fig 2 and then swift build command will fetch, clone and resolve the added packages as shown in Fig 3.

Edit the Package.swift file like the one below

Package.swift file
Fig 2. Adding packages to manifest file
Fig 3. Downloading all the packages

Note: Adding a package in Package.swift for older swift versions has a slightly different syntax and this will not work.

Building the tool

The package CommandLineKit is used as a base package to create the command with an option to get input as a string. The following code takes a directory path as an input for the specified option

Lets build and run this code and you should get an output similar to the one below.

So the CommandLineKit package gives a class Commandline which is analogous to any bash command and an instance of StringOption is analogous to an option for a typical command. So we instantiate string option(s) and add it to the option list of the commandline instance.

Now lets find all the available file types in that directory. We’ll use FileManager to get fileUrls of all the files in the directory and get the extensions of the files. Finally an array is created to store the unique file types/extensions. The following code does just that. Remove the print statement from the code above and paste the following code.

Upon building and running the code now should produce an output similar to the one below —

Now using the SwiftLine package, we can ask the user which type of files should be grouped and moved into a subfolder. ask is a method provided by SwiftLine which presents a prompt and accepts the entered string.

Again using another method agree of SwiftLine, we can confirm if the user really wants to group files of the entered type.

Now we just check the user’s choice and ask user for the name of the subdirectory and move the files of that type to the newly created subdirectory. Consolidating the above two code snippets, below is the code which needs to be added after printing the file types/extensions.

The complete main.swift looks like this —

Upon building and running this, the output should look something like this..

Note: As you can see, the prompts and output is styled. I have used the ColorizeSwift package to style the strings which are pretty straightforward and as simple as calling a method on a string. Check out the resources below for more information on this.

Obviously there can be a ton of improvements in this tool. This should serve as a good starting point to build command line apps in Swift.

Resources:

Please click 👏 button below a few times to show your support! ⬇⬇ Thanks! Don’t forget to follow Quick Code below.

Find out Free courses on Quick Code for various programming languages. Get new updates on Messenger.

Quick Code

Find the best tutorials and courses for the web, mobile…

Sign up for Developer Updates

By Quick Code

Receive weekly updates about new posts on programming, development, data science, web development and more Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Sandeep Joshi

Written by

CS grad student and iOS dev

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store