Yargs - 如何为省略的位置参数提供自定义错误消息
Yargs - How to provide custom error msg for omitted positional argument
我找不到正确配置位置参数的方法。我有这个代码:
#!/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):
I recommend instead using demandCommand and demandOption (each of
which are documented).
These allow you to configure positional arguments and flag arguments
separately
我尝试了各种与
的组合
.demandCommand(1, 'You need to provide name for the new component')
或
.demandOption('name', 'You need to provide name for the new component')
但运气不好。有人知道怎么做吗?
yargs的命令选项可以接受两种类型的参数。
第一个是强制性:<varName>
。如果出于某种原因,用户在键入命令时没有输入 varName
,那么它将 运行 帮助页面。
第二个是可选:[varName]
。如果用户输入命令,即使缺少 varName
,命令也会 运行.
额外:如果你想要无限的varName
变量,那么你可以为想要的选项提供一个spread operator。成为 <...varNames>
或 [...varNames]
也就是说,如果您想提供自定义错误消息,有几种方法可以解决。第一个是这个:
const program = require('yargs')
.command('create [fileName]', 'your description', () => {}, argv => {
if(argv.fileName === undefined) {
console.error('You need to provide name for the new component')
return;
}
console.log(`success, a component called ${argv.fileName} got created.`)
})
Lodash 还提供了一个 function _.isUndefined 也可以工作。
第二个是这个:
const program = require('yargs')
.command('create <fileName>', 'A description', () => {}, argv => {
}).fail((msg, err, yargs) => {
console.log('Sorry, no component name was given.')
})
program.argv
有关详细信息,请参阅 yargs api 上的 fail documentation。
tl;dr - 尝试通过将字符串 *
或 [=13=]
添加到命令名称的开头来使您的命令成为默认命令。
我发现当位置参数定义在 default command.
这是一个让它与您的代码一起工作的示例(请注意,您不再需要使用 .demandCommand()
):
require('yargs')
.scriptName('cli-app')
.command({
command: '[=10=] create <name>',
desc: 'Create a new component',
builder: yargs => {
yargs.positional('name', {
desc: 'Name of the new component'
});
},
handler: function(argv) {
console.log('this is the handler function!');
}
})
.help().argv;
输出(注意末尾的 "Not enough non-option arguments: got 1, need at least 2" 行):
➞ node cli-app.js create 1 ↵
cli-app create <name>
Create a new component
Positionals:
name Name of the new component
Options:
--version Show version number [boolean]
--help Show help [boolean]
Not enough non-option arguments: got 1, need at least 2
我找不到正确配置位置参数的方法。我有这个代码:
#!/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):
I recommend instead using demandCommand and demandOption (each of which are documented).
These allow you to configure positional arguments and flag arguments separately
我尝试了各种与
的组合.demandCommand(1, 'You need to provide name for the new component')
或
.demandOption('name', 'You need to provide name for the new component')
但运气不好。有人知道怎么做吗?
yargs的命令选项可以接受两种类型的参数。
第一个是强制性:<varName>
。如果出于某种原因,用户在键入命令时没有输入 varName
,那么它将 运行 帮助页面。
第二个是可选:[varName]
。如果用户输入命令,即使缺少 varName
,命令也会 运行.
额外:如果你想要无限的varName
变量,那么你可以为想要的选项提供一个spread operator。成为 <...varNames>
或 [...varNames]
也就是说,如果您想提供自定义错误消息,有几种方法可以解决。第一个是这个:
const program = require('yargs')
.command('create [fileName]', 'your description', () => {}, argv => {
if(argv.fileName === undefined) {
console.error('You need to provide name for the new component')
return;
}
console.log(`success, a component called ${argv.fileName} got created.`)
})
Lodash 还提供了一个 function _.isUndefined 也可以工作。
第二个是这个:
const program = require('yargs')
.command('create <fileName>', 'A description', () => {}, argv => {
}).fail((msg, err, yargs) => {
console.log('Sorry, no component name was given.')
})
program.argv
有关详细信息,请参阅 yargs api 上的 fail documentation。
tl;dr - 尝试通过将字符串 *
或 [=13=]
添加到命令名称的开头来使您的命令成为默认命令。
我发现当位置参数定义在 default command.
这是一个让它与您的代码一起工作的示例(请注意,您不再需要使用 .demandCommand()
):
require('yargs')
.scriptName('cli-app')
.command({
command: '[=10=] create <name>',
desc: 'Create a new component',
builder: yargs => {
yargs.positional('name', {
desc: 'Name of the new component'
});
},
handler: function(argv) {
console.log('this is the handler function!');
}
})
.help().argv;
输出(注意末尾的 "Not enough non-option arguments: got 1, need at least 2" 行):
➞ node cli-app.js create 1 ↵
cli-app create <name>
Create a new component
Positionals:
name Name of the new component
Options:
--version Show version number [boolean]
--help Show help [boolean]
Not enough non-option arguments: got 1, need at least 2