1、初始化配置:
{
"name": "hahaya",
"version": "1.0.1",
"description": "",
"main": "index.js",
"bin": {
"hahaya": "bin/index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
#!/usr/bin/env node
const yargs = require('yargs')
//帮助处理参数解析
const { hideBin } = require('yargs/helpers')
const arg = hideBin(process.argv)
console.log(yargs(arg).argv)
1、usage 配置 cli 的开始内容
.usage('Usage: hahaya [command] <options>')
2、.demandCommand
(1, ‘A command is required. Pass —help to see all available commands and options.’) 最少要有一个参数,否则提示
3、.alias(‘h’, ‘help’) 设置别名
4、.wrap(100) 设置宽度
5、epilogue 结尾配置 dedent 去掉缩进,位于句首
.epilogue(dedent<br /> When a command fails, all logs are written to lerna-debug.log in the current working directory.<br /> For more information, find our manual at https://github.com/lerna/lerna<br />
)
6、argv 打印到控制台上
7、配置选项
const opts = {
loglevel: {
defaultDescription: 'info',
describe: 'What level of logs to report.',
type: 'string'
},
...
}
.options(opts)
8、将选项分组
// 得到这个option的名称
const globalKeys = Object.keys(opts).concat(["help", "version"]);
// 对 options 进行分组
.group(globalKeys, 'Global Options:')
.options(opts)
.options({
debug: {
type: 'boolean',
describe: 'Bootstrap debug mode',
alias: 'd'
}
})
.group(['debug'], 'dev Options:')
9、添加了一个隐藏的 option,界面不会显示
.option('ci', {
hidden: true,
type: 'boolean'
}).argv
2、完整的bin/index.js代码
#!/usr/bin/env node
const yargs = require('yargs')
const dedent = require('dedent')
const os = require('os')
//帮助处理参数解析
const { hideBin } = require('yargs/helpers')
const arg = hideBin(process.argv)
const cli = yargs(arg)
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-progress
hidden: true,
type: 'boolean'
},
'no-sort': {
describe: 'Do not sort packages topologically (dependencies before dependents).',
type: 'boolean'
},
sort: {
// proxy for --no-sort
hidden: 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'])
//配置宽度
// 配置 cli 结尾的内容
//配置结尾
// 配置 cli 结尾的内容
cli
//cli 配置首行
.usage('Usage: hahaya [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 配置结尾
.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'
}).argv