Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从管道获取参数,但同时运行提示?_Javascript_Node.js_Pipe_Prompt - Fatal编程技术网

Javascript 从管道获取参数,但同时运行提示?

Javascript 从管道获取参数,但同时运行提示?,javascript,node.js,pipe,prompt,Javascript,Node.js,Pipe,Prompt,我正在编写一个节点脚本,设计用于从Bash终端执行。它需要几个文件名,然后通过提示向用户询问如何处理它们。我使用yargs解析命令行参数,使用prompt sync询问用户问题,这一切似乎都很好 除非我像这样在脚本中插入参数: echo "file2.md" | .myscript.js --f1 file1.md 这在比较file1.md和file2.md时有效,我将pipe args与yargs结合使用以达到这一目的。但是当涉及到提示时,使用管道参数会干扰并阻止它们工作 我已经尝试在我的脚

我正在编写一个节点脚本,设计用于从Bash终端执行。它需要几个文件名,然后通过提示向用户询问如何处理它们。我使用
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
):

…如果没有: 但是运行
echo hello world |/prompt sync test.js
会打印第一个提示的消息,但是输入的任何输入都会再次重复提示消息,并输入以前的任何答案,就像这样(我输入的数字与上面描述的测试中的数字相同,并在每个数字之后按enter键)

在我的脚本中添加某些内容似乎会干扰提示符本身


基本上,我希望能够使用脚本中的管道,但像第一种情况一样使用提示。如何实现这一点?

您可以在子shell中使用
cat
,如下所示:

(echo "file2.md"; cat) | .myscript.js --f1 file1.md
(echo hello world; cat) | ./prompt-test.js
您还可以添加
exec
,以减少一个进程:

(echo "file2.md"; exec cat) | .myscript.js --f1 file1.md
(echo hello world; exec cat) | ./prompt-test.js
您也可以将
cat
放在外部并使用
-

cat <(echo "file2.md") - | .myscript.js --f1 file1.md
cat <(echo hello world) - | ./prompt-test.js

cat我找到了一个解决方案,它使用了包的组合,即和

我切换到,因为它允许您为每个问题配置一个名为
stdin
的变量,这意味着您可以决定从何处获取输入

大多数提示包似乎默认使用
process.stdin
作为输入。这通常很好,但是当您将数据导入命令时,
process.stdin
被管道占用,因此我以前遇到过这个问题。但是,如果您知道管道占用了
process.stdin
,您仍然可以从用户那里获得键盘输入

这就是我们的优势所在。这是一个非常简单的包,用于检查管道是否已被使用

我几乎可以用下面的脚本重新创建我在问题中概述的成功结果——唯一缺少的是提示前消息(
将显示提示
)。我添加了管道参数和yarg,以显示管道中的数据是保留的

#!/usr/bin/env node
const prompts = require("prompts");
const tty = require("tty");
const pipe = require("pipe-args").load();
const argv = require("yargs").argv;
const ttys = require("ttys");

const questions = [0, 1, 2, 3, 4]
  .map(x => ({
    type: "text",
    name: `prompt-${x}`,
    message: "This is a test -- enter something",
    stdin: ttys.stdin
  }));


const onSubmit = function (prompt, answer) {
  console.log("Result", answer); // => { value: 24 }
};

(async () => {
  const response = await prompts(questions, { onSubmit });
  console.log(argv);
  ttys.stdin.destroy();
})();

谢谢你的回复。我对此进行了测试,但仍然看到了提示的相同行为。添加了一些关于如何重新创建我遇到的问题的详细信息,以使问题更清楚。
(echo "file2.md"; exec cat) | .myscript.js --f1 file1.md
(echo hello world; exec cat) | ./prompt-test.js
cat <(echo "file2.md") - | .myscript.js --f1 file1.md
cat <(echo hello world) - | ./prompt-test.js
#!/usr/bin/env node
const prompts = require("prompts");
const tty = require("tty");
const pipe = require("pipe-args").load();
const argv = require("yargs").argv;
const ttys = require("ttys");

const questions = [0, 1, 2, 3, 4]
  .map(x => ({
    type: "text",
    name: `prompt-${x}`,
    message: "This is a test -- enter something",
    stdin: ttys.stdin
  }));


const onSubmit = function (prompt, answer) {
  console.log("Result", answer); // => { value: 24 }
};

(async () => {
  const response = await prompts(questions, { onSubmit });
  console.log(argv);
  ttys.stdin.destroy();
})();