I’d like to get general consensus on whether or not the community believes a command line argument parser should be available in the standard library. If we gain consensus that we should move forward I will start working on the implementation and API details for the Swift Evolution proposal.
Command Line Argument Parsing
Swift is being embraced for building command line applications and scripting. The user experience of these types of applications expose configuration through the use of flags and subcommands. Currently to extract this configuration, a developer must either write their own solution that consumes
CommandLine.arguments or rely on a third-party dependency which must be integrated into the project.
We have many first-party and third-party implementations in the Swift community of argument parsing to support these configurations and this pitch is advocating for a default implementation in the Swift standard library since this functionality has become essential to build these types of applications and is applicable for any application invoked from a terminal.
There is a precedent for argument parsing logic in the standard library of other languages:
- Go’s flag package implements command-line flag parsing.
- Python exposes the argparse library which provides much of the necessary functionality around argument parsing, displaying errors and deriving help documentation.
- OCaml’s Arg module provides a general mechanism for extracting options and arguments from the command line.
Previous discussions on the subject:
There are a lot of things that can make the experience of building command line applications better in Swift and I believe an argument parser in the standard library is a good start. A builtin argument parser will make scripting and command line applications easier to write, improve portability and user experience, and lastly make these use-cases first-class citizens for Swift development.
Luckily the Swift community has produced many different solutions to this problem which I believe we can use as inspiration to design a great solution. Below is a (very incomplete) list of popular argument parsing libraries:
Our design would provide an API to satisfy these requirements:
- Declaring / Parsing / Validating flags
- Declaring / Parsing / Validating subcommands
- Generate “help” / “usage” documentation
Nice to have:
- Shell completion utilities
- External configuration for declaring command line interface (YAML, JSON, etc)
This proposal does not impact source compatibility.
Effect on ABI stability
This proposal does not impact ABI stability.
Effect on API resilience