SwiftArgs is a small Swift framework for creating simple command line interfaces.
You can install SwiftArgs
with the Swift Package Manager or Cocoapods.
Add SwiftArgs
as a dependency in Package.swift
:
// Package.swift
dependencies: [
.package(url: "https://github.com/pkrll/SwiftArgs", 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 CommandOption
represents 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 {
args.printError(error)
args.printUsage()
exit(1)
}
/**
* 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 }
args.printUsage(argument)
} 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.