yargs入门
yargs开源参考
参数演示
js
#!/usr/bin/env node
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const args = hideBin(process.argv);
const cli = yargs(args)
const dedent = require('dedent')
cli.usage('Usage: cli-test [command] <options>')
.demandCommand(1, `A command is requied. Pass --help to see all available commands and options`)
.strict()
.recommendCommands() // 控制台提示拼写相似的命令
.fail((err, msg) => { // 命令执行失败是错误信息
console.log('err', err)
console.log('msg', msg)
})
.alias('h', 'help')
.alias('v', 'version')
.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({
debug: {
type: 'boolean',
decribe: 'Boolean debug mode',
alias: 'd'
}
})
.option('ci', {
type: 'string',
decribe: 'Define global registry',
alias: 'r'
})
.group(['debug'], 'Dev Options')
.group(['registry'], 'Extra Options')
.command('init [name]', 'Do init a project', (yargs) => {
yargs
.option('name', {
type: 'string',
decribe: ' Name of a project',
alias: 'n'
});
}, (argv) => { console.log(argv)})
.command({
command: 'list',
aliases: ["ls", "la", "ll"],
describe: 'List local packages',
builder: (yargs) => {
},
handler: (argv) => {
console.log('argv', argv)
}
})
.argv;