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

Swift Argument Parser


  • [x] Nested commands
  • [x] Type checking for options and parameters
  • [x] 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
Help us keep the lights on


Used By

Total: 0


2.1.1 - Nov 29, 2019

  • Fix #1
  • Write error messages to standard error

2.1.0 - Sep 14, 2019

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

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

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