swift-argument-parser
Straightforward, type-safe argument parsing for Swift
Swift Argument Parser is a library made by Apple that helps developers build command-line tools in the Swift programming language. When you write a program that runs in a terminal and accepts options or arguments from the user, you need code to read and interpret whatever the user typed. This library handles that work for you in a way that takes advantage of Swift's type system.
The way you use it is by describing your command-line interface as a data structure. You define a type with properties for each piece of input you expect, annotate those properties with labels that indicate whether each one is a flag, an option with a value, or a positional argument, and the library takes care of reading from the terminal, converting values to the right types, and generating useful error messages if something is missing or invalid. Help text is built automatically from the descriptions you provide.
The library also supports organizing a tool into subcommands. If you are building something with multiple modes, like a tool that can both add and remove things, you can model each mode as its own separate type and nest them together.
Apple uses this library in its own projects, including the Swift package manager and the swift-format tool. The library follows a stable versioning policy, meaning that breaking changes to its public API can only happen in a new major version. Adding it to a Swift project is done through Swift Package Manager by listing it as a dependency in the package configuration file.
The README includes several working examples of increasing complexity, from a simple script to a tool with nested subcommands, which makes it easy to understand how the pieces fit together.