【问题标题】:Fill out both Question and Answer field In Enquirer CLI Prompt在 Enquirer CLI Prompt 中填写问题和答案字段
【发布时间】:2020-07-03 23:18:55
【问题描述】:

我正在使用 enquirer 包来提示用户在我的 Nodejs CLI 应用程序中输入。

目前,我正在使用内置的form 提示符进行输入,就像这样 -

const prompt = new Form({
  name: 'user',
  message: 'Please provide the following information:',
  choices: [
    { name: 'First Name', message: 'First Name'},
    { name: 'Last Name', message: 'Last Name'},
    { name: 'Date of Birth', message: 'Date of Birth'}
  ]
});

这适用于具有预定义字段的表单,即。 -

Please provide the following information:    

Field1: _______ 

Field2: _______ 

Field3: _______ 

但我现在想向用户展示一个自定义字段,能够编辑该字段的问题和答案,即。 -

________: _______

理想情况下,此自定义字段将与其他预定义字段一起显示 -

Field1: _______ 

Field2: _______ 

Field3: _______ 

______: _______

用户可以这样填写 -

Field1: Answer1

Field2: Answer2 

Field3: Answer3 

NewField: Answer4

这将返回一个类似于 -

的对象
{
  "Field1": "Answer1",
  "Field2": "Answer2",
  "Field3": "Answer3",
  "NewField": "Answer4",
}

Enquirer 确实支持 custom prompts,但我正在努力弄清楚我应该如何实施这样的事情。

或者,如果使用不同的包可以更轻松地实现这种类型的提示,我愿意使用另一个模块(即 Inquirer)。

任何帮助或指点将不胜感激,谢谢。

【问题讨论】:

    标签: javascript node.js typescript input command-line-interface


    【解决方案1】:

    查看Editable Prompt(它扩展了Form Prompt 以添加您正在寻找的功能)。您按Ctrl+n 添加新项目。

    见examples here...最简单的是add choice

    与您的用例的主要区别在于,当点击Ctrl+n 时,会提示用户输入名称,然后可以输入值。

    您还可以使用传递给提示构造函数的header 或footer 选项向您的用户提供额外的说明。

    【讨论】:

    • 非常感谢,这正是我所追求的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多