【发布时间】:2017-11-15 10:00:39
【问题描述】:
我找不到正确配置位置参数的方法。我有这个代码:
#!/usr/bin/env node
const create = (argv) => {
console.log('create component with name:', argv.name)
}
const createBuilder = (yargs) => {
yargs.positional('name', {
desc: 'Name of the new component',
})
}
/* eslint-disable no-unused-expressions */
require('yargs')
.command({
command: 'create <name>',
desc: 'Create a new component',
builder: createBuilder,
handler: create,
})
.demandCommand(1, 'A command is required')
.help()
.argv
如果用户在创建命令后没有指定名称,我想提供自定义错误消息。
从文档中我不清楚如何做到这一点,在浏览 github 问题时,我遇到了这条评论 (#928):
我建议改为使用 demandCommand 和 demandOption(每个 记录在案)。
这些允许您配置位置参数和标志参数 分别
我尝试过各种搭配
.demandCommand(1, 'You need to provide name for the new component')
或
.demandOption('name', 'You need to provide name for the new component')
但没有运气。有人知道怎么做吗?
【问题讨论】:
标签: node.js command-line-interface yargs