如何用Node.js readline实现提问后换行输入响应?
解决方法
只需要在提问的提示文本末尾加上换行符\n,就能让光标跳到下一行等待输入。同时记得调用rl.close()关闭readline接口,避免程序卡住无法退出。
修改后的代码:
async function IsAgreed() { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let options = ['yes', 'y', 'YES', 'Y']; // 提示语末尾添加\n实现换行 let response = await rl.question('Do you agree to execute ?\n'); // 关闭接口,防止程序无法退出 rl.close(); return options.includes(response); }
修改后运行效果:
Do you agree to execute ? | <-- 光标会在这里等待输入
内容的提问来源于stack exchange,提问作者user25384569
相关产品推荐
相关产品推荐

