我正在使用 commander.js 包来解析命令行参数:我想将一个标志变成非必须的。在git repo中,API 和测试模糊地提到了如何使一个标志是必需的,但我通常需要遵循指示才能明白。
那么这真的可能吗?如果不满足要求,脚本是否会抛出异常?
我猜这在commander.js中不被支持:https://github.com/visionmedia/commander.js/issues/44
但是你可以在你的程序中做类似这样的事情 -
if (!program.myoption)
throw new Error('--myoption required')
这取决于你如何编写参数。
<>
是必需的。[]
则不是必需的。请看示例。
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
program.requiredOption()
来将选项标记为必需。 - jiminssy