( prompt: string, choices: string[], defaultChoice?: string, limitMessage?: string, )
| 34 | * @param limitMessage Message to show when invalid choice is entered |
| 35 | */ |
| 36 | export async function questionWithChoices( |
| 37 | prompt: string, |
| 38 | choices: string[], |
| 39 | defaultChoice?: string, |
| 40 | limitMessage?: string, |
| 41 | ): Promise<string> { |
| 42 | while (true) { |
| 43 | const answer = await question(prompt); |
| 44 | |
| 45 | // Handle default choice |
| 46 | if (answer === "" && defaultChoice !== undefined) { |
| 47 | return defaultChoice; |
| 48 | } |
| 49 | |
| 50 | // Check if answer is valid |
| 51 | if (choices.includes(answer)) { |
| 52 | return answer; |
| 53 | } |
| 54 | |
| 55 | // Show limit message if provided |
| 56 | if (limitMessage) { |
| 57 | console.log(limitMessage); |
| 58 | } |
| 59 | } |
| 60 | } |
no test coverage detected