1、yargs npm用法
#!/usr/bin/env nodeconst yargs = require('yargs/yargs')const { hideBin } = require('yargs/helpers')yargs(hideBin(process.argv))//command传4个参数,第一个command [optons](命令 参数),第二个 对命令的描述//第三个builder函数 执行该命令之前的操作 第四个参数 handler 最后执行command行为.command('serve [port]', 'start the server', (yargs) => {return yargs.positional('port', {describe: 'port to bind on',default: 5000})}, (argv) => {if (argv.verbose) console.info(`start server on :${argv.port}`)serve(argv.port)}).option('verbose', {alias: 'v',type: 'boolean',description: 'Run with verbose logging'}).parse()
1、command
.command('init [name]','do init a project',yargs => {yargs.option('name', {type: 'string',describe: 'name of a project',alias: 'n'})},argv => {console.log('argv:', argv)})
2、recommendCommands
//当你输入一个错误的 command 的时候,会自动的帮助你去寻找一个最接近的 command 来提示你.recommendCommands()
3、fail
当 command 不存在时的错误处理,
当一个 command 不存在时,默认会输出 —help 的内容 ,如果我们不想看到,那么就可以在 fail 这个方法里进行定制
.fail((err, msg) => {console.log(err)})
4、parse
console.log('argv', process.argv)

// 定义一个内容const context = {testVersion: pkg.version}//会把定义的内容注入到当前的项目中.parse(argv, context)
5、$0 表示取$0这个对象
.usage('Usage: $0 [command] <options>')
6、完整写法
#!/usr/bin/env nodeconst yargs = require('yargs')const dedent = require('dedent')const os = require('os')//帮助处理参数解析const { hideBin } = require('yargs/helpers')const argv = process.argv.slice(2)const arg = hideBin(process.argv)const cli = yargs(arg)const pkg = require('../package.json')const opts = {loglevel: {defaultDescription: 'info',describe: 'What level of logs to report.',type: 'string'},concurrency: {defaultDescription: os.cpus().length,describe: 'How many processes to use when lerna parallelizes tasks.',type: 'number',requiresArg: true,hidden: true //是否显示在终端},'reject-cycles': {describe: 'Fail if a cycle is detected among dependencies.',type: 'boolean'},'no-progress': {describe: 'Disable progress bars. (Always off in CI)',type: 'boolean'},progress: {// proxy for --no-progresshidden: true,type: 'boolean'},'no-sort': {describe: 'Do not sort packages topologically (dependencies before dependents).',type: 'boolean'},sort: {// proxy for --no-sorthidden: true,type: 'boolean'},'max-buffer': {describe: 'Set max-buffer (in bytes) for subcommand execution',type: 'number',requiresArg: true}}const globalKeys = Object.keys(opts).concat(['help', 'version'])// 定义一个内容const context = {testVersion: pkg.version}//配置宽度// 配置 cli 结尾的内容//配置结尾// 配置 cli 结尾的内容cli//cli 配置首行.usage('Usage: $0 [command] <options>')// 配置输入的最小命令,为1,否则弹出提示.demandCommand(1, 'A command is required. Pass --help to see all available commands and options.')// 开启严格模式.strict()//命令别名//命令别名.alias('h', 'help').alias('v', 'version')// 配置cli的宽度和命令行一样.wrap(cli.terminalWidth()).epilogue(dedent`When a command fails, all logs are written to lerna-debug.log in the current working directory.For more information, find our manual at https://github.com/lerna/lerna`)// 对 options 进行分组.group(globalKeys, 'Global Options:').options(opts).options({debug: {type: 'boolean',describe: 'Bootstrap debug mode',alias: 'd'}}).group(['debug'], 'dev Options:')// 添加了一个隐藏的 option.option('ci', {hidden: true,type: 'boolean'}).command('init [name]','do init a project',yargs => {yargs.option('name', {type: 'string',describe: 'name of a project',alias: 'n'})},argv => {console.log('argv:', argv)})//根据@lerna/list/command的语法,command也可以传入对象.command({command: 'list',aliases: ['ls', 'la', 'll'],describe: 'List local packages',builder: yargs => {},handler: yargs => {}})//当你输入一个错误的 command 的时候,会自动的帮助你去寻找一个最接近的 command 来提示你.recommendCommands().fail((err, msg) => {console.log(err)}).parse(argv, context)//epilogue 配置结尾


