← 中文资源

简体中文版本

Prompts

轻量、零依赖的 CLI 交互库,支持 text、select、multiselect 与 confirm 等问答类型。

阅读英文原文

概述

Prompts 提供轻量 CLI 交互提问的能力。本参考列出最常用的核心 API 与典型用法,帮助你快速上手并在生产环境中安全使用。

核心 API

  • prompts:签名与典型用法见下方代码示例。
  • select question:签名与典型用法见下方代码示例。
  • onCancel:签名与典型用法见下方代码示例。

代码示例

const prompts = require('prompts');

const res = await prompts({
  type: 'text',
  name: 'project',
  message: 'Project name?',
});
console.log(res.project);

使用注意

取消的问答会返回 undefined,使用前务必判空并为脚本提供 onCancel 优雅退出。

验证方式

运行一个 text 问答并输入答案,确认解析对象在对应 name 下包含该值。