yargs常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

const yargs = require('yargs/yargs')
// 页脚格式化插件
const dedent = require('dedent')
const pkg = require('../package.json')

const cli = yargs()
const argv = process.argv.slice(2)

const context = {
cliVersion: pkg.version
}

cli
.usage('Usage:lio-imooc-test [command] <options>')
.demandCommand(1, 'A command is required. 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` hello
world`)// 去除缩进
.options({// 对所有的command添加的options都有效
debug: {// 添加debug命令
type: 'boolean',
describe: 'Bootstrap debug mode',
alias: 'd'
}
})
.option('ci', {
type: 'string',
describe: 'Define global registry',
alias: 'r'
})
.group(['debug'], 'Dev Options:')// 把debug添加到Dev Options中
.group(['registry'], 'Extra Options:')
.command('init [name]', 'Do init a project', (yargs) => {
yargs
.option('name', {
type: 'string',
describe: '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) => { }
})
.parse(argv, context)// 将默认参数与配置参数合并