zoukankan      html  css  js  c++  java
  • gulp常用插件之yargs使用

    更多gulp常用插件使用请访问:gulp常用插件汇总


    yargs这是一款通过解析参数并生成优雅的用户界面来帮助您构建交互式命令行工具。处理命令行参数的通用解决方案,只要一句代码 var args = require('yargs').argv;就可以让命令行的参数都放在变量args上,可以根据参数判断是测试环境还是正式环境。

    更多使用文档请点击访问yargs工具官网

    安装

    npm install --save yargs
    

    使用

    • 单字符的简单参数,如传入-m=5或-m 5,则可得到args.m = 5
    • 多字符参数(必须使用双连字符),如传入--test=5或--test 5,则可得到args.test = 5。
    • 不带值的参数,如传入--production,则会被认为是布尔类型的参数,可得到args.production = true。

    简单的例子:

    #!/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!')
    }
    

    输出结果:

    $ ./plunder.js --ships=4 --distance=22
    Plunder more riffiwobbles!
    
    $ ./plunder.js --ships 12 --distance 98.7
    Retreat from the xupptumblers!
    

    复杂的例子:

    #!/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
    

    运行上面的示例 --help 以查看应用程序的帮助。

  • 相关阅读:
    七夕祭
    Running Median
    电影Cinema
    Best Cow Fences
    Sumdiv
    Tallest Cow
    激光炸弹
    Strange Towers of Hanoi
    Gerald and Giant Chess
    CF24D Broken robot
  • 原文地址:https://www.cnblogs.com/jiaoshou/p/12032844.html
Copyright © 2011-2022 走看看