While there are many scripting languages available to create command line tools, creating binary executables has many benefits. Specially when you can write them in Swift.
These are the minimum steps to create a new command line tool and run it:
$ mkdir mycommandlinetool
$ cd mycommandlinetool
$ swift package init --type executable
$ swift run
Hello world!
To generate an Xcode project, you can run:
$ swift package generate-xcodeproj
generated: ./mycommandlinetool.xcodeproj
Keep this command handy, as you will need to regenerate the Xcode project when adding new dependencies to the Package.swift
file.
Command Line Tools Series
- Creating command line tools with Swift Package Manager
- Manual argument parsing
- Parsing command line arguments with SPM ArgumentParser
- Handling commands with ArgumentParser
- Internet requests
- What else can you do with a command line tool?