Skip to main content

prompts

prompts

The prompts property is an array of objects that define the questions to ask the user when a command is run.

Usage​

Prompts are passed as an array of objects, with each object containing the properties of the prompt.

{
prompts: [
{
type: 'input',
name: 'name',
label: 'What is your name?'
}
]
}

Properties​

PropertyRequiredType(s)DefaultDescription
nameYstringPrompt name
typeNstringinputPrompt type
labelYstringPrompt message
optionsNany[]/functionPrompt choices
defaultNanyDefault value
tagsNstring/arrayTag(s) to apply in details
validateNfunctionFunction to validate value
transformNfunctionFunction to transform value
whenNfunctionFunction to determine if prompt should be asked
forceNbooleanfalseAsk prompt even if value is already present
_overridesNobjectOverrides for the prompt
name: string
type?: string
label: string // message
options?: any[] | ((answers) => any[]) // choices
default?: any
tags?: string | string[]
validate?: (v) => Promise<boolean>
transform?: (v) => any // filter
when?: (answers) => boolean // when
force?: boolean // askAnswered
_overrides?: { [key: string]: any }

name​

The name of the prompt. This is the key that the value will be stored under in the command data object.

type​

The type of the prompt. This can be a string or a function. If a function is provided, it will be used to parse the value. Default is input.

Common values for type include:

  • input
  • number
  • confirm
  • list
  • rawlist
  • expand
  • checkbox
  • password

More information can be found in the Inquirer.js documentation.

label​

The message to display to the user when asking the prompt.

{
prompts: [
{
name: 'name',
label: 'What is your name?'
}
]
}

options​

The choices available to the user. This can be an array of strings or objects, or a function that returns an array of strings or objects.

{
prompts: [
{
name: 'name',
type: 'list',
label: 'What is your name?',
options: ['Alice', 'Bob', 'Charlie']
}
]
}

default​

The default value for the prompt.

{
prompts: [
{
name: 'name',
label: 'What is your name?',
default: 'Alice'
}
]
}

tags​

A string or array of strings to apply to the prompt in the details object.

{
prompts: [
{
name: 'name',
label: 'What is your name?',
tags: ['name']
}
]
}

validate​

A function that validates the value of the prompt. Should return a boolean.

{
prompts: [
{
name: 'name',
label: 'What is your name?',
validate: (v) => v.length > 0
}
]
}

transform​

A function that transforms the value of the prompt.

{
prompts: [
{
name: 'name',
label: 'What is your name?',
transform: (v) => v.toUpperCase()
}
]
}

when​

A function that determines if the prompt should be asked. Should return a boolean.

{
prompts: [
{
name: 'age',
label: 'How old are you?'
},
{
name: 'name',
label: 'What is your name?',
when: (answers) => answers.age > 18
}
]
}

force​

A boolean that determines if the prompt should be asked even if the value is already present.

{
prompts: [
{
name: 'name',
label: 'What is your name?',
force: true
}
]
}

_overrides​

An object that contains overrides for the prompt directly to the underlying prompt library.

{
prompts: [
{
name: 'name',
label: 'What is your name?',
_overrides: {
when: (answers) => answers.age > 18
}
}
]
}

Examples​

Basic Usage​

new Command({
name: 'copy',
prompts: [
{
name: 'name',
label: 'What is your name?'
}
],
action: async (data, details) => console.log(data)
})
$ copy
? What is your name? Alice
{
name: 'Alice'
}

Multiple Prompts​

new Command({
name: 'copy',
prompts: [
{
name: 'name',
label: 'What is your name?'
},
{
name: 'age',
label: 'How old are you?'
}
],
action: async (data, details) => console.log(data)
})
$ copy
? What is your name? Alice
? How old are you? 30
{
name: 'Alice',
age: 30
}

Conditional Prompts​

new Command({
name: 'hello',
options: [
{
name: 'name'
}
]
prompts: [
{
name: 'name',
label: 'What is your name?',
when: (answers) => answers.age > 18
}
],
action: async (data, details) => console.log(data)
})
$ hello --name Alice
{
name: 'Alice'
}