1 个不稳定版本

0.0.1 2020 年 8 月 8 日

#24 in #questions

MIT/Apache

16KB
223 行(不包括注释)

CowMug

License

cowmod 提供一个交互式命令行用户界面,用于向用户提问。

!!!! 目前处于早期开发阶段,尚不稳定 !!!!

用法

将以下内容添加到您的 Cargo.toml

[dependencies]
cowmug = "0.0.1"

许可证

本项目采用以下任一许可证:

任选其一。

示例

此示例代码可在此找到:examples/sample.rs

extern crate cow_mug;
use cow_mug::Prompt;
use cow_mug::Question;
fn main() {
    let mut list_questions: Vec<Question> = Vec::new();
    let mut pizza_size = Question::new(String::from("Choose the size of your pizza: "));
    pizza_size.add_choice(String::from("Large"));
    pizza_size.add_choice(String::from("Medium"));
    pizza_size.add_choice(String::from("Small"));
    list_questions.push(pizza_size);

    let mut pizza_flavor = Question::new(String::from("Choose the flavor:"));
    let pizza_flavor_choices: Vec<String> = vec![
        String::from("Pepperoni"),
        String::from("4 cheeses"),
        String::from("Chicken"),
    ];
    pizza_flavor.add_choices(pizza_flavor_choices);
    list_questions.push(pizza_flavor);

    let mut prompt = Prompt::new();
    prompt.exec(&mut list_questions).unwrap();

    let answer_size = list_questions.get(0).unwrap().string_answer().unwrap();
    let answer_flavor = list_questions.get(1).unwrap().string_answer().unwrap();
    let mut choices_is_correct = Question::new(format!(
        "You choose pizza {} with {}, you confirm it?",
        answer_size, answer_flavor
    ));
    choices_is_correct.add_choice(String::from("yes"));
    choices_is_correct.add_choice(String::from("no"));
    list_questions.clear();
    list_questions.push(choices_is_correct);
    prompt.exec(&mut list_questions).unwrap();

    if let Some(is_correct) = list_questions.get(0).unwrap().string_answer() {
        if is_correct.as_str() == "yes" {
            println!("Great! See ya");
        } else {
            println!("Thats is terrible!! =/ ");
        }
    }
}

Alt Text

依赖项

~175KB