Swift Package Index


Elegant @propertyWrapper-based command line argument parser


  • The latest stable release is v1.0.3. Released over 1 year ago.
  • The last commit to master was over 1 year ago.

Swift Version Compatibility

  • v1.0.3 and master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • v1.0.3 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

CLInterface

Swift 5.1 era argument parser

Swift 5.1 platforms: macOS, Linux Swift Package Manager compatible releases MIT license

CLInterface protocol should be implemented by a class or a struct that will represent command line interface of your program. Apart from required description property, it should contain @Argument and @PositionalArgument properties for any arguments you want. They will be set automatically after you call parseArguments.

Both optional and required arguments are supported. Use default: and optional properies to get the behavior you want. (For example, non-optional property without default value will throw error at parseArguments call.)

Use Swift package manager to add to your project:

        .package(url: "https://github.com/nikstar/CLInterface.git", from: "1.0.3"),

This is work in progress. PRs and suggestions are welcome!

Example

final class Swiftc : CLInterface {
    var description = "Swift compiler"

    @Argument("--output", "-o", usage: "Write output to <file>")
    var outputPath: String?
    
    @Argument("-g", usage: "Emit debug info", default: false)
    var debugMode: Bool
    
    @PositionalArgument(name: "files", usage: "Files that will be compiled")
    var files: [String]
}

After calling parseArguments, you will be able to use these properties like normal variables.

let swiftc = Swiftc()
try swiftc.parseArguments(["-o", "hello", "-g", "main.swift", "Greeter.swift"])

swiftc.outputPath // Optional("hello")
swiftc.debugMode // true
swiftc.files // ["main.swift", "Greeter.swift"]

swiftc -h or swiftc --help prints usage info:

OVERVIEW: Swift compiler

OPTIONS:
--output, -o   Write output to <file>
-g             Emit debug info
--help         Display available options

POSITIONAL ARGUMENTS:
files          Files that will be compiled

See ExampleSwiftc for working example.

See Opusab, my audiobook converter, for another real-life example.