Yargs 配置

#!/usr/bin/env node
const dedent = require('dedent')
const yargs = require('yargs/yargs');
const pkg = require('../package.json');
const { hideBin } = require('yargs/helpers');

const cli = yargs();
const argv = process.argv.slice(2);
const context = {
  winbridgeVersion: pkg.version
}


cli
  .usage('Usage: imooc-test [command] <options>')
  .demandCommand(1, "A command is required. Pass --help to see all available commands and options.")
  .strict()
  .recommendCommands()
  .alias("h", "help")
  .alias("v", "version")
  .wrap(cli.terminalWidth())
  .epilogue(dedent`命令行到这里就结束了`)
  .options({
    debug: {
      type: 'boolean',
      describe: 'Bootstrap debug mode',
      alias: 'd'
    }
  })
  .option('registry', {
    type: 'string',
    describe: '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',
      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) => {
      console.log(argv);
    }
  })
  .fail((err, msg) => console.log(err, msg)) 
  .parse(argv, context);
Copyright © imooc-lego (2020 - present) all right reserved,powered by GitbookFile Modify: 2021-06-27 08:04:57

results matching ""

    No results matching ""