Node/commander.js – 参数解析/赋值不当

huangapple go评论68阅读模式
英文:

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 跳过了命令的名称,而是首先放置了参数,而不是解析提供的选项。

我该如何修改我的脚本,以便:

  1. 识别子命令(capture 或 list-devices)
  2. 正确解析选项,并在 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 &quot;commander&quot;;

function parseArguments() {
  const program = new Command();

  program
    .name(&#39;video-recorder&#39;)
    .description(&#39;Video recorder utility capturing screen &amp; sound output&#39;)

  program.command(&quot;list-devices&quot;).description(&quot;Lists devices&quot;);

  program
    .command(&quot;capture&quot;)
    .addOption(
      new Option(&quot;-p --profile &lt;profile&gt;&quot;, &quot;Profile to use to record&quot;)
        .default(undefined),
    )
    .addOption(
      new Option(
        &quot;-d --duration &lt;time&gt;&quot;,
        &quot;Duration of the video, in format [HH:]mm:ss&quot;,
      )
    )
    .argument(&quot;&lt;url&gt;&quot;, &quot;url to the page of the video&quot;)
    .argument(&quot;&lt;path&gt;&quot;, &quot;path to output to&quot;);

  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 &#39;9:48&#39; &#39;https://www.example.com/video-id&#39; &#39;/path/to/output_file.mp4&#39;

I get the following args dictionary printed:

{ url: &#39;capture&#39;, outpath: &#39;-p&#39; }

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:

  1. The subcommand (capture or list-devices) is recognized
  2. 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.

huangapple
  • 本文由 发表于 2023年7月14日 03:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定