0001    //
0002    //  Input.swift
0003    //  Example
0004    //
0005    //  Created by Jake Heiser on 8/17/14.
0006    //  Copyright (c) 2014 jakeheis. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    
0011    public class Input
CLI.swift:45
        Input.checkForPipedData()
{ 0012 0013 private static let inputHandle
Input.swift:35
        var input = String(data: inputHandle.availableData, encoding: NSUTF8StringEncoding)!
Input.swift:102
        inputHandle.readabilityHandler = {(inputHandle) in
= NSFileHandle.fileHandleWithStandardInput() 0014 0015 public private(set) static var pipedData
Input.swift:27
        if pipedData != nil {
Input.swift:103
            pipedData = String(data: inputHandle.availableData, encoding: NSUTF8StringEncoding)
: String? = nil 0016 0017 static let PipeUserInputOverlapError
Input.swift:28
            throw PipeUserInputOverlapError
= CLIError.Error("Data should not be both piped and input") 0018 0019 // MARK: - Public 0020 0021 /** 0022 Awaits a string of input 0023 - Parameter message: message to be printed before accepting input (e.g. "Name: ") 0024 - Throws: Input.PipeUserInputOverlapError if the user piped data and this method was called 0025 */ 0026 public class func awaitInput
Input.swift:49
            let str = try awaitInput(message: message)
(message message: String?) throws -> String { 0027 if pipedData != nil { 0028 throw PipeUserInputOverlapError 0029 } 0030 0031 if let message = message { 0032 print(message) 0033 } 0034 0035 var input = String(data: inputHandle.availableData, encoding: NSUTF8StringEncoding)! 0036 input = input.substringToIndex(input.endIndex.advancedBy(-1)) 0037 0038 return input 0039 } 0040 0041 /** 0042 Awaits a string of valid input; continues accepting input until the given input is determined to be valid 0043 - Parameter message: message to be printed before accepting input (e.g. "Name: ") 0044 - Parameter validation: closure evaluating whether the given input was valid 0045 - Throws: Input.PipeUserInputOverlapError if the user piped data and this method was called 0046 */ 0047 public class func awaitInputWithValidation
Input.swift:66
        let input = try awaitInputWithValidation(message: message) {(input) in
(message message: String?, validation: (input: String) -> Bool) throws -> String { 0048 while (true) { 0049 let str = try awaitInput(message: message) 0050 0051 if validation(input: str) { 0052 return str 0053 } else { 0054 print("Invalid input") 0055 } 0056 } 0057 } 0058 0059 /** 0060 Awaits a string of convertible input; continues accepting input until the given input is successfully converted 0061 - Parameter message: message to be printed before accepting input (e.g. "Name: ") 0062 - Parameter conversion: closure attempting to convert the input to the desired output 0063 - Throws: Input.PipeUserInputOverlapError if the user piped data and this method was called 0064 */ 0065 public class func awaitInputWithConversion
Input.swift:79
        return try awaitInputWithConversion(message: message) { Int($0) }
Input.swift:88
        return try awaitInputWithConversion(message: "\(message) [y/N]: ") {(input) in
<T>(message message: String?, conversion: (input: String) -> T?) throws -> T { 0066 let input = try awaitInputWithValidation(message: message) {(input) in 0067 return conversion(input: input) != nil 0068 } 0069 0070 return conversion(input: input)! 0071 } 0072 0073 /** 0074 Awaits the input of an Int 0075 - Parameter message: message to be printed before accepting input (e.g. "Name: ") 0076 - Throws: Input.PipeUserInputOverlapError if the user piped data and this method was called 0077 */ 0078 public class func awaitInt(message message: String?) throws -> Int { 0079 return try awaitInputWithConversion(message: message) { Int($0) } 0080 } 0081 0082 /** 0083 Awaits yes/no input; "y" and "yes" are accepted as yes and "n" and "no" are accepted as no (case insensitive) 0084 - Parameter message: message to be printed before accepting input (e.g. "Name: ") 0085 - Throws: Input.PipeUserInputOverlapError if the user piped data and this method was called 0086 */ 0087 public class func awaitYesNoInput(message message: String = "Confirm?") throws -> Bool { 0088 return try awaitInputWithConversion(message: "\(message) [y/N]: ") {(input) in 0089 if input.lowercaseString == "y" || input.lowercaseString == "yes" { 0090 return true 0091 } else if input.lowercaseString == "n" || input.lowercaseString == "no" { 0092 return false 0093 } 0094 0095 return nil 0096 } 0097 } 0098 0099 // MARK: - Internal 0100 0101 class func checkForPipedData
CLI.swift:45
        Input.checkForPipedData()
() { 0102 inputHandle.readabilityHandler = {(inputHandle) in 0103 pipedData = String(data: inputHandle.availableData, encoding: NSUTF8StringEncoding) 0104 inputHandle.readabilityHandler = nil 0105 } 0106 NSProcessInfo.processInfo().arguments // For whatever reason, this triggers readabilityHandler for the pipe data 0107 } 0108 0109 }