moon add wangmingfa/promptximport { "wangmingfa/promptx" }
import { "moonbitlang/async" }
async fn main {
// 创建问题数组
let questions : Array[Question] = [
input(
question_name="username",
question_message="What is your name?",
question_default=Some(AnswerValue::StringValue("Guest"))
),
confirm(
question_name="subscribe",
question_message="Do you want to subscribe?"
),
list(
question_name="color",
question_message="Choose your favorite color:",
question_choices=["Red", "Green", "Blue"]
),
]
// 执行提示并获取答案
let answers = prompt(questions)
// 处理答案
match answers["username"] {
AnswerValue::StringValue(name) => println("Hello, \{name}!")
_ => println("Hello, Guest!")
}
}pub async fn prompt(questions : Array[Question]) -> Answerpub fn input(
question_name~ : String,
question_message~ : String,
question_default? : AnswerValue? = None
) -> Question///|
let q = input(
question_name="username",
question_message="What is your username?",
question_default=Some(AnswerValue::StringValue("admin")),
)pub fn confirm(
question_name~ : String,
question_message~ : String,
question_default? : AnswerValue? = None
) -> Question///|
let q = confirm(
question_name="agree",
question_message="Do you agree to the terms?",
question_default=Some(AnswerValue::BoolValue(true)),
)pub fn list(
question_name~ : String,
question_message~ : String,
question_choices~ : Array[String],
question_default? : AnswerValue? = None
) -> Question///|
let q = list(
question_name="color",
question_message="Choose your favorite color:",
question_choices=["Red", "Green", "Blue", "Yellow"],
question_default=Some(AnswerValue::IntValue(0)),
)pub fn rawlist(
question_name~ : String,
question_message~ : String,
question_choices~ : Array[String],
question_default? : AnswerValue? = None
) -> Question///|
let q = rawlist(question_name="action", question_message="Select an action:", question_choices=[
"Run", "Debug", "Build", "Test",
])pub fn expand(
question_name~ : String,
question_message~ : String,
question_choices~ : Array[String],
question_default? : AnswerValue? = None
) -> Question///|
let q = expand(
question_name="format",
question_message="Select output format:",
question_choices=["JSON", "XML", "YAML"],
)pub fn checkbox(
question_name~ : String,
question_message~ : String,
question_choices~ : Array[String],
question_default? : AnswerValue? = None
) -> Question///|
let q = checkbox(
question_name="hobbies",
question_message="Select your hobbies:",
question_choices=["Reading", "Gaming", "Sports", "Music"],
)pub fn password(
question_name~ : String,
question_message~ : String,
question_default? : AnswerValue? = None
) -> Question///|
let q = password(question_name="pwd", question_message="Enter your password:")pub fn editor(
question_name~ : String,
question_message~ : String,
question_default? : AnswerValue? = None
) -> Question///|
let q = editor(
question_name="bio",
question_message="Write your bio:",
question_default=Some(AnswerValue::StringValue("No bio provided")),
)///|
pub enum QuestionType {
Input // 文本输入
Confirm // 确认(是/否)
List // 列表选择
RawList // 带数字的列表选择
Expand // 展开式选择
Checkbox // 多选框
Password // 密码输入
Editor // 多行文本输入
}///|
pub enum AnswerValue {
StringValue(String) // 字符串值(Input、List、Password、Editor)
BoolValue(Bool) // 布尔值(Confirm)
ArrayValue(Array[String]) // 数组值(Checkbox)
IntValue(Int) // 整数值(用于指定默认选项索引)
}///|
pub type Answer = Map[String, AnswerValue]///|
pub struct Question {
question_type : QuestionType // 问题类型
question_name : String // 问题名称(用于获取答案)
question_message : String // 问题文本
question_default : AnswerValue? // 可选的默认值
question_choices : Array[String]? // 可选的选项列表
}pub fn parse_int(s : String) -> Int?match parse_int("123") {
Some(n) => println("Number: \{n}")
None => println("Not a number")
}import { "wangmingfa/promptx" }
import { "moonbitlang/async" }
async fn main {
println("=== MoonBit Inquirer Demo ===\n")
let questions : Array[Question] = [
// 文本输入
input(
question_name="username",
question_message="What is your name?"
),
// 确认问题
confirm(
question_name="subscribe",
question_message="Do you want to subscribe to our newsletter?",
question_default=Some(AnswerValue::BoolValue(true))
),
// 列表选择
list(
question_name="color",
question_message="Choose your favorite color:",
question_choices=["Red", "Green", "Blue", "Yellow", "Purple"],
question_default=Some(AnswerValue::IntValue(0))
),
// 多选框
checkbox(
question_name="hobbies",
question_message="Select your hobbies:",
question_choices=["Reading", "Gaming", "Sports", "Music", "Cooking"]
),
// 密码输入
password(
question_name="password",
question_message="Enter a password (optional)"
),
// 多行文本
editor(
question_name="bio",
question_message="Write a short bio (optional)"
),
]
// 执行提示
let answers = prompt(questions)
// 显示结果
println("\n=== Your Answers ===\n")
match answers["username"] {
AnswerValue::StringValue(name) =>
if name.length() == 0 {
println("Username: (empty)")
} else {
println("Username: \{name}")
}
_ => println("Username: (not provided)")
}
match answers["subscribe"] {
AnswerValue::BoolValue(subscribed) =>
if subscribed {
println("Newsletter subscription: Yes")
} else {
println("Newsletter subscription: No")
}
_ => println("Newsletter subscription: (not provided)")
}
match answers["color"] {
AnswerValue::StringValue(color) => println("Favorite color: \{color}")
_ => println("Favorite color: (not selected)")
}
match answers["hobbies"] {
AnswerValue::ArrayValue(hobbies) =>
if hobbies.length() > 0 {
println("Hobbies: \{hobbies.join(", ")}")
} else {
println("Hobbies: (none selected)")
}
_ => println("Hobbies: (none selected)")
}
match answers["password"] {
AnswerValue::StringValue(pwd) =>
if pwd.length() > 0 {
println("Password: [hidden, length: \{pwd.length()} characters]")
} else {
println("Password: (empty)")
}
_ => println("Password: (not provided)")
}
match answers["bio"] {
AnswerValue::StringValue(bio) =>
if bio.length() == 0 {
println("Bio: (empty)")
} else {
println("Bio: \{bio}")
}
_ => println("Bio: (not provided)")
}
println("\n=== Demo Complete ===")
}moon testtest {
let map = { 3: "three", 8: "eight", 1: "one" }
@test.assert_eq(map.get(2), None)
@test.assert_eq(map.get(3), Some("three"))
map.set(3, "updated")
@test.assert_eq(map.get(3), Some("updated"))
}import { "wangmingfa/promptx" }
import { "moonbitlang/async" }
async fn main {
// 创建问题数组
let questions : Array[Question] = [
input(
question_name="username",
question_message="What is your name?",
question_default=Some(AnswerValue::StringValue("Guest"))
),
confirm(
question_name="subscribe",
question_message="Do you want to subscribe?"
),
list(
question_name="color",
question_message="Choose your favorite color:",
question_choices=["Red", "Green", "Blue"]
),
]
// 执行提示并获取答案
let answers = prompt(questions)
// 处理答案
match answers["username"] {
AnswerValue::StringValue(name) => println("Hello, \{name}!")
_ => println("Hello, Guest!")
}
}import { "wangmingfa/promptx" }
import { "moonbitlang/async" }
async fn main {
// 创建问题数组
let questions : Array[Question] = [
input(
question_name="username",
question_message="What is your name?",
question_default=Some(AnswerValue::StringValue("Guest"))
),
confirm(
question_name="subscribe",
question_message="Do you want to subscribe?"
),
list(
question_name="color",
question_message="Choose your favorite color:",
question_choices=["Red", "Green", "Blue"]
),
]
// 执行提示并获取答案
let answers = prompt(questions)
// 处理答案
match answers["username"] {
AnswerValue::StringValue(name) => println("Hello, \{name}!")
_ => println("Hello, Guest!")
}
}import { "wangmingfa/promptx" }
import { "moonbitlang/async" }
async fn main {
// 创建问题数组
let questions : Array[Question] = [
input(
question_name="username",
question_message="What is your name?",
question_default=Some(AnswerValue::StringValue("Guest"))
),
confirm(
question_name="subscribe",
question_message="Do you want to subscribe?"
),
list(
question_name="color",
question_message="Choose your favorite color:",
question_choices=["Red", "Green", "Blue"]
),
]
// 执行提示并获取答案
let answers = prompt(questions)
// 处理答案
match answers["username"] {
AnswerValue::StringValue(name) => println("Hello, \{name}!")
_ => println("Hello, Guest!")
}
}fn checkbox(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue) -> Questionfn confirm(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Questionfn editor(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Questionfn expand(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue?) -> Questionfn input(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Questionfn list(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue?) -> Questionfn password(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Questionfn rawlist(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue?) -> QuestionInstall
Download zip