Prompter - Capture Command Line Input with Swift
Prompter is light-weight Swift 2+ library that greatly simplifies capturing user input for command line (cli) applications on OSX and Linux.
Specifically, it allow you to prompt the user for input and validate that responses are (currently) String
, Int
, Bool
or a valid single choice from a 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
- required
- message
- optional string to override the default validation message
- block
- 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
let age = prompt.askBool("Do you like beans")
//y, Y, Yes, yes, t, True, 1 are all true etc
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
- formatting
Tests
Run the tests using xctool:
xctool -workspace Prompter.xcworkspace --scheme Prompter
Author
Matthew Clarkson from Hilenium @matt_clarkson
License
Prompter is available under the MIT license. See the LICENSE file for more info.