我正在使用 yargs 为传递给脚本的参数添加命令行选项。当我与脚本名称一起发出help命令时,它不会显示 add 参数的帮助。
const yargs=require('yargs');
const argv= yargs.command('add', 'ADD A NOTE TO A FILE', {
title : {
describe : 'title of the file',
demand: 'true'
}
})
.help()
.argv;
root# node mainFile_node.js --help
Options:
--help Show help [boolean]
--version Show version number
node mainFile_node.js add
YARGS ARGV:-{ _: [ 'add' ], '$0': 'mainFile_node.js' }
答案 0 :(得分:1)
const yargs = require("yargs");
yargs.command({
command: "add",
describe: "Add new note",
builder: {
title: {
// describe: 'note title',
demandOption: true,
type: "string"
}
},
handler: function(argv) {
console.log("My title: " + argv.title);
}
});
yargs.parse();
然后在命令行中运行:
node filename.js add --title="title name"
答案 1 :(得分:0)
尝试以下代码
ab -n 9 -c 3 https://nr41lkw95j.sse.codesandbox.io/test
然后使用以下命令执行
received request: 10
received request: 11
received request: 12
sent response: 10
sent response: 11
sent response: 12
received request: 13
received request: 14
received request: 15
sent response: 13
sent response: 14
sent response: 15
received request: 16
received request: 17
received request: 18
sent response: 16
sent response: 17
sent response: 18
答案 2 :(得分:0)
const yargs=require('yargs');
const argv= yargs.command('add', 'ADD A NOTE TO A FILE', {
title : {
describe : 'title of the file',
demand: 'true'
}
}).parse()
u必须在末尾添加parse()方法。如果您将来会使用u,则parse()还将执行处理程序函数。
或根据代码
console.log(yargs.argv)
如果您没有在文件上编写任何代码,而是在命令行上工作,则此方法很有用。假设
节点app.js添加--title =“ somthing”-帮助
要查看帮助菜单,您必须在文件上键入console.log(yargs.argv)
。
答案 3 :(得分:0)
首先检查节点./main.js --h是否正常工作?
然后,仅在单独的js文件上运行node ./app.js --help,然后它将开始在先前的文件上工作。
const chalk = require('chalk');
const yargs = require('yargs');
//Create add command
yargs.command({
command:'add',
describe:'Add a new note',
handler:function(){
console.log("add a new note");
}
})
//Create remove command
yargs.command({
command:'remove',
describe:'Remove the note',
handler:function(){
console.log("Removing a note");
}
})
//Create list command
yargs.command({
command:'list',
describe:'List all the notes',
handler:function(){
console.log("List notes");
}
})
//Create read command
yargs.command({
command:'read',
describe:'read note',
handler:function(){
console.log("read a note");
}
})
console.log(yargs.argv)
答案 4 :(得分:0)