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
- Property wrappers (@Argument, @Option) automatically parse and validate CLI inputs
- Type-safe parsing: define Int property, get automatic validation with zero casting
- Subcommands work like Git: nest types, list in config, done
- Auto-generates help screens and error messages from your type declarations
- Full CLI tool in under 50 lines including validation and documentation
ArgumentParser uses Swift's type system to build command-line interfaces declaratively, cutting boilerplate while adding automatic validation and help generation.
About This Article
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.
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.
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.