SwiftArg

1.0.2

A stupid simple command line argument parser for Swift.
jaredmpayne/SwiftArg

What's New

2020-09-15T03:32:27Z

Update for macOS 11.0, Swift 5.3.

SwiftArg

A stupid simple argument parser for Swift.

SwiftArg is a command line argument parser with virtually no learning curve. It relies only on the Swift standard library.

Example

import SwiftArg

let parser = Parser()

// Add long options.
parser.addOption(names: ["version"])

// Add short options.
parser.addOption(names: ["f"])
parser.addOption(names: ["r"])

// Long and short options are automatically differentiated between.
parser.addOption(names: ["help", "h"])

// Positional arguments are added to the last option raised, with a limit you define.
// Options can be assigned to with either syntax: `--output=foo.txt` or `--output foo.txt`.
parser.addOption(names: ["output", "o"], maxValueCount: 1)

// So if the arguments were "foo --help -ru -o foo.txt bar --blah"...
parser.parse(arguments: CommandLine.arguments)

// ...then all of the following expressions would be true.
parser.executableArgument == "foo"

parser.hasRaisedOption(named: "h")
parser.hasRaisedOption(named: "r")
parser.hasRaisedOption(named: "help")

!parser.hasRaisedOption(named: "f")

parser.hasRaisedOption(named: "output")
parser.valuesOfOption(named: "output") == ["foo.txt"]

parser.unrecognizedArguments == ["-u", "bar", "--blah"]

This covers the entire interface. Check the Xcode Quick Help if you still want more information on individual functions.

Importing

To import SwiftArg into your project, just add it to your package dependencies in Package.swift and as a target dependency to any target that will import SwiftArg.

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .package(url: "https://github.com/jaredmpayne/SwiftArg.git", from: "1.0")
    ]
    products: [
        .library(name: "YourPackage", targets: ["YourPackage"])
    ],
    targets: [
        .target(name: "YourPackage", dependencies: ["SwiftArg"]),
        .testTarget(name: "YourPackageTests", dependencies: ["YourPackage"])
    ]
)

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Oct 23 2024 08:18:23 GMT-0900 (Hawaii-Aleutian Daylight Time)