yargs 使用
#!/usr/bin/env node
const dedent = require('dedent');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const pkg = require('../package.json');
const context = {
cliVersion: pkg.version
}
const cli = yargs(hideBin(process.argv))
cli
.usage('Usage: $0 <command> [options]')
.strict()
.demandCommand(1, 'A command is required. Pass --help to see all available commands and options.')
.recommendCommands()
.fail((msg, err) => {
console.log('msg: ', msg)
})
.alias('h', 'help')
.alias('v', 'version')
.wrap(cli.terminalWidth())
.epilogue(dedent`
Your own log
`)
.options({
'debug': {
type: 'boolean',
describe: 'Bootstrap debug mode',
alias: 'd'
}
})
.option('registry', {
type: 'string',
describe: 'Define global registry: npm, yarn',
alias: 'r'
})
.group(['debug'], 'Debug Options:')
.group(['r'], 'Repository Options:')
.command(
'init [name]',
'do init project',
yargs => {
yargs.option('name', {
type: 'string',
describe: 'Name of project',
alias: 'n'
})
},
argv => {
console.log(argv)
}
)
.command({
command: 'list',
aliases: ['ll', 'la', 'ls'],
describe: "List local packages",
builder: yargs => {},
handler: argv => {
console.log(argv)
}
})
.argv