Skip to content

Instantly share code, notes, and snippets.

@moaible
Created January 16, 2017 14:02
Show Gist options
  • Select an option

  • Save moaible/c21d4e3d370c066f28f12c77f158be04 to your computer and use it in GitHub Desktop.

Select an option

Save moaible/c21d4e3d370c066f28f12c77f158be04 to your computer and use it in GitHub Desktop.
swift cli system function
//
// SystemMainAction.swift
// SystemMainAction
//
// Created by moaible on 2017/01/16.
//
//
import Foundation
typealias SystemAction = () -> Void
fileprivate var fulfillAction: SystemAction = {
exit(EXIT_SUCCESS)
}
fileprivate var rejectAction: SystemAction = {
exit(EXIT_FAILURE)
}
typealias SystemMainAction = (
_ path: String,
_ args: [String],
_ env: [String: String],
_ fulfill: SystemAction,
_ reject: SystemAction) -> Void
func systemMain(action: SystemMainAction) {
action(
ProcessInfo.processInfo.arguments.first ?? "",
Array(ProcessInfo.processInfo.arguments.dropFirst()),
ProcessInfo.processInfo.environment,
fulfillAction,
rejectAction)
RunLoop.main.run()
}
@moaible

moaible commented Jan 16, 2017

Copy link
Copy Markdown
Author

usage

systemMain { path, args, env, fulfill, reject in
    print(path)
    print(args)
    env.forEach({ (key: String, value: String) in
        print("[\(key):\(value)]")
    })

//    // fail case
//    reject()

    fulfill()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment