yargs gulp common plug-ins of use

More gulp common plug-ins use please visit: gulp common plug-ins summary


yargs This is to help you build an interactive command-line parameters and generate analytical tools and elegant user interface. Universal solution processing command line parameters, as long as a code var args = require ( 'yargs' ) argv;. Can make the command line parameters are variable on args, may be a formal environment or test environment according to the parameter determination.

Greater use of the document, please click visit yargs tool official website .

installation

npm install --save yargs

use

  • Simple single character parameters, such as incoming or -m = 5 -m 5, can be obtained args.m = 5
  • Multi-character parameter (must be double hyphen), or as an incoming --test = 5 --test 5, can be obtained args.test = 5.
  • Without parameter values, such as incoming --production, it would be considered a Boolean type parameter obtained args.production = true.

Simple example:

#!/usr/bin/env node
const argv = require('yargs').argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}

Output:

$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!

Complex example:

#!/usr/bin/env node
require('yargs') // eslint-disable-line
  .command('serve [port]', 'start the server', (yargs) => {
    yargs
      .positional('port', {
        describe: 'port to bind on',
        default: 5000
      })
  }, (argv) => {
    if (argv.verbose) console.info(`start server on :${argv.port}`)
    serve(argv.port)
  })
  .option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging'
  })
  .argv

Running the above example --helpto view the help of the application.

Guess you like

Origin www.cnblogs.com/jiaoshou/p/12032844.html