Swiftpack.co - Package - tadija/cli

CLI

Swift package for making simple command line tools.

I made this for personal use, feel free to use it or contribute.

Instructions

  • Create "myapp" directory and initialize an executable swift package
mkdir myapp && cd myapp && swift package init --type executable
  • Edit Package.swift
// swift-tools-version:4.2

import PackageDescription

let package = Package(
    name: "myapp",
    dependencies: [
        .package(
            url: "https://github.com/tadija/cli.git",
            .upToNextMajor(from: "0.1.0")
        ),
    ],
    targets: [
        .target(
            name: "myapp",
            dependencies: ["CLI"]),
        .testTarget(
            name: "myappTests",
            dependencies: ["myapp"]),
    ]
)
  • Edit Sources/myapp/main.swift
import Foundation
import Utility
import CLI

public struct RunCommand: Command {
    public let command = "run"
    public let overview = "prints a \"Hello, World!\" string"

    public init(parser: ArgumentParser) {
        parser.add(subparser: command, overview: overview)
    }

    public func run(with arguments: ArgumentParser.Result) throws {
        print("Hello, World!")
        exit(EXIT_SUCCESS)
    }
}

let app = CLI.Tool(
    overview: "MyApp - does something",
    commands: [RunCommand.self]
)

app.run()
  • Add install / uninstall scripts
mkdir Resources && touch Resources/install.sh && touch Resources/uninstall.sh
# install.sh
swift build -c release -Xswiftc -static-stdlib
cd .build/release
cp -f myapp /usr/local/bin/myapp
cd -
# uninstall.sh
rm /usr/local/bin/myapp
  • Install the app
. Resources/install.sh
  • Run the app
myapp
  • Uninstall the app
. Resources/uninstall.sh

Credits

This post by @eneko

License

This code is released under the MIT license. See LICENSE for details.

Github

link
Stars: 2
Help us keep the lights on