【发布时间】:2020-04-28 18:40:49
【问题描述】:
我正在编写一个旨在从 Bash 终端执行的 Node 脚本。它需要几个文件名,然后通过有关如何处理它们的提示向用户提问。我使用yargs 解析命令行参数,使用prompt-sync 询问用户问题,一切似乎都很好......
除非我像这样将参数传递给我的脚本:
echo "file2.md" | .myscript.js --f1 file1.md
只要它比较 file1.md 和 file2.md 并且我使用 pipe-args 和 yargs 就可以做到这一点。但是当涉及到提示时,管道参数会干扰并阻止它们工作。
我尝试将它放在我的脚本之外并单独测试它,同时使用 prompt-sync 和 inquirer 并将参数传递到脚本中似乎也会影响提示。
所以我现在很确定这不是特定的提示包。
重新创建:
这是我对提示同步的测试(文件名为 prompt-sync-test.js):
#!/usr/bin/env node
const prompt = require("prompt-sync")({"sigint": true})
for (var i = 0; i < 5; i++) {
console.log("Going to bring up a prompt")
var result = prompt("This is a test -- enter something >");
console.log("Result", result)
}
...当它起作用时:
运行./prompt-sync-test.js 运行良好,每次都会询问五个问题并打印结果。
$ ./prompt-sync-test.js
Going to bring up a prompt
This is a test -- enter something >1
Result 1
Going to bring up a prompt
This is a test -- enter something >2
Result 2
Going to bring up a prompt
This is a test -- enter something >3
Result 3
Going to bring up a prompt
This is a test -- enter something >4
Result 4
Going to bring up a prompt
This is a test -- enter something >5
Result 5
...当它没有时:
但是运行echo hello world | ./prompt-sync-test.js 会打印第一个提示的消息,但是输入的任何输入都会再次重复提示消息,输入任何以前的答案,就像这样(我输入的数字与上述测试中的相同并在每个之后按回车键)...
$ echo hello world | ./prompt-test.
js
Going to bring up a prompt
This is a test -- enter something >1
This is a test -- enter something >1
2
This is a test -- enter something >1
2
3
This is a test -- enter something >1
2
3
4
This is a test -- enter something >1
2
3
4
5
This is a test -- enter something >1
2
3
4
5
在我的脚本中添加一些内容似乎会干扰提示本身。
基本上,我希望能够使用在我的脚本中输入的内容,但可以像第一种情况一样进行提示工作。我怎样才能做到这一点?
【问题讨论】:
标签: javascript node.js pipe prompt