Prompter
Prompter copied to clipboard
Capture user input at the command line on OSX and Linux
Prompter - Capture Command Line Input with Swift
Prompter is lightweight Swift 2+ library that greatly simplifies capturing user input for command line (cli) applications on OSX and Linux.
Specifically, it allows you to prompt the user for input and validate that responses are (currently) String, Int, Bool, or a valid single choice from a given list.
Installation
Swift Package Manager
To add Prompter via the SPM, add the following to your Package.swift file:
let package = Package(
name: "YourPackageName"
dependencies: [
.Package(url: "https://github.com/mpclarkson/Prompter.git", majorVersion: 1),
]
)
CocoaPods
Prompter is available through CocoaPods.
To install it, simply add the following line to your Podfile:
pod "Prompter"
Instructions
Documentation
This library is well documented. View the documentation.
Overview
Each ask method includes the following arguments:
question- requiredmessage- optional string to override the default validation messageblock- an optional closure that is called on success
import Prompter
let prompt = Prompt()
//Prompt the user for a string input
let name = prompt.askString("What is your name?",
message: "This is an optional validation message!") { string in
//this is an optional block
}
//Prompt the user for a string input
let age = prompt.askInt("How old are you?")
//Prompt the user for a Boolean response
//y, Y, Yes, yes, t, True, 1 are all true etc
let age = prompt.askBool("Do you like beans")
//Ask the user to select from a list of choices
let choices = ["beans", "apples"]
let age = prompt.askChoice("Which of these do you prefer",
choices: choices) { (index, string) in
print(index, string)
//0, beans
}
Todo
- askDate
- askMultiple
- initWithStyle
##Tests
Run the tests using xctool:
xctool -workspace Prompter.xcworkspace -scheme Prompter test
Author
Matthew Clarkson from Hilenium @matt_clarkson
License
Prompter is available under the MIT license. See the LICENSE file for more info.