Skip to content

提示

Ace 允许您在命令中显示交互式终端提示,由 @poppinss/prompts 提供支持。

您可以使用 this.prompt 属性在命令类中访问 prompts 模块。每个提示类型都可用作方法调用。可用的提示类型包括:

  • input:接受文本输入
  • password:接受掩码输入
  • confirm:接受是/否确认
  • select:显示单选列表
  • multiselect:显示多选列表
  • autocomplete:显示带有搜索/过滤的选择列表

通用提示选项

以下选项可用于所有提示类型。

default

指定在用户不输入任何内容时使用的默认值。

ts
await this.prompt.ask('Enter your name', {
  default: 'Guest'
})

name

用于存储答案的属性的名称。仅在您将多个提示作为批次运行时有用。

hint

显示提示文本以引导用户。

ts
await this.prompt.ask('Enter your name', {
  hint: 'We need your name to personalize the greeting'
})

result

在返回之前转换最终值的函数。

ts
await this.prompt.ask('Enter your name', {
  result(value) {
    return value.toUpperCase()
  }
})

format

在用户输入时格式化值的函数。与 result 不同,它会在用户输入时影响用户界面。

ts
await this.prompt.ask('Enter your name', {
  format(value) {
    return value.toUpperCase()
  }
})

validate

在用户提交答案后验证输入值的函数。返回 true 表示有效输入,返回错误消息字符串表示无效输入。

ts
await this.prompt.ask('Enter your email', {
  async validate(value) {
    if (!value.includes('@')) {
      return 'Please enter a valid email'
    }
    return true
  }
})

limit

限制在选择提示中一次显示的选项数量。

ts
await this.prompt.choice('Select a color', ['red', 'green', 'blue'], {
  limit: 2
})

ask

ask 方法显示文本输入提示。您可以使用选项对象定义可选的默认值。

ts
const answer = await this.prompt.ask('What is your name?')
ts
const answer = await this.prompt.ask('What is your name?', {
  default: 'Guest'
})

secure

secure 方法显示用于敏感输入(如密码)的掩码输入提示。

ts
const password = await this.prompt.secure('Enter your password')

您可以使用 validate 选项验证输入。

ts
const password = await this.prompt.secure('Enter your password', {
  validate(value) {
    if (value.length < 8) {
      return 'Password must be at least 8 characters'
    }
    return true
  }
})

choice

choice 方法显示单选列表。您可以将选项作为简单的字符串数组或带有 namemessage 属性的对象数组提供。

ts
const color = await this.prompt.choice('Select a color', [
  'red',
  'green',
  'blue'
])

带有自定义值的对象:

ts
const color = await this.prompt.choice('Select a color', [
  { name: 'red', message: 'Red color' },
  { name: 'green', message: 'Green color' },
  { name: 'blue', message: 'Blue color' },
])

multiple

multiple 方法显示多选列表。

ts
const colors = await this.prompt.multiple('Select colors', [
  'red',
  'green',
  'blue'
])

带有自定义值的对象:

ts
const colors = await this.prompt.multiple('Select colors', [
  { name: 'red', message: 'Red color' },
  { name: 'green', message: 'Green color' },
  { name: 'blue', message: 'Blue color' },
])

confirm

confirm 方法显示接受是或否答案的确认提示。

ts
const confirmed = await this.prompt.confirm('Are you sure?')

toggle

toggle 方法显示带有两个选项的切换提示。

ts
const useYarn = await this.prompt.toggle('Use yarn?', ['No', 'Yes'])

autocomplete

autocomplete 方法显示带有搜索/过滤功能的选择列表。

ts
const framework = await this.prompt.autocomplete('Select a framework', [
  'Express',
  'Fastify',
  'Koa',
  'Hapi',
  'AdonisJS'
])

您还可以使用 multiple: true 选项启用多选:

ts
const frameworks = await this.prompt.autocomplete('Select frameworks', [
  'Express',
  'Fastify',
  'Koa',
  'Hapi',
  'AdonisJS'
], {
  multiple: true
})