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

Swift Argument Parser


  • [x] Nested commands
  • [x] 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"]


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


Stars: 0
Help us keep the lights on


Used By

Total: 0


2.0.0 - Aug 22, 2019

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

1.0.1 - Aug 12, 2019

No longer throw Error if an named param is not present

1.0.0 - Aug 7, 2019


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

0.2.0 - Aug 4, 2019

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)