Cheat Sheet速查表
跳转到主要内容

Yargs 速查表

本文根据 Devhints / Yargs 翻译。代码示例及版本范围沿用原文。

基本用法

var argv = require('yargs').argv;

argv._         // [ ... ]
argv.$0        // "node bin/mybin"
argv.verbose   // --verbose

帮助与版本信息

var argv = require('yargs')

  // version
  .alias('v', 'version')
  .version(function() { return require('../package').version; })
  .describe('v', 'show version information')

  // help text
  .alias('h', 'help')
  .help('help')
  .usage('Usage: $0 -x [num]')
  .showHelpOnFail(false, "Specify --help for available options")

选项

  .option('f', {
      alias : 'file',
      describe: 'x marks the spot',
      type: 'string', /* array | boolean | string */
      nargs: 1,
      demand: true,
      demand: 'file is required',
      default: '/etc/passwd'
      // also: count:true, requiresArg:true
  })

  .options({
    f: { ... }
  })

示例及更多帮助信息

  // more help
  .example('...')
  .epilog('copyright 2015')
  .command('start', 'start a server')

堆叠(Stacking)

  .count('verbose')

argv.verbose // -vvv => 3

排除非显式项

  .strict()

方法

yargs.showHelp()
yargs.help() //=>string