Swift.org Blog Nate Cook Feb 27, 2020

Argument Parser Library for Swift

Article Summary

Nate Cook from Apple's Swift team just made building command-line tools in Swift genuinely enjoyable. The new ArgumentParser library turns 50 lines of code into a fully-featured CLI with subcommands, validation, and auto-generated help screens.

Apple open-sourced ArgumentParser, a Swift library that eliminates the boilerplate of parsing command-line arguments. Using property wrappers and Swift's type system, it automatically generates help text, validates inputs, and handles complex command structures. The Swift project is already adopting it across their toolchain.

Key Takeaways

Critical Insight

ArgumentParser uses Swift's type system to build command-line interfaces declaratively, cutting boilerplate while adding automatic validation and help generation.

The library's design reveals how Swift's newer features (property wrappers, reflection) can eliminate entire categories of repetitive code.

About This Article

Problem

Swift's internal argument parser in TSCUtility was built for SwiftPM's specific needs, but it wasn't designed to work across the Swift project's various command-line tools.

Solution

Nate Cook and the Swift team pulled out the parser and made it more general, creating ArgumentParser. It uses property wrappers and Swift's reflection to let developers build CLI interfaces just by declaring types.

Impact

The Swift project will use ArgumentParser in SwiftPM, indexstore-db, swift-format, and the Swift compiler driver rewrite. This gives the entire toolchain consistent argument parsing.