便捷的方法在 Swift CLI 中呈现文本输入和选项选择器提示。
SwiftPrompt
允许单独使用其工具,但最好将您的提示视为“组”,因为SwiftPrompt
会在其中绘制更美观的终端输出。
例如,如果您让用户注册名为“Fruits”的服务,您可以呈现一组输入提示,这些提示绘制在公共标题下。
import SwiftPrompt
Prompt.startPromptGroup(title: "Sign Up to Fruits:")
// present prompts
// Write general update to group
Prompt.writeGroupUpdate(title: "Registering account...")
// Close out the group and write outcome
Prompt.endPromptGroup(title: "Account Registered")
要请求标准文本输入,您可以使用Prompt.textInput
let emailAddress = Prompt.textInput(
question: "What is your email address?",
placeholder: "This input will have not-empty validation",
isSecureEntry: false,
validator: {
// no validation
return .valid
}
)
您还可以根据需要自定义验证逻辑,例如,如果您想确保输入不为空
let emailAddress = Prompt.textInput(
question: "What is your email address?",
placeholder: "This input will have not-empty validation",
isSecureEntry: false,
validator: {
// Ensure input is not empty
if !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return .valid
} else {
return .invalid(message: "Please enter something")
}
}
)
如果您希望将输入屏蔽为◆
符号,您还可以将isSecureEntry
设置为true
// Ask for password
let password = Prompt.textInput(
question: "Enter a password:",
placeholder: "Enter something secure with at least 8 characters",
isSecureEntry: true,
validator: {
if $0.trimmingCharacters(in: .whitespacesAndNewlines).count >= 8 {
return .valid
} else {
return .invalid(message: "Passwords should be at least 8 characters long")
}
}
)
选项提示将向用户显示PromptOption<Type>
列表,并允许他们使用向上、向下和 Enter 键来选择一个显示的项。
虽然选项必须产生相同的值类型,但您可以自定义将要显示的显示标题
let appTypes: [PromptOption<String>] = [
.init(title: "SwiftUI", value: "swift-ui"),
.init(title: "UIKit", value: "ui-kit")
]
// Present prompt
let option = Prompt.selectOption(
question: "Select your tech stack",
options: appTypes
)
print(option) // prints `swift-ui` or `ui-kit` depending on selection
这还允许您使用自定义类型或枚举等,例如从枚举中选择您最喜欢的水果
// Available Fruits
enum Fruit: CaseIterable {
case apple
case banana
case orange
case tomato
var title: String {
switch self {
case .apple:
return "🍎 Apple"
case .banana:
return "🍌 Banana"
case .orange:
return "🍊 Orange"
case .tomato:
return "🍅 Tomato"
}
}
// Convenience getter for the `Option<Type>` instance the prompt will use
var option: PromptOption<Fruit> {
return .init(title: title, value: self)
}
}
// Ask for favourite fruit selection
let selectedFruit = Prompt.selectOption(
question: "Select your favourite fruit",
options: Fruit.allCases.map(\.option)
)
print(selectedFruit) // prints the selected `Fruit` enum
将以下内容添加到您的Package.swift
文件中
let package = Package(
// name, platforms, products, etc.
dependencies: [
// ...
.package(url: "https://github.com/CheekyGhost-Labs/SwiftPrompt.git", from: "1.0.0"),
],
.product(name: "SwiftPrompt", package: "SwiftPrompt")
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "SwiftPrompt", package: "SwiftPrompt")
]
)
// ...
]
)
我正在研究一些测试方法。 然而,由于该实用程序的性质,我正在进行大量手动测试以启用该库的使用。 请关注此空间,因为在不久的将来会添加单元测试方法。
此实用程序深受名为swift-clack
的Clack
的正在进行的移植版本的启发。 该存储库可以在这里找到
原始的 clack 库(在 JS 领域)可以在这里找到