Swift CLI with ArgumentParser: Creating Powerful Console Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CLI Framework for Swift (iOS/macOS): ArgumentParser



In the world of development for the Apple ecosystem, there is often a need to create not only graphical applications but also powerful console utilities. Whether it's build automation, data processing, or creating auxiliary tools for iOS/macOS — a high-quality CLI (Command Line Interface) is essential. For these purposes, Apple offers the official ArgumentParser framework, which is part of the Swift Package Manager. It is a modern, type-safe, and declarative tool that allows you to create console applications in Swift with minimal effort.



Unlike simple argument parsing via CommandLine.arguments, ArgumentParser provides a structured approach: you describe commands, flags, and options as properties of your structures, and the framework automatically generates help, handles errors, and validates input. Let's figure out how to use this framework for macOS and iOS (via Swift Package Manager).



Installation and Setup



ArgumentParser does not require a separate installation — it is available as part of the Swift standard library (starting with Swift 5.5). To use it in your project, add the dependency in Package.swift:



// swift-tools-version:5.7import PackageDescription

let package = Package( name: "MyCLI", dependencies: [ .package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0") ], targets: [ .executableTarget( name: "MyCLI", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser") ] ) ])


For iOS projects, the framework can be added via Swift Package Manager in Xcode by specifying the same repository. However, it's worth remembering that CLI applications are usually created for macOS, but the library can also be used in iOS applications for internal string parsing.



Core Concepts



ArgumentParser is built around three key entities:



  • Commands (ParsableCommand) — the main element representing a single CLI command. Each command is a structure that implements the ParsableCommand protocol.
  • Arguments (@Argument) — positional parameters that are required for input.
  • Options (@Option) — named parameters with long (--name) and short (-n) flags.
  • Flags (@Flag) — boolean switches (--verbose, --quiet).


Example: Creating a Simple CLI Calculator



Let's create a console application for macOS that performs basic arithmetic operations:



import ArgumentParser

struct Calculator: ParsableCommand { static let configuration = CommandConfiguration( commandName: "calc", abstract: "Simple command-line calculator", discussion: "Supports addition, subtraction, multiplication, and division." )

@Argument(help: "First number") var firstNumber: Double

@Argument(help: "Second number") var secondNumber: Double

@Option(name: [.short, .long], help: "Operation: add, sub, mul, div") var operation: String = "add"

@Flag(name: [.short, .long], help: "Verbose output") var verbose: Bool = false

mutating func run() throws { let result: Double switch operation { case "add": result = firstNumber + secondNumber case "sub": result = firstNumber - secondNumber case "mul": result = firstNumber * secondNumber case "div": guard secondNumber != 0 else { throw ValidationError("Division by zero!") } result = firstNumber / s

Recommendations