How to make a command line tool in Xcode
Tags: swift, xcode, macos
Published: 2018-09-01 18:37:14

---
title: How to make a command line tool in Xcode
published: true
description: Find out how to build a MacOS command line tool with XCode and Swift
tags: Swift, Xcode, MacOS
---

MacOS command line tools can be handy for automating tasks and they're also a great way to have more fun with Swift, so here's how to set one up:

**Creating the project**

1) Create a new Xcode project, select MacOS and command line

![Create an Xcode project](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Picture1-300x216.png)

2) Give your app a name, and make sure Swift is selected as the Language

![Name the app](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-15.59.05-300x216.png)

3) Click Next and choose where to save your project.

You now have an Xcode command line project! The <code>main.swift</code> file is         where Xcode will look to run your code, similar to the main function in Java or C# or the AppDelegate in iOS. There will be a <code>“Hello World”</code> print statement there already.

Under Products there is an executable file called <code>CommandLineTool</code> which is red. This is the executable file created when we build the project. from the command line. It’s red because we haven’t built it yet

![Folder in Finder](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-16.00.10-300x159.png)

4) Build and run the project. Th<span style="font-size: 1rem;">e CommandLineTool product will now change to black and the following output should display in the output window</span>

![Output window](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Picture3-300x97.png)

&nbsp;

**Running from the Command Line**

1) In File Inspector (⌥⌘1), check where the CommandLineTool Product is being saved. It will be a long location ending in <code>/Build/Products/Debug/CommandLineTool</code>

2) Navigate to this <code>Debug</code> folder and run theCommandLineTool by typing in <code>./CommandLineTool</code>

```shell
$ ./CommandLineTool
Hello, World!
```

3) Try going into Xcode and changing the program slightly, for example:


{% gist https://gist.github.com/Ceri-anne/45f77ae09dec199fb7dd52a98fb90a9e %}


4) Build in Xcode (don’t miss this step out otherwise the executable won't update). There’s no need to run in Xcode - just build ( ⌘ B ).

5) Go back to terminal and re-run to see if it works:

```shell
$ ./CommandLineTool
Hello, World! This is my command line tool
```

This location isn't very convenient to to run our tool from so we can move it to somewhere better. 

6) In either Finder or Terminal, navigate to the CommandLineTool folder and create a folder called bin. Your CommandLineTool folder should now look like this:

![Folder](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-16.04.47-300x101.png)

7) In Xcode, go to the CommandLineTool target and Build Phase and add a new Run Script Phase

![Run Script Phase](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-16.06.00-300x82.png)

8) Add the following script <code>cp ${BUILT_PRODUCTS_DIR}/CommandLineTool ${PROJECT_DIR}/bin</code>

![Script Code](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-16.06.56-300x91.png)

This will copy the executable when we build our project and paste it in our bin folder.

9) Build the project and you should see the executable in your bin directory

![Executable in bin](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-16.07.33-300x121.png)

10) Navigate to the project folder in terminal and you can run your CommandLineTool by typing in bin/CommandLineTool

```shell
$ bin/CommandLineTool
Hello, World!
```

**Adding arguments**

**Command Line**

1) We can pass arguments in to our program when we run it from the command line. We just need to add a space and then the arguments. Let's pass in a name:

```shell
$ bin/CommandLineTool Ceri
Hello, World! This is my command line tool
```

We're not doing anything with this argument so nothing different happens.

2) Update <code>main.swift</code> to print out any arguments we pass in:

{% gist https://gist.github.com/Ceri-anne/771bac64a2c7ab96179b6da6be783618 %}

3) Build and run from the command line, passing in arguments separated by spaces

```shell

$ bin/CommandLineTool Hello 23
["bin/CommandLineTool", "Hello", "23"]
Hello, World! This is my command line tool
```

Note that the first argument is the program name, and the arguments we passed in are 2nd and 3rd. Also, all arguments are strings.

4) Amend the code to this:

{% gist https://gist.github.com/Ceri-anne/4959356c5ac0c7c0a463b0de6fc3521c %}


5) Run again and you'll see each argument printed out

```shell
$ bin/CommandLineTool Hello 23
Hello
23
Hello, World! This is my command line tool
```

**Xcode**

1) To add an argument in Xcode, click on Edit Scheme

![Edit Scheme](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-15.35.22-300x171.png)

2) In Run/Arguments add an argument to <code>Arguments Passed On Launch</code> and click <code>Close</code>

![Arguments](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-16.14.39-300x168.png)

3) Now when you run the project in Xcode you'll see the arguments printed out in the Output window

![Output window](http://ceri-anne.co.uk/wp-content/uploads/2018/08/Screen-Shot-2018-08-31-at-16.15.21-300x143.png)

And that's it! Now you can develop as much Swift code as you like as normal. Just kick it all off in main.swift. Happy Swifting :)

The code for this project is on Github: https://github.com/Ceri-anne/CommandLineTool

*This article was first published on my blog here:* 
https://ceri-anne.co.uk/how-to-make-a-command-line-macos-tool-in-xcode