Node.js yargs需要一个建造商选项
如何使命令中的“从生成器对象”选项成为必需选项之一Node.js yargs需要一个建造商选项,node.js,npm,yargs,Node.js,Npm,Yargs,如何使命令中的“从生成器对象”选项成为必需选项之一 require('yargs') .usage('Usage: $0 <cmd> [options]') .command( 'read', 'Read a note', { id: { demand: true, string: true }, first: { demand: true, boole
require('yargs')
.usage('Usage: $0 <cmd> [options]')
.command(
'read',
'Read a note',
{
id: {
demand: true,
string: true
},
first: {
demand: true,
boolean: true
}
},
argv => {
note.read(argv.id).then(data => {
console.log('==================note read==================');
console.log(data);
console.log('==================note read==================');
});
}
)
.help()
.strict().argv;
yargs:^12.0.5
您可以使用检查API
// code is written for logic purpose. Not tested.
.check(function (argv) {
if ((argv.id && !argv.first) || (!argv.id && argv.first)) {
return true;
} else if (argv.id && argv.first) {
throw(new Error('Error: pass either id or first option for read command'));
} else {
throw(new Error('Error: pass either id or first option for read command'));
}
})
PS:1对于选项值可以是字符串或布尔值这是我当前使用的解决方案。尽管我在寻找更好的解决方案
require('yargs')
.usage('Usage: $0 <cmd> [options]')
.command(
'read',
'Read a note',
yargs =>
yargs
.option('id', {
string: true
})
.option('first', {
boolean: true
})
.check(({ id, first }) => {
if (!id.trim() && !first) {
throw new Error('id or first option is required');
}
return true
}),
argv => {
if (argv.first) {
note.readFirst().then(data => {
console.log('==================note read==================');
console.log(data);
console.log('==================note read==================');
});
} else {
note.read(argv.id).then(data => {
console.log('==================note read==================');
console.log(data);
console.log('==================note read==================');
});
}
}
)
.help()
.strict().argv;
require('yargs'))
.usage('用法:$0[选项]')
.命令(
“读”,
“读笔记”,
yargs=>
雅格斯
.选项('id'{
字符串:true
})
.期权(“第一”{
布尔值:真
})
.check({id,first})=>{
如果(!id.trim()&&!first){
抛出新错误(“需要id或第一个选项”);
}
返回真值
}),
argv=>{
如果(argv.first){
注意.readFirst().然后(数据=>{
console.log('==============================================================================================');
控制台日志(数据);
console.log('==============================================================================================');
});
}否则{
note.read(argv.id)。然后(data=>{
console.log('==============================================================================================');
控制台日志(数据);
console.log('==============================================================================================');
});
}
}
)
.help()
.strict().argv;
yargs命令有4个选项。命令、说明、生成器和处理程序。生成器可以是对象或函数。使用函数可用于提供高级命令特定帮助
另外,我删除了对这两个选项的需求,因为使用demand它会同时询问这两个选项,但我只想要一个
此外,当将选项设置为string或boolean时,它只强制转换为该类型,而不验证该类型。因此,如果此处未提供选项,则argv.first
默认值将为false
&argv.id
默认值将为'
空字符串
另外,当从check函数抛出错误时,它实际上会显示Error对象的错误消息,但如果我们返回false,它将在控制台中显示函数体作为消息,以帮助跟踪错误
此外,如果不访问argv,yargs将不会解析
请参阅。您可以使用demandOption=true
来解决此问题
require('yargs')
.usage('Usage: $0 <cmd> [options]')
.command(
'read',
'Read a note',
yargs =>
yargs
.option('id', {
string: true
})
.option('first', {
boolean: true
})
.check(({ id, first }) => {
if (!id.trim() && !first) {
throw new Error('id or first option is required');
}
return true
}),
argv => {
if (argv.first) {
note.readFirst().then(data => {
console.log('==================note read==================');
console.log(data);
console.log('==================note read==================');
});
} else {
note.read(argv.id).then(data => {
console.log('==================note read==================');
console.log(data);
console.log('==================note read==================');
});
}
}
)
.help()
.strict().argv;