Latest 0.5.4
License MIT
Platforms osx 10.10

Build Status
Pod version


SwiftArgs is a small Swift framework for creating simple command line interfaces.


You can install SwiftArgs with the Swift Package Manager or Cocoapods.

Swift Package Manager

Add SwiftArgs as a dependency in Package.swift:

// Package.swift

dependencies: [
    .package(url: "", from: "0.5.0")

Add SwiftArgs to your Podfile:

pod 'SwiftArgs', '~> 0.5'


SwiftArgs offers four different kinds of argument types: CommandOption, BoolOption, StringOption and EnumOption.

Supply these to the SwiftArgs object, and run the parse() method to validate and parse the command line arguments (see below for an example).


A CommandOptionrepresents a command, which can accept sub arguments on its own:

$ my_app init
$ my_app init --bare
$ my_app package init --type library

BoolOption represents a boolean flag:

$ my_app --help

A StringOption represents an option that can take an arbitrary value:

$ my_app --set-path /some/path

An EnumOption<T> represents an option that only accepts predefined values:

$ my_app --type library


import Foundation
import SwiftArgs

enum BuildType: String {
    case debug
    case release

let help = BoolOption(
    name: "help",
    shortFlag: "h",
    longFlag: "help",
    description: "Display available options"

let version = BoolOption(
    name: "version",
    shortFlag: "v",
    longFlag: "version",
    description: "Display version information"

let buildType = EnumOption<BuildType>(
    name: "BuildType",
    shortFlag: "t",
    longFlag: "type",
    description: "Specify the build configuration: debug|release",
    isRequired: true

let clean = CommandOption("clean", description: "Clean up any build artifacts")
let build = CommandOption("build", withArguments: [help, buildType], description: "Build the project")
let test = CommandOption("test", withArguments: [help, buildType], description: "Test the project")
let run = CommandOption("run", withArguments: [help, buildType], description: "Execute the project")

let args = SwiftArgs(arguments: [help, version, clean, build, test, run])

do {
    try args.parse() // or try args.parse(["build", "--type", "debug"])
} catch {

*   Check if the BoolOption help (-h, --help) or version
*   (-v --version) was specified with the value property.
if help.value! {
    var argument: Argument? = nil

    if build.value { argument = build }
    if test.value { argument = test }
    if run.value { argument = run }

} else if version.value! {
    print("SwiftArgsDemo v1.0")
} else {
    *   You can directly access the EnumOption's value property
    *   to check its value (nil if not used)...
    if buildType.value == BuildType.debug {
        print("Build type: Debug!")
    } else if buildType.value == BuildType.release {
        print("Build type: Release!")
    *   ... or to check which command it's associated with, use
    *   optional chaining to unwrap the nested arguments.
    if let bType = build.argument as? EnumOption<BuildType>, let value = bType.value {
        switch value {
        case BuildType.debug:
            print("Build type: Debug!")
        case BuildType.release:
            print("Build type: Release!")

    if build.value {
        print("Commence building...")


This framework is, in its API, heavily inspired by the CommandLineKit framework.


SwiftArgs was created by Ardalan Samimi.

Latest podspec

    "name": "SwiftArgs",
    "version": "0.5.4",
    "summary": "A small Swift framework for creating simple command line interfaces.",
    "description": "`SwiftArgs` lets you handle command line arguments with ease. Just supply the valid arguments, and let `SwiftArgs` handle the rest.",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "Ardalan Samimi": "[email protected]"
    "social_media_url": "",
    "platforms": {
        "osx": "10.10"
    "source": {
        "git": "",
        "tag": "0.5.4"
    "source_files": [
    "swift_version": "4.0",
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "4.0"
    "static_framework": true

Pin It on Pinterest

Share This