英文:
Node/commander.js - Improper parsing/assignment of parameters
问题
我已经编写了一个实用程序,可以通过命令行界面(CLI)调用,使用参数和标志。
不幸的是,由于我不知道的某种原因,commander.js 不能正确解析这些参数。我漏掉了什么?
以下是 commander.js 的设置脚本:
import { Command, Option } from "commander";
function parseArguments() {
const program = new Command();
program
.name('video-recorder')
.description('Video recorder utility capturing screen & sound output')
program.command("list-devices").description("列出设备");
program
.command("capture")
.addOption(
new Option("-p --profile <profile>", "用于录制的配置文件")
.default(undefined),
)
.addOption(
new Option(
"-d --duration <time>",
"视频时长,格式为 [HH:]mm:ss",
)
)
.argument("<url>", "视频页面的 URL")
.argument("<path>", "输出路径");
program.parse(process.argv);
return program;
}
async function main() {
const program = parseArguments();
const [url, outpath] = program.args;
const args = {
url: url,
outpath: outpath,
... program.opts(),
};
console.log(args)
// ...
}
main();
当使用以下命令调用上述脚本时:
node src/main.js capture -p abc -d '9:48' 'https://www.example.com/video-id' '/path/to/output_file.mp4'
我得到以下参数字典的输出:
{ url: 'capture', outpath: '-p' }
这是不正确的。看起来 commander.js 跳过了命令的名称,而是首先放置了参数,而不是解析提供的选项。
我该如何修改我的脚本,以便:
- 识别子命令(capture 或 list-devices)
- 正确解析选项,并在 CLI 调用行的末尾传递参数?
非常感谢任何帮助!谢谢!
英文:
I have written an utility that can be called via CLI, with arguments and flags.
Unfortunately, for a reason that I ignore, commander.js does not parse the arguments properly. What am I missing?
Here's the commander.js setup script:
import { Command, Option } from "commander";
function parseArguments() {
const program = new Command();
program
.name('video-recorder')
.description('Video recorder utility capturing screen & sound output')
program.command("list-devices").description("Lists devices");
program
.command("capture")
.addOption(
new Option("-p --profile <profile>", "Profile to use to record")
.default(undefined),
)
.addOption(
new Option(
"-d --duration <time>",
"Duration of the video, in format [HH:]mm:ss",
)
)
.argument("<url>", "url to the page of the video")
.argument("<path>", "path to output to");
program.parse(process.argv);
return program;
}
async function main() {
const program = parseArguments();
const [url, outpath] = program.args;
const args = {
url: url,
outpath: outpath,
... program.opts(),
};
console.log(args)
// ...
}
main();
When calling said script using
node src/main.js capture -p abc -d '9:48' 'https://www.example.com/video-id' '/path/to/output_file.mp4'
I get the following args dictionary printed:
{ url: 'capture', outpath: '-p' }
Which is incorrect. It seems like commander.js skipped the name of the command, and put the arguments first instead of parsing the options provided.
How can I modify my script such that:
- The subcommand (capture or list-devices) is recognized
- The options are correctly parsed and the arguments are passed at the end of the cli call line?
Any help would be greatly appreciated!
Thanks!
答案1
得分: 1
如果你有一个像capture
这样的子命令,请使用.action()
为子命令添加一个操作处理程序。
操作处理程序会接收已解析的参数和选项。
英文:
If you have a subcommand like capture
, add an action handler to the subcommand using .action()
.
The action handler is passed the parsed arguments and options.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论