commander.js:如何指定必需的cli参数

13
我正在使用 commander.js 包来解析命令行参数:我想将一个标志变成非必须的。在git repo中,API 和测试模糊地提到了如何使一个标志是必需的,但我通常需要遵循指示才能明白。

那么这真的可能吗?如果不满足要求,脚本是否会抛出异常?
2个回答

23

4
指挥官现在有了program.requiredOption()来将选项标记为必需。 - jiminssy

6

这取决于你如何编写参数。

  • 使用<>是必需的。
  • 使用[]则不是必需的。

请看示例。

const commander = require('commander')
    , program = new commander.Command()

program
   .command('autorecord')
   .argument('<title>', 'Title and file name of record') // is required
   .argument('[type]', 'Type of record. "undefined" by default') // is not required

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接