Swiftpack.co - Package - Zehua-Chen/swift-argparse

Swift Argument Parser


  • ☑ Nested commands
  • ☑ Type checking for options and parameters
  • ☑ Help information

Get Started

The package requires Swift 5 or above to build

  1. Add the project to your swift package manifest
// dependencies section
.package(url: "https://github.com/Zehua-Chen/swift-argparse.git", from: "2.0.0")
// target section
.target(name: "example", dependencies: ["SwiftArgParse"]
  1. Add the following line to your application
import SwiftArgParse

struct Calculator: Command {
    func setup(with config: Configuration) {
        config.use(Parameter(type: Double.self))
        config.use(Parameter(type: Double.self))

    func run(with context: CommandContext) {
        let result = (context[0] as! Double) + (context[1] as! Double)
        print("result = \(result)")

struct Application: Command {
    func setup(with config: Configuration) {
        config.use(Calculator(), for: "calc")
        config.use(Option(name: "--hello", defaultValue: false))

    func run(with context: CommandContext) {
        if context.hello as! Bool {
            print("hello world")

try! CommandLine.run(Application())

  1. Build and run wit the following arguments
./example suba "hello world" --age=2


commands parameters -options

The command line arguments are parsed into these parts

  1. Commands (swift build)
  2. Parameters (main.swift)
  3. Options (-c build)


The input of parameters must match a defined regular expression during the application's setup. For example

Parameter(type: String.self)
Parameter(type: Int.self, isRepeating: true)
Parameter(type: Bool)

must have parameters in the with one string at the head and a boolean at the tail with an unlimited amount of integers in-between.

All of the parameters are required


  • All of the options are not requried;
  • Options can have a defualt value, in which case if the user does not enter the option, the default value will be inserted into public struct CommandContext


The types of parameters and options are implemented using Swift Standard Library's types, such as Swift.Int, Swift.Bool, Swift.String etc.


app generate parameters

Each command is associated with an array of strings known as the command's path. In the above example, the path will be ["app", "generate"]

Help Information

Help information is printed under these conditions

  • Options contains "--help"
  • An exception is thrown

Syntax errors would be still printed, but without the full help information


Go to Documents/Overview.md to learn more about the implementation about the package


Stars: 0


Used By

Total: 0


Fix Issue #1 - 2019-11-29 20:29:19

  • Fix #1
  • Write error messages to standard error

Help! - 2019-09-14 06:52:49

Add the ability to print help information. Complete help information will be printed under these conditions

  • User enters "--help"
  • On semantic errors

New API - 2019-08-22 15:08:42

  • API has been refactored to be cleaner (see README.md)
  • Lay in the foundation for better error reporting

Release 1.0.1 - 2019-08-12 13:35:13

No longer throw Error if an named param is not present

Release 1.0.0! - 2019-08-07 12:24:04


  • Refactor API to following Swift API Design Guidelines
  • Fully integrate semantic checks
  • Fix bugs with UnnamedParamChecker
  • Improve printing for some errors

Release 0.2.0 - 2019-08-04 09:57:18

New Features

Syntax Improvement

-name peter

Now is considered to be semantically equal to


Better Error Reporting

struct Token has been made public and ParserError has been revised as

public enum ParserError: Error {
    case unexpected(token: Token)
    case unexepctedEnd
    case expecting(variantOf: [Token])
    case expecting(token: Token)
    case incorrectRootSubcommand(found: String, expecting: String)