0001 // 0002 // Asker.swift 0003 // Ask 0004 // 0005 // Created by Omar Abdelhafith on 31/10/2015. 0006 // Copyright © 2015 Omar Abdelhafith. All rights reserved. 0007 // 0008 0009 0010 /** 0011 Display a promt to the user 0012 0013 - parameter prompt: The message to display 0014 - parameter customizationBlock: The block to costumize the prompt before displaying 0015 0016 - returns: The string enters from the user 0017 */ 0018 public func ask(prompt: String, customizationBlock: (AskSettings<String> -> Void)? = nil) -> String { 0019 return ask(prompt, type: String.self, customizationBlock: customizationBlock) 0020 } 0021 0022 0023 /** 0024 Display a promt to the user 0025 0026 - parameter prompt:The message to display 0027 - parameter type: The value type to be expected from the user 0028 - parameter customizationBlock: The block to costumize the prompt before displaying 0029 0030 - returns: The string casted to the type requested 0031 - discussion: If the user enters a wrong type, ask will keep prompting until a correct value has been entered 0032 */ 0033 public func ask<T: ArgConvertibleType>(prompt: String, type: T.Type, customizationBlock: (AskSettings<T> -> Void)? = nil) -> T { 0034 0035 PromptSettings.print(prompt) 0036 0037 let settings = getSettings(customizationBlock) 0038 0039 if settings.confirm { 0040 return getValidatedStringWithConfirmation(settings) 0041 } else { 0042 return getValidatedString(settings) 0043 } 0044 } 0045 0046 0047 // MARK:- Internal functions 0048 0049 0050 func getValidatedString
Ask.swift:19 return ask(prompt, type: String.self, customizationBlock: customizationBlock)<T: ArgConvertibleType, W: AskerValidator where W.Item == T>(validator: W) -> T { 0051 let stringOrEmpty = readStringOrEmpty() 0052 return askForValidatedItem(originalValue: stringOrEmpty, validator: validator) 0053 } 0054 0055 0056 func getValidatedStringWithConfirmation
Ask.swift:42 return getValidatedString(settings)<T: ArgConvertibleType, W: AskerValidator where W.Item == T>(validator: W) -> T { 0057 0058 while true { 0059 let stringOrEmpty = readStringOrEmpty() 0060 let answer = askForValidatedItem(originalValue: stringOrEmpty, validator: validator) 0061 0062 if agree("Are you sure?") { 0063 return answer 0064 } else { 0065 PromptSettings.print("? ", terminator: "") 0066 } 0067 } 0068 } 0069 0070 0071 func getSettings
Ask.swift:40 return getValidatedStringWithConfirmation(settings)<T>(callback: (AskSettings<T> -> Void)?) -> AskSettings<T> { 0072 let settings = AskSettings<T>() 0073 callback?(settings) 0074 return settings 0075 } 0076 0077 0078 func readStringOrEmpty
Ask.swift:37 let settings = getSettings(customizationBlock)() -> String { 0079 return PromptSettings.read() ?? "" 0080 } 0081
Agree.swift:17 let value = readStringOrEmpty()Ask.swift:51 let stringOrEmpty = readStringOrEmpty()Ask.swift:59 let stringOrEmpty = readStringOrEmpty()AskerValidator.swift:32 validatedValue = readStringOrEmpty()Choose.swift:84 let stringRead = readStringOrEmpty()