Friday, February 28, 2020 [Tweets] [Favorites]

Swift Argument Parser

Nate Cook:

We’re delighted to announce ArgumentParser, a new open-source library that makes it straightforward — even enjoyable! — to parse command-line arguments in Swift.

[…]

In addition to what we’ve seen so far, ArgumentParser supports --flag arguments for Boolean or enumerable properties, multiple names for options and flags, encapsulating groups of arguments, and much more. You can learn more by visiting the repository’s README, browsing the guides in the documentation folder, and reading the in-source symbol documentation.

You can also explore the Swift project’s in-flight adoption of ArgumentParser:

  • indexstore-db is a simple utility with two commands.
  • swift-format uses some advanced features, like custom option values and hidden flags.

This looks both full featured and easy to use. It’s also interesting to see how it was implemented using property wrappers (to associate metadata with the instance variables that represent each option) and reflection (so that it can get the name of the instance variable from its declaration, without your having to retype it).

Hopefully this will eventually be built into the standard library so that single-file scripts can use it without needing to manage any dependencies.

See also: getopt_long, argparse.

Previously:

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment