如何呈现节点生成参数
How to present node spawn arguments
在人们开始喊“重复”之前,我已经检查过了
- Spawning process with arguments in node.js
- Use NodeJS spawn to call node script with arguments
- How do I pass command line arguments to a Node.js program?
第一个基本上是不同用例中的同一个问题,因此答案没有针对我的用例。
那么...如何使用 space 将命名参数与它们的值分隔开来对如下命令行进行编码?
arduino-cli compile --fqbn arduino:avr:nano
它应该像这样 (1) 吗?
let cp = child.process(
"/path/to/arduino-cli.exe",
[
"compile",
"--fqbn arduino:avr:nano"
]
);
还是这个 (2)?
let cp = child.process(
"/path/to/arduino-cli.exe",
[
"compile",
"--fqbn",
"arduino:avr:nano"
]
);
还是这个 (3)?
let cp = child.process(
"/path/to/arduino-cli.exe",
[
"compile",
"fqbn",
"arduino:avr:nano"
]
);
还是这个 (4)?
let cp = child.process(
"/path/to/arduino-cli.exe",
{
_: ["compile"],
fqbn: "arduino:avr:nano"
}
);
TypeScript 不允许最后一个选项,即使我怀疑它是正确的答案,所以我提交这个问题以供更广泛的考虑。
设置可重复测试后
let args: any[] = [];
args.push(["compile", `--fqbn ${selectedBoard.board.fqbn}`]);
args.push(["compile", "--fqbn", selectedBoard.board.fqbn]);
args.push(["compile", "fqbn", selectedBoard.board.fqbn]);
args.push({ _: ["compile"], fqbn: selectedBoard.board.fqbn });
let cp = child_process.spawn(cliPath, args[1], { cwd: getInoPath() });
cp.stdout.on("data", (data: any) => outputChannel.append(data.toString()));
cp.stderr.on("data", (data: any) => outputChannel.append(data.toString()));
cp.on("error", (err: any) => {
outputChannel.append(err);
});
我发现@jfriend00说的对,确实是二参版本
["compile", "--fqbn", selectedBoard.board.fqbn]
但还有另一个问题导致它失败——需要在选项中设置 CWD。
let cp = child_process.spawn(cliPath, args[1], { cwd: getInoPath() });
此处的关键见解是捕获错误事件 和 stderr
。在 stderr 上报告了失败,并且没有引发错误事件。暴露后stderr
问题很快就解决了
在人们开始喊“重复”之前,我已经检查过了
- Spawning process with arguments in node.js
- Use NodeJS spawn to call node script with arguments
- How do I pass command line arguments to a Node.js program?
第一个基本上是不同用例中的同一个问题,因此答案没有针对我的用例。
那么...如何使用 space 将命名参数与它们的值分隔开来对如下命令行进行编码?
arduino-cli compile --fqbn arduino:avr:nano
它应该像这样 (1) 吗?
let cp = child.process(
"/path/to/arduino-cli.exe",
[
"compile",
"--fqbn arduino:avr:nano"
]
);
还是这个 (2)?
let cp = child.process(
"/path/to/arduino-cli.exe",
[
"compile",
"--fqbn",
"arduino:avr:nano"
]
);
还是这个 (3)?
let cp = child.process(
"/path/to/arduino-cli.exe",
[
"compile",
"fqbn",
"arduino:avr:nano"
]
);
还是这个 (4)?
let cp = child.process(
"/path/to/arduino-cli.exe",
{
_: ["compile"],
fqbn: "arduino:avr:nano"
}
);
TypeScript 不允许最后一个选项,即使我怀疑它是正确的答案,所以我提交这个问题以供更广泛的考虑。
设置可重复测试后
let args: any[] = [];
args.push(["compile", `--fqbn ${selectedBoard.board.fqbn}`]);
args.push(["compile", "--fqbn", selectedBoard.board.fqbn]);
args.push(["compile", "fqbn", selectedBoard.board.fqbn]);
args.push({ _: ["compile"], fqbn: selectedBoard.board.fqbn });
let cp = child_process.spawn(cliPath, args[1], { cwd: getInoPath() });
cp.stdout.on("data", (data: any) => outputChannel.append(data.toString()));
cp.stderr.on("data", (data: any) => outputChannel.append(data.toString()));
cp.on("error", (err: any) => {
outputChannel.append(err);
});
我发现@jfriend00说的对,确实是二参版本
["compile", "--fqbn", selectedBoard.board.fqbn]
但还有另一个问题导致它失败——需要在选项中设置 CWD。
let cp = child_process.spawn(cliPath, args[1], { cwd: getInoPath() });
此处的关键见解是捕获错误事件 和 stderr
。在 stderr 上报告了失败,并且没有引发错误事件。暴露后stderr
问题很快就解决了